MarketHours TimeTillClose Backtesting Bug

Created at 29 Nov 2023, 15:30
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!
IN

Intetics

Joined 29.11.2023

MarketHours TimeTillClose Backtesting Bug
29 Nov 2023, 15:30


Hi there,

I'm trying to close all CFD positions 30 minutes before the market close. To achive it I do:

protected override void OnBarClosed()
{
    foreach (Position p in Positions)
    {
        if (p.MarketHours.TimeTillClose().TotalMinutes <= 30)
        {
            ClosePosition(p);
        }
    }
}

This code works for half a year and then it goes off for 1 hour. And then it works again. It happens because of the DST time change.

I'd expect that TimeTillClose would always return the actual time for the day when it's called. However, it looks like what's happening is that cTrader caches Symbol information (including MarketHour) for the first day of backtest. Which is clearly wrong.

Steps to reproduce:

  • Symbol: Apple
  • Open a position on the start of the day
  • Try to close the position with the code above
  1. Set 20/Mar/2023 as the first date of backtest. Set breakpoint for 11/Apr/2023 and check the p.MarketHours.Sessions[0].EndTime. The value would be equal to 16:58:59
  2. Set 05/Apr/2023 as the first date of backtest. Set breakpoint for 11/Apr/2023 and check the p.MarketHours.Sessions[0].EndTime. The value would be equal to 15:58:59

The same happens if I do Symbol curSymbol = Symbols.GetSymbol(p.SymbolName); instead of p.

As you can see, depending on the first date of the backtest, you get completelly different values for the market close for the same (11/Apr/2023) day, which doesn't make any sense.


@Intetics
Replies

Intetics
08 Dec 2023, 13:57 ( Updated at: 09 Dec 2023, 05:51 )

Hi PanagiotisCharalampous, is there an update on this one?


@Intetics

PanagiotisCharalampous
09 Dec 2023, 05:58

Hi there,

Please send us the complete cBot code and your broker so that we can reproduce this issue on backtesting.

Best regards,

Panagiotis


@PanagiotisCharalampous

Intetics
20 Dec 2023, 15:26

RE: MarketHours TimeTillClose Backtesting Bug

PanagiotisCharalampous said: 

Hi there,

Please send us the complete cBot code and your broker so that we can reproduce this issue on backtesting.

Best regards,

Panagiotis

Somehow I can't reproduce it anymore. Really weird. I'll keep an eye on it.


@Intetics