Trouble passing indicator value to another indicator.

Created at 17 May 2020, 09:19
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!
SA

sascha.dawe

Joined 11.02.2019

Trouble passing indicator value to another indicator.
17 May 2020, 09:19


Hi,

 

I am not sure what I am doing wrong here. I am trying to pass a value from a multi symbol indicator to another indication. This will be a panel eventually, with the ability to

see different trade signals coming through on the one chart.

The primary indicator is working as it should.

But the sample panel indicator is returning NaN values.

 

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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleSignalMultiSymbol : Indicator
    {
        [Parameter("SMA Short Period ", DefaultValue = 5)]
        public int shortPeriod { get; set; }

        [Parameter("SMA Long Period ", DefaultValue = 210)]
        public int longPeriod { get; set; }
        [Parameter("Symbol 1", DefaultValue = CurrencyPairs.AUDUSD)]
        public CurrencyPairs currencyPair1 { get; set; }
        [Parameter("Time Frame ", DefaultValue = "Daily")]
        public TimeFrame timeframe { get; set; }

        public IndicatorDataSeries SMA1;
        public IndicatorDataSeries SMA2;
        public IndicatorDataSeries signal;

        public SimpleMovingAverage sma5, sma210;

        private MarketSeries customSeries;
        private Symbol symbol1;
        private int lastIndex;

        public enum CurrencyPairs
        {
            EMPTY,
            AUDUSD,
            EURUSD,
            GBPUSD,
            USDJPY,
            USDCHF,
            USDCAD,
            AUDCAD,
            AUDCHF,
            AUDNZD,
            AUDSGD,
            EURAUD,
            EURCHF,
            EURGBP,
            GBPAUD,
            GBPCHF,
            NZDUSD,
            EURJPY,
            GBPJPY,
            AUDJPY,
            CHFJPY,
            CADJPY,
            EURCAD,
            GBPCAD,
            NZDJPY,
            GBPNZD,
            EURNZD,
            CADCHF,
            NZDCAD,
            NZDCHF,
            SGDJPY,
            AUS200,
            US30,
            US500,
            US2000,
            XPTUSD,
            XAUUSD,
            XTIUSD,
            UK100,
            JPN225
        }

        protected override void Initialize()
        {
            //Symbol 1
            if (currencyPair1.ToString() != "EMPTY" && MarketData.GetSymbol(currencyPair1.ToString()) != null)
            {
                signal = CreateDataSeries();
                SMA1 = CreateDataSeries();
                SMA2 = CreateDataSeries();
                symbol1 = MarketData.GetSymbol(currencyPair1.ToString());
                customSeries = MarketData.GetSeries(symbol1, timeframe);
                sma5 = Indicators.SimpleMovingAverage(customSeries.Close, shortPeriod);
                sma210 = Indicators.SimpleMovingAverage(customSeries.Close, longPeriod);

            }
        }

        public override void Calculate(int index)
        {
            var indexUserSelectedTimeFrame = GetIndexByDate(customSeries, MarketSeries.OpenTime[index]);
            if (indexUserSelectedTimeFrame != -1)
            {
                double close = Bars.ClosePrices[indexUserSelectedTimeFrame - 1];
                double previousClose = Bars.ClosePrices[index - 2];
                SMA1[index] = sma5.Result[indexUserSelectedTimeFrame];
                SMA2[index] = sma210.Result[indexUserSelectedTimeFrame];
                if (close > SMA1[indexUserSelectedTimeFrame - 1] && previousClose < SMA1[indexUserSelectedTimeFrame - 2])
                {
                    signal[index] = 1;
                }
                else if (close < SMA1[indexUserSelectedTimeFrame - 1] && previousClose > SMA1[indexUserSelectedTimeFrame - 2])
                {
                    signal[index] = -1;
                }
                else
                {
                    signal[index] = 0;
                }
                if (index != lastIndex)
                {
                    lastIndex = index;
                    Print(signal[index]);
                }
            }
        }

        private int GetIndexByDate(MarketSeries series, DateTime time)
        {
            for (int i = series.Close.Count - 1; i > 0; i--)
            {
                if (time == series.OpenTime[i])
                {
                    return i;
                }
                else if (series.OpenTime[i] < time)
                {
                    return i;
                }
            }
            return -1;
        }

    }
}

 

