MarketHours TimeTillClose Backtesting Bug
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
- 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
- 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.
Replies
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
Intetics
08 Dec 2023, 13:57 ( Updated at: 09 Dec 2023, 05:51 )
Hi PanagiotisCharalampous, is there an update on this one?
@Intetics