Different timeframes for macdcrossover

Created at 18 May 2022, 21:31
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!
JM

jmmslagter

Joined 17.11.2021

Different timeframes for macdcrossover
18 May 2022, 21:31


To extract an indicator value from another timeframe, for example, the command

var tsmfast = Indicators.HullMovingAverage(MarketData.GetBars(TimeFrame.Renko5). ClosePrices, 8);

The macdcrossover has three elements (Histogram, signal and macd) What is the command to read the data of MacdCrossover from another time frame?


@jmmslagter
Replies

amusleh
19 May 2022, 09:29

Hi,

To use an indicator on a different time frame you have to first pass that time frame bars series as source to indicator and then use that time frame index values to access the indicator output values, here is an example for MACD:

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

namespace cAlgo
{
    [Indicator(AccessRights = AccessRights.None)]
    public class MACDMTF : Indicator
    {
        private MacdCrossOver _macd;
        private Bars _bars;

        [Parameter("Selected Time Frame")]
        public TimeFrame SelectedTimeFrame { get; set; }

        [Parameter("Long Cycle", DefaultValue = 26)]
        public int LongCycle { get; set; }

        [Parameter("Short Cycle", DefaultValue = 12)]
        public int ShortCycle { get; set; }

        [Parameter("Signal Periods", DefaultValue = 9)]
        public int SignalPeriods { get; set; }

        [Output("Histogram", LineColor = "Yellow", IsHistogram = true, PlotType = PlotType.Histogram, Thickness = 2)]
        public IndicatorDataSeries Histogram { get; set; }

        [Output("MACD", LineColor = "Blue", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries MACD { get; set; }

        [Output("Signal", LineColor = "Red", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries Signal { get; set; }

        protected override void Initialize()
        {
            _bars = MarketData.GetBars(SelectedTimeFrame);

            _macd = Indicators.MacdCrossOver(_bars.ClosePrices, LongCycle, ShortCycle, SignalPeriods);
        }

        public override void Calculate(int index)
        {
            var timeFrameIndex = _bars.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);

            Histogram[index] = _macd.Histogram[timeFrameIndex];
            MACD[index] = _macd.MACD[timeFrameIndex];
            Signal[index] = _macd.Signal[timeFrameIndex];
        }
    }
}

 


@amusleh

jmmslagter
19 May 2022, 10:03

RE:

amusleh said:

Hi,

To use an indicator on a different time frame you have to first pass that time frame bars series as source to indicator and then use that time frame index values to access the indicator output values, here is an example for MACD:

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

namespace cAlgo
{
    [Indicator(AccessRights = AccessRights.None)]
    public class MACDMTF : Indicator
    {
        private MacdCrossOver _macd;
        private Bars _bars;

        [Parameter("Selected Time Frame")]
        public TimeFrame SelectedTimeFrame { get; set; }

        [Parameter("Long Cycle", DefaultValue = 26)]
        public int LongCycle { get; set; }

        [Parameter("Short Cycle", DefaultValue = 12)]
        public int ShortCycle { get; set; }

        [Parameter("Signal Periods", DefaultValue = 9)]
        public int SignalPeriods { get; set; }

        [Output("Histogram", LineColor = "Yellow", IsHistogram = true, PlotType = PlotType.Histogram, Thickness = 2)]
        public IndicatorDataSeries Histogram { get; set; }

        [Output("MACD", LineColor = "Blue", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries MACD { get; set; }

        [Output("Signal", LineColor = "Red", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries Signal { get; set; }

        protected override void Initialize()
        {
            _bars = MarketData.GetBars(SelectedTimeFrame);

            _macd = Indicators.MacdCrossOver(_bars.ClosePrices, LongCycle, ShortCycle, SignalPeriods);
        }

        public override void Calculate(int index)
        {
            var timeFrameIndex = _bars.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);

            Histogram[index] = _macd.Histogram[timeFrameIndex];
            MACD[index] = _macd.MACD[timeFrameIndex];
            Signal[index] = _macd.Signal[timeFrameIndex];
        }
    }
}

 

I knew it was something like this, but could not get a solution. Thanks very much.


@jmmslagter