Ninja Trader strategy - how to get the average size of last X bars so I can reference it later?

29 Views Asked by At

When the strategy loads I need to calculate the average size of last X bars and save it. So then I can reference this value in OnBarUpdate() method and compare the size of current bar with the average size of the last bars. Any tips would be appreciated.

I tried something like this but it doesn't seem to work:

    private double GetAverageCandleSize(int period = 50)
    {
        double[] lastXCandleSizes = new double[period];

        double currentCandleSize = Closes[0][0] - Opens[0][0];

        for (int i = 0; i < lastXCandleSizes.Length - 1; i++)
        {
            lastXCandleSizes[i] = lastXCandleSizes[i + 1];
        }

        lastXCandleSizes[lastXCandleSizes.Length - 1] = currentCandleSize;
        var average = lastXCandleSizes.Average();
       
        return average;
    }
0

There are 0 best solutions below