cant call custom indicator in cbot - please help

Created at 29 Aug 2019, 20:50
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!
DA

danblack9988

Joined 03.06.2019

cant call custom indicator in cbot - please help
29 Aug 2019, 20:50


Hi,

i am trying to use a custom indicator i found to work with a moving average from a different timeframe in my cbot but i cant seem to get it to work. 

I have followed the steps in the guide clicking on 'manage references' and adding my indicator.

I then call the indicator in the cbot using the code below.

 

   private MTFMA dma;

        protected override void OnStart()
        {
            dma = Indicators.GetIndicator<MTFMA>(PeriodsDMA, TimeFrame.Daily, typeDMA);
        }

        protected override void OnBar()
        {

            Print(dma.Result.Last(1));

 

the indicator code is 

 

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None, TimeZone = TimeZones.UTC)]
    public class MTFMA : Indicator
    {
        [Parameter(DefaultValue = 14)]
        public int Periods1 { get; set; }

        [Parameter("Timeframe 1", DefaultValue = "Minute15")]
        public TimeFrame MATimeframe1 { get; set; }

        [Parameter("FMA Type", DefaultValue = MovingAverageType.Weighted)]
        public MovingAverageType MAType { get; set; }

        [Output("Timeframe 1", Color = Colors.Lime, Thickness = 1)]
        public IndicatorDataSeries MA1 { get; set; }

        private MarketSeries series1;

        private MovingAverage Ma1;

        protected override void Initialize()
        {
            series1 = MarketData.GetSeries(MATimeframe1);

            Ma1 = Indicators.MovingAverage(series1.Close, Periods1, MAType);

        }

        public override void Calculate(int index)
        {

            {
                var index1 = GetIndexByDate(series1, MarketSeries.OpenTime[index]);
                if (index1 != -1)
                {
                    MA1[index] = Ma1.Result[index1];
                }
            }
        }


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

 

it just throws an error saying does not contain definition for 'Result'

Can anyone tell me what I'm doing wrong? quite new to coding so probably more than one thing im guessing....

 


@danblack9988
Replies

PanagiotisCharalampous
30 Aug 2019, 09:09

Hi danblack9988,

As the message says, there is no property called Result. Try the below

 Print(dma.MA1.Last(1));

Best Regards,

Panagiotis


@PanagiotisCharalampous

danblack9988
31 Aug 2019, 14:28

Hi

Thank you very much for that, i see how it works now. 

Unfortunately I am still not getting the result I expected when i call Print(dma.MA1.Last(1));

It returns Nan, which I have read menas not a number. I was expecting a Y axis position to be returned so that i could compare the MA level with price and other MAs. 

Can you tell me why it is returning NaN?

//CODE OF ROBOT ---------------------------

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SRBotWIP : Robot
    {

        [Parameter("Timeframe 1", DefaultValue = "Minute15")]
        public TimeFrame MATimeframe1 { get; set; }

        [Parameter("Periods DMA", DefaultValue = 14)]
        public int PeriodsDMA { get; set; }

        [Parameter("Type DMA", DefaultValue = MovingAverageType.Weighted)]
        public MovingAverageType typeDMA { get; set; }


        double dpp, dr1, dr2, dr3, ds1, ds2, ds3, wpp, wr1, wr2,
        wr3, ws1, ws2, ws3, mpp, mr1, mr2, mr3, ms1, ms2,
        ms3;

        public MTFMA dma;

        protected override void OnStart()
        {
           

            dma = Indicators.GetIndicator<MTFMA>(MATimeframe1, PeriodsDMA, typeDMA);
        }

//INDICATOR CODE ----------------

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None, TimeZone = TimeZones.UTC)]
    public class MTFMA : Indicator
    {

        [Parameter("Timeframe 1", DefaultValue = "Minute15")]
        public TimeFrame MATimeframe1 { get; set; }

        [Parameter(DefaultValue = 14)]
        public int Periods1 { get; set; }

        [Parameter("FMA Type", DefaultValue = MovingAverageType.Weighted)]
        public MovingAverageType MAType { get; set; }

        [Output("Timeframe 1", Color = Colors.Lime, Thickness = 1)]
        public IndicatorDataSeries MA1 { get; set; }

        private MarketSeries series1;

        private MovingAverage Ma1;

        protected override void Initialize()
        {
            series1 = MarketData.GetSeries(MATimeframe1);

            Ma1 = Indicators.MovingAverage(series1.Close, Periods1, MAType);
        }

        public override void Calculate(int index)
        {

            {
                var index1 = GetIndexByDate(series1, MarketSeries.OpenTime[index]);
                if (index1 != -1)
                {
                    MA1[index] = Ma1.Result[index1];
                }
            }
        }


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

 


@danblack9988

PanagiotisCharalampous
02 Sep 2019, 09:41

Hi danblack9988

Try the indicator below

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None, TimeZone = TimeZones.UTC)]
    public class MTFMA : Indicator
    {

        [Parameter("Timeframe 1", DefaultValue = "Minute15")]
        public TimeFrame MATimeframe1 { get; set; }

        [Parameter(DefaultValue = 14)]
        public int Periods1 { get; set; }

        [Parameter("FMA Type", DefaultValue = MovingAverageType.Weighted)]
        public MovingAverageType MAType { get; set; }

        [Output("Timeframe 1", Color = Colors.Lime, Thickness = 1)]
        public IndicatorDataSeries MA1 { get; set; }

        private MarketSeries series1;

        private MovingAverage Ma1;

        protected override void Initialize()
        {
            series1 = MarketData.GetSeries(MATimeframe1);

            Ma1 = Indicators.MovingAverage(series1.Close, Periods1, MAType);
        }

        public override void Calculate(int index)
        {
            var index1 = series1.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index]);
            if (index1 != -1)
            {
                MA1[index] = Ma1.Result[index1];
            }
        }
    }
}

Best Regards,

Panagiotis


@PanagiotisCharalampous

danblack9988
05 Sep 2019, 21:30

Ah thats great thank you very much!


@danblack9988

jani
28 Oct 2019, 18:20

RE:

danblack9988 said:

Hi,

i am trying to use a custom indicator i found to work with a moving average from a different timeframe in my cbot but i cant seem to get it to work. 

I have followed the steps in the guide clicking on 'manage references' and adding my indicator.

I then call the indicator in the cbot using the code below.

...

You really don't need to have MA to read other timeframes than the current frame. To have 30 period MA on 1H to read 2H 30 period values, you just set the MA period  on 1H to 60, in 4H use period 120 and so on. The result is almost the same, or I would say close enough...


@jani