Custom MACD

Created at 16 Jan 2022, 18:45
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!
IF

iForex2015

Joined 17.03.2015

Custom MACD
16 Jan 2022, 18:45


Hello,

Below is a code for showing  MACD for custom TimeFrame.

 

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

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

        public MacdCrossOver MacdCrossOver;

        [Parameter()]

        MarketSeries series;


        [Output("Histogram Up", PlotType = PlotType.Histogram, Color = Colors.DodgerBlue)]
        public IndicatorDataSeries HistogramPositive { get; set; }

        [Output("Histogram Down", PlotType = PlotType.Histogram, Color = Colors.DodgerBlue)]
        public IndicatorDataSeries HistogramNegative { get; set; }

        [Output("MACD", Color = Colors.DodgerBlue, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries MACD { get; set; }

        [Output("Signal", Color = Colors.Red, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries Signal { get; set; }

        [Parameter("Time Frame", DefaultValue = "Hour")]
        public TimeFrame tFrame { get; set; }

        protected override void Initialize()
        {
            series = MarketData.GetSeries(tFrame);
            MacdCrossOver = Indicators.MacdCrossOver(series.Close, 26, 12, 9);

        }

        public override void Calculate(int index)
        {


            if (MacdCrossOver.Histogram[index] > 0)
            {
                HistogramPositive[index] = MacdCrossOver.Histogram[index];
            }

            else if (MacdCrossOver.Histogram[index] < 0)
            {
                HistogramNegative[index] = MacdCrossOver.Histogram[index];
            }



            Signal[index] = MacdCrossOver.Signal[index];

            MACD[index] = MacdCrossOver.MACD[index];

        }
    }
}

 

 

When I use this CustomMACD indicator with a time frame like m5 or m15 along with a default MACD indicator with a different time frame like m2, H1 or H4 , CustomMACD indicator is not aligning with present/current data. Here is an image of it.

As seen in the image, CustomMACD is showing near to 10/12/2022

This problem get worse if I scroll back for more past market data; in that case CustomMACD would be nowhere to be seen on screen.

Is there a way I can bring the CustomMACD to show near current date for example like 16/01/2022 in my case?

Thank you.

Regards
Ivan


@iForex2015
Replies

amusleh
17 Jan 2022, 09:24

Hi,

Each time frame uses different index, you can't use the current time frame bars index on another time frame.

You have to first get the other time frame bar index by using current time frame bar Open Time, try this:

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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class CustomMACD : Indicator
    {
        public MacdCrossOver _macd;

        private Bars _bars;

        [Output("Histogram Up", PlotType = PlotType.Histogram, Color = Colors.DodgerBlue)]
        public IndicatorDataSeries HistogramPositive { get; set; }

        [Output("Histogram Down", PlotType = PlotType.Histogram, Color = Colors.DodgerBlue)]
        public IndicatorDataSeries HistogramNegative { get; set; }

        [Output("MACD", Color = Colors.DodgerBlue, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries MACD { get; set; }

        [Output("Signal", Color = Colors.Red, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries Signal { get; set; }

        [Parameter("Time Frame", DefaultValue = "Hour")]
        public TimeFrame tFrame { get; set; }

        protected override void Initialize()
        {
            _bars = MarketData.GetBars(tFrame);
            _macd = Indicators.MacdCrossOver(_bars.ClosePrices, 26, 12, 9);
        }

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

            if (_macd.Histogram[seriesIndex] > 0)
            {
                HistogramPositive[index] = _macd.Histogram[seriesIndex];
            }
            else if (_macd.Histogram[seriesIndex] < 0)
            {
                HistogramNegative[index] = _macd.Histogram[seriesIndex];
            }

            Signal[index] = _macd.Signal[seriesIndex];

            MACD[index] = _macd.MACD[seriesIndex];
        }
    }
}

@amusleh

iForex2015
17 Jan 2022, 22:31

RE:

amusleh said:

Hi,

Each time frame uses different index, you can't use the current time frame bars index on another time frame.

You have to first get the other time frame bar index by using current time frame bar Open Time, try this:

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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class CustomMACD : Indicator
    {
        public MacdCrossOver _macd;

        private Bars _bars;

        [Output("Histogram Up", PlotType = PlotType.Histogram, Color = Colors.DodgerBlue)]
        public IndicatorDataSeries HistogramPositive { get; set; }

        [Output("Histogram Down", PlotType = PlotType.Histogram, Color = Colors.DodgerBlue)]
        public IndicatorDataSeries HistogramNegative { get; set; }

        [Output("MACD", Color = Colors.DodgerBlue, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries MACD { get; set; }

        [Output("Signal", Color = Colors.Red, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries Signal { get; set; }

        [Parameter("Time Frame", DefaultValue = "Hour")]
        public TimeFrame tFrame { get; set; }

        protected override void Initialize()
        {
            _bars = MarketData.GetBars(tFrame);
            _macd = Indicators.MacdCrossOver(_bars.ClosePrices, 26, 12, 9);
        }

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

            if (_macd.Histogram[seriesIndex] > 0)
            {
                HistogramPositive[index] = _macd.Histogram[seriesIndex];
            }
            else if (_macd.Histogram[seriesIndex] < 0)
            {
                HistogramNegative[index] = _macd.Histogram[seriesIndex];
            }

            Signal[index] = _macd.Signal[seriesIndex];

            MACD[index] = _macd.MACD[seriesIndex];
        }
    }
}

Hi,

Thank you for the code. It kinda works. Is there a way to show CustomMACD from the last index without stretching  or contracting?

 

Thank you
Regards
Ivan


@iForex2015

amusleh
18 Jan 2022, 09:22

RE: RE:

IGForex said:

Hi,

Thank you for the code. It kinda works. Is there a way to show CustomMACD from the last index without stretching  or contracting?

 

Thank you
Regards
Ivan

Hi,

Not sure what do you mean, it's not stretching or contracting, usually a larger time frame bar contains multiple shorter time frame bars, that's why you see the same value for multiple bars.


@amusleh

iForex2015
18 Jan 2022, 22:38

RE: RE: RE:

amusleh said:

IGForex said:

Hi,

Thank you for the code. It kinda works. Is there a way to show CustomMACD from the last index without stretching  or contracting?

 

Thank you
Regards
Ivan

Hi,

Not sure what do you mean, it's not stretching or contracting, usually a larger time frame bar contains multiple shorter time frame bars, that's why you see the same value for multiple bars.

Hi 

This is how it is showing on my system.

What I was asking was is it possible to show CustomMACD without stretching as seen in the first image I posted but without a gap between current time and CustomMACD end point. Every point of CustomMACD does not have to be aligned with the time/index, but only the end of it being aligned with last index.   

Thanks


@iForex2015

amusleh
19 Jan 2022, 07:56 ( Updated at: 21 Dec 2023, 09:22 )

RE: RE: RE: RE:

IGForex said:

Hi 

This is how it is showing on my system.

What I was asking was is it possible to show CustomMACD without stretching as seen in the first image I posted but without a gap between current time and CustomMACD end point. Every point of CustomMACD does not have to be aligned with the time/index, but only the end of it being aligned with last index.   

Thanks

Hi,

I'm not sure what you are looking for, can you make it more clear? the indicator shows the MACD indicator values for all available bars on your chart including the last bar.


@amusleh