Is index continuous?

04 Nov 2019, 22:36Is index continuous?#1
douglascvasposts: 16since: 02 Jan 2018

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?

04 Nov 2019, 23:24#2
douglascvasposts: 16since: 02 Jan 2018

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