Backtesting freezes when opening and closing positions on the same tick, but only while indicator is on the chart

Created at 21 Jul 2022, 08:14
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!
BI

BigManDave

Joined 02.12.2021

Backtesting freezes when opening and closing positions on the same tick, but only while indicator is on the chart
21 Jul 2022, 08:14


In attempts to migrate my cBot to cTrader 4.3, I got a problem during backtesting where my cBot would freeze as soon as it opened a position.

The cBot is doing some statistical data gathering operations, and this involves opening and closing positions within the same tick. For some reason, opening and closing positions within the same tick seemed to cause it to freeze when certain conditions were met (I don't know what these conditions are), but only when my indicator was on the chart in visual backtesting.

 

I have made some sample code to reproduce the issue.

I reproduced the issue backtesting on Pepperstone's SpotCrude symbol from 19/06/2022 to 19/07/2022, with Tick data from the server and Commission = 0, at 2000x speed.

It seems to crash between 0225-0245 on 20/06/2022.

When Ticks == 10000, it closes a position and this causes it to freeze. After closing this position, I set a breakpoint at OnTick, and it does not get called again after that.

You can get the indicator here 

using System.Diagnostics;
using cAlgo.API;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.FullAccess)]
    public class TestPOC : Robot
    {
        [Parameter(DefaultValue = "Hello world!")]
        public string Message { get; set; }

        private int Ticks { get; set; }
        private int TickCount { get; } = 5000;

        private int NumPositions { get; } = 10;

        private Position Position { get; set; }

        private Position OtherPosition { get; set; }

        protected override void OnStart()
        {
#if DEBUG
            var result = Debugger.Launch();

            if (result is false)
            {
                Print("Debugger launch failed");
            }
#else
            Print("Not debugging");
#endif
            // To learn more about cTrader Automate visit our Help Center:
            // https://help.ctrader.com/ctrader-automate

            Print(Message);
        }

        protected override void OnTick()
        {
            if (Ticks % TickCount == 0)
            {
                for (int i = 0; i < NumPositions; i++)
                {
                    OtherPosition = OpenPosition(Symbol.VolumeInUnitsMin);
                    ClosePosition(OtherPosition);
                }
            }

            if (Ticks++ == TickCount)
            {
                Position = OpenPosition(Symbol.VolumeInUnitsMin * 5, TradeType.Buy);
            }
            else if (Ticks == TickCount * 2)
            {
                // After closing the position once Ticks == 10000,
                //it freezes. I set a breakpoint at OnTick and it does not get called again.
                ClosePosition(Position); 
            }

            // Handle price updates here
        }

        private Position OpenPosition(double volume, TradeType direction = TradeType.Sell)
        {
            var result = ExecuteMarketOrder(direction, SymbolName, volume);
            if (!result.IsSuccessful)
            {
                Debugger.Break();
            }

            return result.Position;
        }
    }
}

 


@BigManDave
Replies

PanagiotisCharalampous
21 Jul 2022, 11:28

Hi David,

It seems we will need the TripleMASlopeAverage as well.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

BigManDave
21 Jul 2022, 14:22

RE:

PanagiotisCharalampous said:

Hi David,

It seems we will need the TripleMASlopeAverage as well.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

Okay, this one should work. It doesn't have source code, however.

 

 


@BigManDave

PanagiotisCharalampous
21 Jul 2022, 14:35

Hi David,

We will need the source code so that we can properly investigate.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

BigManDave
21 Jul 2022, 15:07

RE:

PanagiotisCharalampous said:

Hi David,

We will need the source code so that we can properly investigate.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

Here you go.


@BigManDave

PanagiotisCharalampous
25 Jul 2022, 16:28

Hi David,

We managed to reproduce the problem. The team will investigate further and fix any issues identified in an upcoming update.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

BigManDave
25 Jul 2022, 16:37

RE:

PanagiotisCharalampous said:

Hi David,

We managed to reproduce the problem. The team will investigate further and fix any issues identified in an upcoming update.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

Brilliant news! Thanks.


@BigManDave

BigManDave
25 Jul 2022, 18:41

RE:

PanagiotisCharalampous said:

Hi David,

We will need the source code so that we can properly investigate.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

I should note also that this bug happens even if my cBot and all the indicators are built with .NET 4, but only on cTrader 4.3.

No issues on cTrader 4.1 though


@BigManDave