This next code snippet is the panel that the first signal gets sent to.

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SamplePanel : Indicator
    {
        [Parameter("SMA Short Period ", DefaultValue = 5)]
        public int shortPeriod { get; set; }

        [Parameter("SMA Long Period ", DefaultValue = 210)]
        public int longPeriod { get; set; }
        [Parameter("Symbol 1", DefaultValue = CurrencyPairs.AUDUSD)]
        public CurrencyPairs currencyPair1 { get; set; }
        [Parameter("Time Frame ", DefaultValue = "Daily")]
        public TimeFrame timeframe { get; set; }

        public SampleSignalMultiSymbol multiSymbol;
        public IndicatorDataSeries signal;
        private Symbol symbol1;
        private MarketSeries customSeries;

        public enum CurrencyPairs
        {
            EMPTY,
            AUDUSD,
            EURUSD,
            GBPUSD,
            USDJPY,
            USDCHF,
            USDCAD,
            AUDCAD,
            AUDCHF,
            AUDNZD,
            AUDSGD,
            EURAUD,
            EURCHF,
            EURGBP,
            GBPAUD,
            GBPCHF,
            NZDUSD,
            EURJPY,
            GBPJPY,
            AUDJPY,
            CHFJPY,
            CADJPY,
            EURCAD,
            GBPCAD,
            NZDJPY,
            GBPNZD,
            EURNZD,
            CADCHF,
            NZDCAD,
            NZDCHF,
            SGDJPY,
            AUS200,
            US30,
            US500,
            US2000,
            XPTUSD,
            XAUUSD,
            XTIUSD,
            UK100,
            JPN225
        }

        protected override void Initialize()
        {
            //Symbol 1
            if (currencyPair1.ToString() != "EMPTY" && MarketData.GetSymbol(currencyPair1.ToString()) != null)
            {
                signal = CreateDataSeries();
                symbol1 = MarketData.GetSymbol(currencyPair1.ToString());
                customSeries = MarketData.GetSeries(symbol1, TimeFrame.Minute5);
                multiSymbol = Indicators.GetIndicator<SampleSignalMultiSymbol>(shortPeriod, longPeriod, currencyPair1, TimeFrame.Minute5);
            }
        }

        public override void Calculate(int index)
        {
            //Symbo1 1
            if (currencyPair1.ToString() != "EMPTY" && MarketData.GetSymbol(currencyPair1.ToString()) != null)
            {
                var indexUserSelectedTimeFrame = GetIndexByDate(customSeries, MarketSeries.OpenTime[index]);
                if (indexUserSelectedTimeFrame != -1)
                {
                    signal[index] = multiSymbol.signal[indexUserSelectedTimeFrame];
                    Print(signal[index]);
                }
            }
        }

        private int GetIndexByDate(MarketSeries series, DateTime time)
        {
            for (int i = series.Close.Count - 1; i > 0; i--)
            {
                if (time == series.OpenTime[i])
                {
                    return i;
                }
                else if (series.OpenTime[i] < time)
                {
                    return i;
                }
            }
            return -1;
        }

    }
}

 


@sascha.dawe
Replies

PanagiotisCharalampous
18 May 2020, 10:41

Hi sascha.dawe,

There seem to be a lot of issues with your code. A couple of points you should revisit

1) Try adding output attributes to your SampleSignalMultiSymbol indicator

2) Revisit your indexing. Doesn't seem to make much sense to me.

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

sascha.dawe
18 May 2020, 14:17

RE:

PanagiotisCharalampous said:

Hi sascha.dawe,

There seem to be a lot of issues with your code. A couple of points you should revisit

1) Try adding output attributes to your SampleSignalMultiSymbol indicator

2) Revisit your indexing. Doesn't seem to make much sense to me.

Best Regards,

Panagiotis 

Join us on Telegram

Thanks Panagiotis,

Is an output attribute strictly necessary though for the signal to be passed through?

I don't really want an output to the screen from this indicator. I just want various time frame signals to be passed through

to the one chart panel, where buy/sell buttons will be displayed on screen.

Regards,

Sascha

 


@sascha.dawe