Cross timeframe indicator value is different in backtesting and on screen.
Cross timeframe indicator value is different in backtesting and on screen.
20 Oct 2020, 20:58
Hi,
I notice that the indicator value that i get during backtesting is different with on screen indicator value.
The indicator is a custom indicator that grab MACD value from a different timeframe and show it on the current timeframe. The image given below shows the custom indicator grabbing daily MACD value to show in H4 timeframe.
As you can see from the 2nd image, the printed value is different with the on screen value in the 1st image.
I have double confirmed that i am using the same parameters during backtesting and placing the indicator on screen.
I have been debugging this issue for a few days, I really have no idea why this happen. Can anyone please let me know where I did wrong? Thank you in advance.
Below is my custom indicator code and the simple cBot code that print the MACD value during backtesting.
Custom Indicator:
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class Timeframe_MACD : Indicator
{
[Parameter("Long Cycle", DefaultValue = 22, MinValue = 1, Step = 1)]
public int LongCycle { get; set; }
[Parameter("Short Cycle", DefaultValue = 11, MinValue = 1, Step = 1)]
public int ShortCycle { get; set; }
[Parameter("Signal Period", DefaultValue = 9, MinValue = 1, Step = 1)]
public int SignalPeriod { get; set; }
[Parameter("Timeframe", DefaultValue = "Daily")]
public TimeFrame Timeframe { get; set; }
[Output("Histogram", PlotType = PlotType.Histogram, Thickness = 1, LineColor = "Aqua")]
public IndicatorDataSeries Histogram { get; set; }
[Output("MACD", PlotType = PlotType.Line, Thickness = 1, LineColor = "Blue")]
public IndicatorDataSeries MACD { get; set; }
[Output("Signal", PlotType = PlotType.Line, Thickness = 1, LineColor = "Red")]
public IndicatorDataSeries Signal { get; set; }
private MacdCrossOver MACDIndicator;
private Bars DiffTFSeries;
protected override void Initialize()
{
DiffTFSeries = MarketData.GetBars(Timeframe);
MACDIndicator = Indicators.MacdCrossOver(DiffTFSeries.ClosePrices, LongCycle, ShortCycle, SignalPeriod);
}
public override void Calculate(int index)
{
var TFidx = GetIndexByDate(DiffTFSeries, Bars.OpenTimes[index]);
if (TFidx != -1)
{
Histogram[index] = MACDIndicator.Histogram[TFidx];
MACD[index] = MACDIndicator.MACD[TFidx];
Signal[index] = MACDIndicator.Signal[TFidx];
//Print("Time: " + Bars.OpenTimes[index] + " | Hist: " + Histogram[index]);
}
}
private int GetIndexByDate(Bars series, DateTime time)
{
for (int i = series.ClosePrices.Count - 1; i > 0; i--)
{
if (time == series.OpenTimes[i])
return i;
}
return -1;
}
}
}
cBot that print the indicator value during backtesting:
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class DiffTimeframeMACD : Robot
{
[Parameter(DefaultValue = 22)]
public int LongCycle { get; set; }
[Parameter(DefaultValue = 11)]
public int ShortCycle { get; set; }
[Parameter(DefaultValue = 9)]
public int Signal { get; set; }
[Parameter(DefaultValue = "Daily")]
public TimeFrame TF { get; set; }
private Timeframe_MACD TF_MACD;
protected override void OnStart()
{
TF_MACD = Indicators.GetIndicator<Timeframe_MACD>(LongCycle, ShortCycle, Signal, TF);
}
protected override void OnBar()
{
Print("Time: {0} | MACD Hist: {1}", Bars.OpenTimes.LastValue, GetLastNonNANValue(TF_MACD.Histogram, 0));
}
// index = 0 -> return the 1st Non NAN value in the series.
// index = 1 -> return the 2nd Non NAN value in the series.
// ...
private double GetLastNonNANValue(IndicatorDataSeries series, int index)
{
int c = index;
double result = Double.NaN;
for (int i = 0; i < series.Count; i++)
{
if (!Double.IsNaN(series.Last(i)))
{
result = series.Last(i);
c--;
if (c < 0)
break;
}
}
return result;
}
}
}
Replies
lohzi97
22 Oct 2020, 11:20
( Updated at: 21 Dec 2023, 09:22 )
RE:
PanagiotisCharalampous said:
Hi lohzi97,
This seems to be a bug. I reported this to the product team and will be fixed in a future update. The correct values are the ones printed by the cBot.
Best Regards,
Panagiotis
Thanks for the reply.
I think the correct values are the ones that is on screen, aren't it? If you compare it with the cTrader built in MACD indicator, you will notice that in daily timeframe, 19/10/2020 21:00, the MACD histogram value is -0.00091 which is similar to the custom indicator that i have wrote. Then if you change the timeframe down to 4H, the custom indicator histogram value is still -0.00091.
Please see the attached image below:
If the on screen value is incorrect, then it means that the built in MACD Crossover indicator has bug?
Best regards,
Loh
@lohzi97
Anka Software
11 Aug 2021, 04:20
RE:
PanagiotisCharalampous said:
Hi lohzi97,
This seems to be a bug. I reported this to the product team and will be fixed in a future update. The correct values are the ones printed by the cBot.
Best Regards,
Panagiotis
Hi Panagiotis,
Is this issue fixed. I have a similar problem with ATR indicator. Can provide sample code and screenshots.
Regards
Vivek
@Anka Software
PanagiotisCharalampous
11 Aug 2021, 10:07
Hi Vivek,
This will be fixed in 4.2.
Best Regards,
Panagiotis
Join us on Telegram and Facebook
@PanagiotisCharalampous
Anka Software
11 Aug 2021, 11:23
RE:
PanagiotisCharalampous said:
Hi Vivek,
This will be fixed in 4.2.
Best Regards,
Panagiotis
Hi Panagiotis,
What is the ETA for vs 4.2.
Regards
Vivek
@Anka Software
PanagiotisCharalampous
11 Aug 2021, 11:30
Hi Vivek,
No ETA yet, sometime in autumn.
Best Regards,
Panagiotis
Join us on Telegram and Facebook
@PanagiotisCharalampous
PanagiotisCharalampous
21 Oct 2020, 14:12
Hi lohzi97,
This seems to be a bug. I reported this to the product team and will be fixed in a future update. The correct values are the ones printed by the cBot.
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous