Calculate(), Moving Average, Nan

Created at 18 Dec 2016, 12:09
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!
MH

MHBB

Joined 30.10.2015

Calculate(), Moving Average, Nan
18 Dec 2016, 12:09


I wrote an indicator which I want to use in cAlgo, but I'm having problems

Can someone explain how calculate works? sometimes it's called once, sometimes it's several times with the same index?!

If I update the IndicatorDataSeries on every call to Calculate I can get values from a MovingAverage assigned to that IndicatorDataSeries

But getting the value for the original IndicatorDataSeries on every call doesn't match up to the data in cTrader?!

So I use 

using cAlgo.API;
 
namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewIndicator : Indicator
    {
        private int lastIndex = 0;
 
        public override void Calculate(int index)
        {
            if (index > lastIndex)
            {
                OnBarClosed(lastIndex);
                lastIndex = index;
            }
        }
 
        private void OnBarClosed(int index)
        {
            // Put your logic here
        }
    }
}

Which gives me the correct value that corresponds to cTrader

But now in cAlgo, the Moving Average that uses this IndicatorDataSeries doesn't work and all I get is Nan

What is going on?


@MHBB
Replies

MHBB
18 Dec 2016, 15:33

So I logged each time Calculate is called, and when backtesting using 1m (tick data) MarketSeries.High[index] on the dax, it gets called twice

[12/16/2016 10:41:00 AM] index:18342 / price:11387.3
[12/16/2016 10:41:00 AM]  index:18342 / price:11390.5

The second value is the one that corresponds to those shown in cTrader

So unless I set the IndicatorDataSeries like this:

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewIndicator : Indicator
    {
        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        private int lastIndex = 0;

        protected override void Initialize()
        {
        }

        public override void Calculate(int index)
        {
            Result[index] = 123; 
        }
    }
}

And not like this, which would allow me to get the value I want

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewIndicator : Indicator
    {
        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        private int lastIndex = 0;

        protected override void Initialize()
        {
        }

        public override void Calculate(int index)
        {
            if (index > lastIndex)
            {
                OnBarClosed(lastIndex);
                lastIndex = index;
            }
        }

        private void OnBarClosed(int index)
        {
            Result[index] = 123;
        }
    }
}

Then the Moving Average indicator only produces Nan

Confused...


@MHBB

whis.gg
18 Dec 2016, 17:32

Hi Simon.

Calculate(int index) is called on each price change (each tick). The method that you have written to call OnBarClosed(int index) is called only at opening of new bar (new index) and passes previous index into the method. Therefore the indicator has always unassigned last value because its waiting for the close (opening of new bar).

I suppose you are trying to access last value of the indicator, that's why you get the NaN value.


@whis.gg

MHBB
19 Dec 2016, 06:04

Thanks for the explanation tmc

Doh! am getting the values now


@MHBB