How to apply SMA to MACD signal, instead of EMA

Created at 30 Jul 2016, 08:27
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!
Singleton's avatar

Singleton

Joined 13.04.2015

How to apply SMA to MACD signal, instead of EMA
30 Jul 2016, 08:27


Hello

As is well known, MACD employs EMA for both long and short cycle and signal.

 

I want to change it a little bit by utilizing SMA for signal, instead of EMA, with other variables, long and short cycle using EMA.

In short signal = SimpleMovingAverage(MACD, period)

 

I tried to make a indicator that suits this, but failled.

 

Is there anyone who can help me? I do not think it is so hard a job.

 

----------------- below is what I created in attempt, which ends up errors for reasons I do not know------------

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

namespace cAlgo.Indicators
{
    [Indicator(ScalePrecision = 5, AccessRights = AccessRights.None)]
    public class OSMAMACD : Indicator
    {
        private MovingAverage longperiod;
        private MovingAverage shortperiod;
        private MovingAverage signal;

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

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

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

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

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

        protected override void Initialize()
        {
            longperiod = Indicators.MovingAverage(MarketSeries.Close, LongCycle, MovingAverageType.Simple);
            shortperiod = Indicators.MovingAverage(MarketSeries.Close, MovingAverageType.Simple);

            signal = Indicators.SimpleMovingAverage(MACD, SignalPeriods);
        }

        public override void Calculate(int index)
        {
            MACD[index] = shortperiod[index] - longperiod[index];

            Signal[index] = _signal.Result[index];
        }
    }
}


@Singleton
Replies

hedgehog
30 Jul 2016, 11:54

RE:

Here you go

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

namespace cAlgo.Indicators
{
    [Indicator(ScalePrecision = 5, AccessRights = AccessRights.None)]
    public class OSMAMACD : Indicator
    {
        private MovingAverage longperiod;
        private MovingAverage shortperiod;
        private MovingAverage signal;

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

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

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

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

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

        protected override void Initialize()
        {
            longperiod = Indicators.MovingAverage(MarketSeries.Close, LongCycle, MovingAverageType.Simple);
            shortperiod = Indicators.MovingAverage(MarketSeries.Close, ShortCycle, MovingAverageType.Simple);

            signal = Indicators.SimpleMovingAverage(MACD, SignalPeriods);
        }

        public override void Calculate(int index)
        {
            MACD[index] = shortperiod.Result[index] - longperiod.Result[index];

            Signal[index] = signal.Result[index];
        }
    }
}

 


@hedgehog

Singleton
30 Jul 2016, 14:25

RE: RE:

Thank you so much

 

 

msforex said:

Here you go

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

namespace cAlgo.Indicators
{
    [Indicator(ScalePrecision = 5, AccessRights = AccessRights.None)]
    public class OSMAMACD : Indicator
    {
        private MovingAverage longperiod;
        private MovingAverage shortperiod;
        private MovingAverage signal;

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

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

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

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

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

        protected override void Initialize()
        {
            longperiod = Indicators.MovingAverage(MarketSeries.Close, LongCycle, MovingAverageType.Simple);
            shortperiod = Indicators.MovingAverage(MarketSeries.Close, ShortCycle, MovingAverageType.Simple);

            signal = Indicators.SimpleMovingAverage(MACD, SignalPeriods);
        }

        public override void Calculate(int index)
        {
            MACD[index] = shortperiod.Result[index] - longperiod.Result[index];

            Signal[index] = signal.Result[index];
        }
    }
}

 

 


@Singleton

hedgehog
30 Jul 2016, 14:40

RE: RE: RE:

You are welcome


@hedgehog