Position closing issue

Created at 13 Apr 2022, 18:40
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!
RI

riku.nortje@gmail.com

Joined 19.07.2016

Position closing issue
13 Apr 2022, 18:40


In 4.2.1 beta bot, when closing trades of specific type not all trades of that type are closed.

Example of buggy code

foreach (var pos in Positions)
{
        if (pos.TradeType == TradeType.Sell)
        {
                 var res = ClosePosition(pos);

                 ......

 

This would leave some short trades open. To bypass the issue I had to first put all positions in a list which solved the problem

foreach (var pos in Positions.ToList())
{
        if (pos.TradeType == TradeType.Sell)
        {
                 var res = ClosePosition(pos);

 

Seems there is some bug when enumerating over the Positons collection and closing some of them 


@riku.nortje@gmail.com
Replies

amusleh
14 Apr 2022, 10:43

Hi,

I tried to replicate the issue, but I was not able to.

The Positions and PendingOrders collections are derived from IEnumerable, and if your code is running a loop over them and they get modified it will cause an enumerable modified exception which is normal .NET behavior.

These collections aren't thread safe, actually Automate API in general is not thread safe, 

Please post a minimum cBot sample that can reproduce this issue.

And regarding creating a copy of Positions collections, use ToArray instead of ToList, as the later overhead is much larger than the former.


@amusleh

riku.nortje@gmail.com
14 Apr 2022, 11:55

RE:

amusleh said:

Hi,

I tried to replicate the issue, but I was not able to.

The Positions and PendingOrders collections are derived from IEnumerable, and if your code is running a loop over them and they get modified it will cause an enumerable modified exception which is normal .NET behavior.

These collections aren't thread safe, actually Automate API in general is not thread safe, 

Please post a minimum cBot sample that can reproduce this issue.

And regarding creating a copy of Positions collections, use ToArray instead of ToList, as the later overhead is much larger than the former.

 

Thanks for looking into this. I will set up a simple bot to duplicate the issue and post it. Just to add some clarification, the issue occurred specifically during backtesting and most probably optimisation phases and not normal bot running. I initially noticed it when back testing in visual mode as the results after optimisation did not make sense. The bot is allowed to open multiple positions of the same type, but closes all positions of that type if it decides it is going to trade in the opposite direction. What happens is that after the loop above executed, some positions remained open which should not be the case, these then remained open, even if after some time the bot opened trades in the same direction again and then closed it. So it started having more and more of these loose hanging trades that never get closed. Using the tolist() solved the issue for me. Simple bot that this happens to to follow shortly    


@riku.nortje@gmail.com