Backtest indicator value different to cBot indicator value just after 00:00 UTC
Backtest indicator value different to cBot indicator value just after 00:00 UTC
20 Nov 2023, 23:31
Hi,
I've been noticing discrepancies between how a cBot functions using a custom indicator and what the custom indicator shows on the chart.
I've managed to replicate the issue using a basic custom indicator and cBot; I've used Print statements in the indicator to help identify when the indicator ClosePrices value and cBot ClosePrices value start to diverge and it always seems to be the bar after 00:00 UTC on any timeframe.
For example here is the log printed from Automate → Indicator:
20/11/2023 23:26:04.354 | Indicator instance [Check Ind, AUDUSD_SB, m30] loaded.
20/11/2023 23:26:04.510 | Bars.OpenTimes[j]): 15/11/2023 23:00:00
20/11/2023 23:26:04.510 | CheckIndicator[j]: 0.651
20/11/2023 23:26:04.510 | Bars.OpenTimes[j]): 15/11/2023 23:30:00
20/11/2023 23:26:04.510 | CheckIndicator[j]: 0.65111
20/11/2023 23:26:04.510 | Bars.OpenTimes[j]): 16/11/2023 00:00:00
20/11/2023 23:26:04.510 | CheckIndicator[j]: 0.65047
20/11/2023 23:26:04.510 | Bars.OpenTimes[j]): 16/11/2023 00:30:00
20/11/2023 23:26:04.510 | CheckIndicator[j]: 0.64962
20/11/2023 23:26:04.510 | Bars.OpenTimes[j]): 16/11/2023 01:00:00
20/11/2023 23:26:04.510 | CheckIndicator[j]: 0.6492
And here is the log printed from Automate → cBot using backtest for the same time period:
16/11/2023 00:00:00.000 | CBot instance [Check Bot, AUDUSD_SB, m30] started.
16/11/2023 01:00:00.000 | Bars.OpenTimes[j]): 15/11/2023 23:00:00
16/11/2023 01:00:00.000 | CheckIndicator[j]: 0.651
16/11/2023 01:00:00.000 | Bars.OpenTimes[j]): 15/11/2023 23:30:00
16/11/2023 01:00:00.000 | CheckIndicator[j]: 0.65111
16/11/2023 01:00:00.000 | Bars.OpenTimes[j]): 16/11/2023 00:00:00
16/11/2023 01:00:00.000 | CheckIndicator[j]: 0.65047
16/11/2023 01:00:00.000 | Bars.OpenTimes[j]): 16/11/2023 00:30:00
16/11/2023 01:00:00.000 | CheckIndicator[j]: 0.65039
16/11/2023 01:00:00.000 | Bars.OpenTimes[j]): 16/11/2023 01:00:00
16/11/2023 01:00:00.000 | CheckIndicator[j]: 0.64965
16/11/2023 23:59:00.000 | CBot instance [Check Bot, AUDUSD_SB, m30] stopped.
The bold values above are an example of where the cBot and indicator differ, these sorts of slight differences have been interfering with buy/sell signals in my other cBot. In another cBot I'm using VWAP and I'm also seeing large TickVolume discrepancies after the midnight bar but that issue has been hard for me to replicate.
Please see the indicator and cBot code below, could you take a look and let me know if I'm doing something wrong?
Thanks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo
{
[Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
public class CheckInd : Indicator
{
[Output("CheckInd Indicator")]
public IndicatorDataSeries CheckIndicator { get; set; }
private int lastIndex = 0;
public override void Calculate(int i)
{
if (i == lastIndex) return;
lastIndex = i;
CheckIndicator[i] = Bars.TypicalPrices[i];
if (Bars.OpenTimes[i-2].TimeOfDay.Ticks == 0 && Bars.OpenTimes[i-2].Day == 16 && Bars.OpenTimes[i-2].Month == 11)
{
for (int j = i - 4; j <= i; j++)
{
Print("Bars.OpenTimes[j]): ",Bars.OpenTimes[j]);
Print("CheckIndicator[j]: ",CheckIndicator[j]);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class CheckBot : Robot
{
public CheckInd checkind;
protected override void OnStart()
{
checkind = Indicators.GetIndicator<CheckInd>();
}
protected override void OnBar()
{
int i = Bars.OpenTimes.GetIndexByTime(Bars.OpenTimes.LastValue);
double x = checkind.CheckIndicator[i];
}
}
}
Replies
PanagiotisCharalampous
26 Nov 2023, 07:53
Backtest indicator value different to cBot indicator value just after 00:00 UTC
Hi attila2k1,
Your problem here is that you are printing the indicator value on the opening of the bar. The values of the indicator might change by the time the bar is closed, hence cause such discrepancies.
Best regards,
Panagiotis
@PanagiotisCharalampous
PanagiotisCharalampous
26 Nov 2023, 07:55
RE: Same here! I've noticed too
firemyst said:
And have posted about it previously, but nobody from @Spotware has responded nor addressed the issue:
https://ctrader.com/forum/calgo-support/41630
AFAIK this has been fixed. Do you still experience this?
@PanagiotisCharalampous
attila2k1
26 Nov 2023, 09:26
( Updated at: 26 Nov 2023, 11:08 )
Backtest indicator value different to cBot indicator value just after 00:00 UTC
Thank you Panagiotis and firemyst for your comments, as a result I was able to fix all my problems.
Currently all my cBots use index minus 1 or less for most calculations because I'm trying to avoid working with open candles, but with custom indicators I was still using index as opposed to index minus 1.
In my indicator programs I changed all the references of index to be index minus 1 and the discrepancies with ClosePrices and VolumeTicks all disappeared from the log, chart and buy/sell signals.
Thanks,
attila2k1.
@attila2k1
firemyst
26 Nov 2023, 10:02
( Updated at: 26 Nov 2023, 11:07 )
RE: RE: Same here! I've noticed too
PanagiotisCharalampous said:
firemyst said:
And have posted about it previously, but nobody from @Spotware has responded nor addressed the issue:
https://ctrader.com/forum/calgo-support/41630
AFAIK this has been fixed. Do you still experience this?
I didn't know this was fixed. I will have a look next chance I get as I haven't bothered to check since reporting the issue since it didn't seem to be acknowledged.
I'm sure you know by now I'm not shy on raising issues, assume no news is good news. :-)
@firemyst
firemyst
25 Nov 2023, 09:27 ( Updated at: 26 Nov 2023, 07:27 )
Same here! I've noticed too
And have posted about it previously, but nobody from @Spotware has responded nor addressed the issue:
https://ctrader.com/forum/calgo-support/41630
@firemyst