Fast Close of Multiple Positions

Created at 23 Oct 2020, 06:01
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!
HE

heliodorstar

Joined 09.09.2020

Fast Close of Multiple Positions
23 Oct 2020, 06:01


Hello,

I am developing a robot that needs to close a large number of positions as fast as possible.

Assume for example that I have opened close to 300 positions and I want to close all EURUSD positions {let's say those are 50 or so}.

Currently I use:

// close all positions from that instrument
foreach(var pos in Positions
		.Where(x => x.SymbolName == "EURUSD"))
		{
			ClosePositionAsync(pos);
		}

Is that the fastest possible way to do that?

For instance before closing the positions if I iterate all positions in #Positions and get some index of them {or maybe the Id of the position?} and add that index/Id to a list, can I assign closure to all of those selected without iterating all 300+ positions in #Positions?

Something like {in pseudo code}:

For (all items in that list) { ClosePositionAsync(item) };

 


@heliodorstar
Replies

PanagiotisCharalampous
23 Oct 2020, 08:54

Hi heliodorstar,

You could consider preprocessing the Where query and save the results in a separate list to have the positions ready when the time to close them comes.

You can also consider using Parallel.ForEach

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

heliodorstar
23 Oct 2020, 12:05

Thanks very much for your answer, Panagiotis!

I decided to follow through your advice and first created a dictionary of type <string, List<Position>>.

After that I looped through all open positions and added them to the above dictionary using the pair name as a key.

Then I cycled the keys of the dictionary and did my calculations for every single pair and closed all suitable positions using:

Parallel.ForEach(
	dict_pos[pair],  // #dict_pos being my pair positions dictionary
	pos => close_pos_async(pos)
	);

where #close_pos_async is simply:

private void close_pos_async(Position pos)
	{
        ///<summary>Performs asynchronous close of a position.</summary>

		ClosePositionAsync(pos);
    }

 


@heliodorstar

ctid1074959
21 Jan 2021, 16:02

heliodorstar - Could I get your help with this?

I need help track all manually entered positions so I can can close each individually using cTrader Automate as they hit defined variables i.e. profitTarget = +X or lossTarget = -X

Would you solution below work for this?

Would you mind sharing a simple bot that uses this approach?

I could pay via Venmo

Thanks,

-DE  

 

heliodorstar said:

Thanks very much for your answer, Panagiotis!

I decided to follow through your advice and first created a dictionary of type <string, List<Position>>.

After that I looped through all open positions and added them to the above dictionary using the pair name as a key.

Then I cycled the keys of the dictionary and did my calculations for every single pair and closed all suitable positions using:

Parallel.ForEach(
	dict_pos[pair],  // #dict_pos being my pair positions dictionary
	pos => close_pos_async(pos)
	);

where #close_pos_async is simply:

private void close_pos_async(Position pos)
	{
        ///<summary>Performs asynchronous close of a position.</summary>

		ClosePositionAsync(pos);
    }

 

 


@ctid1074959

PanagiotisCharalampous
22 Jan 2021, 08:01

Hi ctid1074959,

You can consider posting a Job too.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous