Is index continuous?

Created at 04 Nov 2019, 22:36
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

Is index continuous?
04 Nov 2019, 22:36


Is the index passed to the "Calculate" method continuous from the last call?

I'm trying to do my own Simple Moving Average, but a fast one that does not need to re-iterate over the N bars again and again, something like this:

 

 [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class FastSMA : Indicator
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter(DefaultValue = 14)]
        public int period { get; set; }

        [Output("Main", Color = Colors.Turquoise)]
        public IndicatorDataSeries Result { get; set; }

        public int count;
        public double totalSum;

        public int lastIndex;

        protected override void Initialize()
        {
            count = 0;
            totalSum = 0;
            lastIndex = 0;
        }

        public override void Calculate(int index)
        {
            if (index > 0 && lastIndex + 1 != index)
                Print("[ERROR] Not a sequential index");
            lastIndex = index;

            if (count >= period)
            {
                double first = Source[index - period];
                totalSum = totalSum - first + Source[index];
                Result[index] = totalSum / period;
            }
            else
            {
                totalSum += Source[index];
            }
            count++;
        }
    }

 

When visualising the chart it works great. If I use the Indicator in a Robot: in backtesting seems to work fine, but in "Optimisation" it prints 

[ERROR] Not a sequential index

thousands of times. Why is that so?


@douglascvas
Replies

douglascvas
04 Nov 2019, 23:24

Ah I think it has just been answered in https://ctrader.com/forum/indicator-support/16617


@douglascvas