ATR Multi TimeFrame Issue

Created at 22 Jan 2019, 04:38
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!
CY

cysecsbin.01

Joined 10.11.2018 Blocked

ATR Multi TimeFrame Issue
22 Jan 2019, 04:38


Good Evening, I was trying to create a multi timeframe atr but i encountered some difficulties:

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 ATRMTF : Indicator
    {
        [Parameter()]
        public int per { get; set; }

        [Parameter()]
        public TimeFrame tf { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        public MarketSeries series;
        public IndicatorDataSeries tr, ema;

        protected override void Initialize()
        {
            tr = CreateDataSeries();
            ema = Indicators.ExponentialMovingAverage(tr, per);
            series = MarketData.GetSeries(tf);
        }

        public override void Calculate(int index)
        {
            var index1 = series.OpenTime.GetIndexByExactTime(MarketSeries.OpenTime[index]);
            tr[index1] = series.High[index1] - series.Low[index1];
            Result[index] = ema.Result[index1];
        }
    }
}

i tought this was the right piece of code to do it, but when i change timeframe it only produces nan values.

is there a way to do it? i would really appreciate it since a lot of my algos use mtf ATR and I'm in a little bit of trouble with this.

Thanks in advance


Replies

PanagiotisCharalampous
22 Jan 2019, 10:17

Hi cysecsbin.01,

Here it is 

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 ATRMTF : Indicator
    {
        [Parameter()]
        public int per { get; set; }

        [Parameter()]
        public TimeFrame tf { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        AverageTrueRange atr;
        MarketSeries series;
        protected override void Initialize()
        {
            series = MarketData.GetSeries(tf);
            atr = Indicators.AverageTrueRange(series, per, MovingAverageType.Exponential);
        }

        public override void Calculate(int index)
        {
            var index1 = series.OpenTime.GetIndexByExactTime(MarketSeries.OpenTime[index]);
            if (Result[index - 1] != atr.Result[index1])
                Result[index] = atr.Result[index1];
        }
    }
}

Best Regards,

Panagiotis


@PanagiotisCharalampous