How do i close multiple orders order by profit descending or time ascending ...

Created at 26 Jun 2022, 11:57
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
MongolTrader's avatar

MongolTrader

Joined 12.02.2015

How do i close multiple orders order by profit descending or time ascending ...
26 Jun 2022, 11:57


How do i close orders by profit descending or time descending etc..


@MongolTrader
Replies

firemyst
27 Jun 2022, 07:14

//Quick example on how to sort positions by NetProfit (not gross profit)
//Change things around if you want to sort by time or something else.

foreach (Position p in Positions.FindAll(YourLabel, s.Name).OrderBy(x => x.NetProfit))
{
	ClosePositionAsync(p, (TradeResult r) =>
	{
		if (r.IsSuccessful)
		{
			Print("Closed position \"{0} {1}\".", p.Id, p.Label);
		}
		else if (!r.IsSuccessful)
		{
			Print("WARNING! Could not close position \"{0} {1}\"!", p.Id, p.Label);
			Print("Error message: {0}", r.Error.Value);
		}
	});
}

 


@firemyst

MongolTrader
29 Jun 2022, 09:18

RE:

firemyst said:

//Quick example on how to sort positions by NetProfit (not gross profit)
//Change things around if you want to sort by time or something else.

foreach (Position p in Positions.FindAll(YourLabel, s.Name).OrderBy(x => x.NetProfit))
{
	ClosePositionAsync(p, (TradeResult r) =>
	{
		if (r.IsSuccessful)
		{
			Print("Closed position \"{0} {1}\".", p.Id, p.Label);
		}
		else if (!r.IsSuccessful)
		{
			Print("WARNING! Could not close position \"{0} {1}\"!", p.Id, p.Label);
			Print("Error message: {0}", r.Error.Value);
		}
	});
}

 

Thank you very much. It delete from low profit to high. Can you provide code delete from high profit to low. 


@MongolTrader