Cross timeframe indicator value is different in backtesting and on screen.

Created at 20 Oct 2020, 20:58
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!
LO

lohzi97

Joined 29.10.2019

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. 

Cross Timeframe MACD Custom Indicator on screen value
Cross Timeframe MACD Custom Indicator on screen value
Cross Timeframe MACD Custom Indicator value print during backtesting
Cross Timeframe MACD Custom Indicator value print during backtesting

 

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;
        }
    }
}

 


@lohzi97
Replies

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

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 

Join us on Telegram

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:

Built in MACD Crossover (Daily)
Built in MACD Crossover (Daily)
Custom Indicator value (Daily)
Custom Indicator value (Daily)
Custom Indicator value (4H)
Custom Indicator value (4H)

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 

Join us on Telegram

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 

Join us on Telegram and Facebook

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