shift multi time frame indicator

Created at 09 Jul 2021, 11:34
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!
IR

IRCtrader

Joined 17.06.2021

shift multi time frame indicator
09 Jul 2021, 11:34


i want when i change the shift , indicator shift bast time frame not current time frame i used.

for ex: when i add daily moving in m1, when i change the shift, indicator shift base m1 not  daily

 

//Mahdi khoshroo make this indicator for AcademyWave


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 AWEMA : Indicator
    {
        private ExponentialMovingAverage _moving;

        private Bars _baseTimeFrameBars;
        // you could change default Value to Minute,Minute15,Hour2 and ...
        [Parameter("Base Timeframe", DefaultValue = "Daily")]
        public TimeFrame BaseTimeFrame { get; set; }

        [Parameter("Applied price", DefaultValue = Price.High)]
        public Price Source { get; set; }

        [Parameter("Shift:", DefaultValue = 0)]
        public int Shift { get; set; }

        [Parameter("Period", DefaultValue = 1)]
        public int Period { get; set; }



        [Output(" Moving,", LineColor = "Red", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries Moving_Line { get; set; }



        protected override void Initialize()
        {
            _baseTimeFrameBars = MarketData.GetBars(BaseTimeFrame);

            var baseSeries = GetBaseSeries();

            _moving = Indicators.ExponentialMovingAverage(baseSeries, Period);
        }

        public override void Calculate(int index)
        {
            if (Shift < 0 && index < Math.Abs(Shift))
                return;
            var baseSeriesIndex = _baseTimeFrameBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
            Moving_Line[index + Shift] = _moving.Result[baseSeriesIndex];

        }

        private DataSeries GetBaseSeries()
        {
            switch (Source)
            {
                case Price.Open:
                    return _baseTimeFrameBars.OpenPrices;

                case Price.Close:
                    return _baseTimeFrameBars.ClosePrices;

                case Price.High:
                    return _baseTimeFrameBars.HighPrices;

                case Price.Low:
                    return _baseTimeFrameBars.LowPrices;

                case Price.Median:
                    return _baseTimeFrameBars.MedianPrices;

                case Price.Typical:
                    return _baseTimeFrameBars.TypicalPrices;

                case Price.Weighted:
                    return _baseTimeFrameBars.WeightedPrices;
                default:

                    return _baseTimeFrameBars.ClosePrices;
            }
        }
    }

    public enum Price
    {
        Open,
        Close,
        High,
        Low,
        Median,
        Typical,
        Weighted
    }
}

 


@IRCtrader
Replies

amusleh
09 Jul 2021, 16:10

Hi,

You have to shift the index of base time frame:

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 AWEMA : Indicator
    {
        private ExponentialMovingAverage _moving;

        private Bars _baseTimeFrameBars;
        // you could change default Value to Minute,Minute15,Hour2 and ...
        [Parameter("Base Timeframe", DefaultValue = "Daily")]
        public TimeFrame BaseTimeFrame { get; set; }

        [Parameter("Applied price", DefaultValue = Price.High)]
        public Price Source { get; set; }

        [Parameter("Shift:", DefaultValue = 0)]
        public int Shift { get; set; }

        [Parameter("Period", DefaultValue = 1)]
        public int Period { get; set; }



        [Output(" Moving,", LineColor = "Red", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries Moving_Line { get; set; }



        protected override void Initialize()
        {
            _baseTimeFrameBars = MarketData.GetBars(BaseTimeFrame);

            var baseSeries = GetBaseSeries();

            _moving = Indicators.ExponentialMovingAverage(baseSeries, Period);
        }

        public override void Calculate(int index)
        {
            if (Shift < 0 && index < Math.Abs(Shift))
                return;

            var baseSeriesIndex = _baseTimeFrameBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]) - Shift;

            Moving_Line[index] = _moving.Result[baseSeriesIndex];

        }

        private DataSeries GetBaseSeries()
        {
            switch (Source)
            {
                case Price.Open:
                    return _baseTimeFrameBars.OpenPrices;

                case Price.Close:
                    return _baseTimeFrameBars.ClosePrices;

                case Price.High:
                    return _baseTimeFrameBars.HighPrices;

                case Price.Low:
                    return _baseTimeFrameBars.LowPrices;

                case Price.Median:
                    return _baseTimeFrameBars.MedianPrices;

                case Price.Typical:
                    return _baseTimeFrameBars.TypicalPrices;

                case Price.Weighted:
                    return _baseTimeFrameBars.WeightedPrices;
                default:

                    return _baseTimeFrameBars.ClosePrices;
            }
        }
    }

    public enum Price
    {
        Open,
        Close,
        High,
        Low,
        Median,
        Typical,
        Weighted
    }
}

 


@amusleh