Inconsistent data between robot and indicator

Created at 10 Nov 2019, 09:28
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!
douglascvas's avatar

douglascvas

Joined 02.01.2018

Inconsistent data between robot and indicator
10 Nov 2019, 09:28


Why is the data used by a Robot different from the one used by an Indicator?

 

Version: 3.5

Indicator:

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TestData : Indicator
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        private int lastIndex;
        private MarketSeries targetSeries;
        private string screenText;

        protected override void Initialize()
        {
            lastIndex = 0;
            targetSeries = MarketData.GetSeries(TimeFrame.Hour4);
            screenText = "";
        }


        public override void Calculate(int index)
        {
            if (lastIndex == index)
                return;
            lastIndex = index;

            int targetIndex = targetSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index]);

            string text = string.Format("{0}: {1} - {2}: {3}", MarketSeries.OpenTime[index], MarketSeries.Close[index], targetSeries.OpenTime[targetIndex], targetSeries.Close[targetIndex]);
            screenText = text + "\n" + screenText; 
            Chart.DrawStaticText("test", screenText, VerticalAlignment.Top, HorizontalAlignment.Right, Color.Lime);
        }
    }
}

 

Robot:

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 TestData : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        private MarketSeries targetSeries;
        
        protected override void OnStart()
        {
            targetSeries = MarketData.GetSeries(TimeFrame.Hour4);
        }

        protected override void OnBar()
        {
            int index = MarketSeries.Close.Count - 1;
            int targetIndex = targetSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index]);

            string text = string.Format("{0}: {1} - {2}: {3}", MarketSeries.OpenTime[index], MarketSeries.Close[index], targetSeries.OpenTime[targetIndex], targetSeries.Close[targetIndex]);
            Print(text);
        }

    }
}

 

Result:


@douglascvas
Replies

srubtsov
11 Nov 2019, 14:24

At first look, in the cBot you call it only once at the beginning of a bar. So you have the close value with which bar started. The indicator takes completed bars in that case.


@srubtsov

douglascvas
16 Nov 2019, 01:08

RE:

srubtsov said:

At first look, in the cBot you call it only once at the beginning of a bar. So you have the close value with which bar started. The indicator takes completed bars in that case.

 

You mean "MarketSeries.Close.Count - 2"? The same happens.


@douglascvas

srubtsov
19 Nov 2019, 14:58

RE: RE:

douglascvas said:

You mean "MarketSeries.Close.Count - 2"? The same happens.

What exactly do you mean? H1 bars are ok, but H4 bars aren't ok, right? There is the same reason. The cBot with previous last index uses completed H1 bars and non-completed H4 bars. But indicator on backtesting chart uses completed H1 and H4 bars. An indicator referenced in a cBot should work properly.


@srubtsov