Calculate(), Moving Average, Nan
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?
Replies
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
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:
And not like this, which would allow me to get the value I want
Then the Moving Average indicator only produces Nan
Confused...
@MHBB