Calling an indicator From an indicator, Result is NaN

Created at 26 Apr 2020, 16:55
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!
ER

ergun

Joined 26.04.2020

Calling an indicator From an indicator, Result is NaN
26 Apr 2020, 16:55


Hello,

First of all thanks for such good platform.

I am calling an indicator from another indicator as described in documentation. While Sample EMA is working mine does not. Here is Indicator i use:

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo
{
    [Indicator("Induk", IsOverlay = false, TimeZone = TimeZones.UTC, AutoRescale = false, AccessRights = AccessRights.None)]
    public class Induk : Indicator
    {
        [Parameter(DefaultValue = 55)]
        public int period { get; set; }

        [Parameter()]
        public DataSeries SourceHigh { get; set; }

        [Parameter()]
        public DataSeries SourceLow { get; set; }

        [Parameter()]
        public DataSeries SourceClose { get; set; }

        [OutputAttribute("Main1", LineColor = "Red")]
        public IndicatorDataSeries Result1 { get; set; }

        [OutputAttribute("Main2", LineColor = "Green")]
        public IndicatorDataSeries Result2 { get; set; }

        [OutputAttribute("Main3", LineColor = "White")]
        public IndicatorDataSeries Result3 { get; set; }

        private int lastCalculatedIndex = 0;

        protected override void Initialize()
        {
            //
        }

        public override void Calculate(int index)
        {
            // Yeterli bar oluncaya kadar bekle
            if (index < (period + 1))
            {
                return;
            }
            if (IsLastBar)
            {
                return;
            }

            // Do not calculate same bar again
            if (lastCalculatedIndex == index)
            {
                return;
            }

            lastCalculatedIndex = index;

            Result1[index] = 1;
            Result2[index] = 2;
            Result3[index] = 3;
        }
    }
}

And here is my second indicator which tries to use the one above:

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo
{
    [Indicator("IndukCaller", IsOverlay = false, TimeZone = TimeZones.UTC, AutoRescale = false, AccessRights = AccessRights.None)]
    public class IndukCaller : Indicator
    {
        private Induk induk;
        private int lastCalculatedIndex = 0;
        private int period = 55;

        protected override void Initialize()
        {
            induk = Indicators.GetIndicator<Induk>(period, Bars.HighPrices, Bars.LowPrices, Bars.ClosePrices);
        }

        public override void Calculate(int index)
        {
            // Yeterli bar oluncaya kadar bekle
            if (index < (period + 1))
            {
                return;
            }
            if (IsLastBar)
            {
                return;
            }

            // Do not calculate same bar again
            if (lastCalculatedIndex == index)
            {
                return;
            }

            lastCalculatedIndex = index;

            Print("Index: {0} => induk.1: {1}", index, induk.Result1[index]);
        }
    }
}

 

Manage reference mark 's marked.

Using cTrader 3.7 Desktop in Windows 10.

Here is sample output:

Best


@ergun
Replies

PanagiotisCharalampous
27 Apr 2020, 10:10

Hi ergun,

If you remove the following code, it should work fine

            if (IsLastBar)
            {
                return;
            }

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

ergun
27 Apr 2020, 11:43

RE:

Hi Panagiotis,

Thanks for the fast reply.

Just to clarify; if an indicator is called from another indicator, the called one has IsLastBar always true?

Best


@ergun

PanagiotisCharalampous
27 Apr 2020, 12:02

Hi ergun,

Yes this is correct because the referenced indicator does not know how many bars are to be calculated yet.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous