MovingAverage of another two moving average results

Created at 03 Mar 2023, 04:46
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!
YS

ys2310

Joined 03.12.2021

MovingAverage of another two moving average results
03 Mar 2023, 04:46


I'd like to calculate a moving average of the difference of two other moving averages' results like below. 

            _emaFast = Indicators.MovingAverage(MarketSeries.Close, 10, MovingAverageType.Exponential);
            _emaSlow = Indicators.MovingAverage(MarketSeries.Close, 20, MovingAverageType.Exponential);            
            _emaSignal = Indicators.MovingAverage(_emaFast.Result - _emaSlow.Result, 9, MovingAverageType.Exponential);

However, this code doesn't compile with a "Operator '-' cannot be applied to operands of type 'IndicatorDataSeries' and 'IndicatorDataSeries'" error.

How can I get the _emaSignal correctly in here?

Thank you very much.


@ys2310
Replies

PanagiotisChar
03 Mar 2023, 09:05

Hi there,

Here is an example

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

namespace cAlgo
{
    [Indicator(AccessRights = AccessRights.None)]
    public class NewIndicator7 : Indicator
    {
        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        public IndicatorDataSeries Diff;

        private MovingAverage _emaFast;
        private MovingAverage _emaSlow;
        private MovingAverage _emaDiff;

        protected override void Initialize()
        {
            _emaFast = Indicators.MovingAverage(Bars.ClosePrices, 10, MovingAverageType.Exponential);
            _emaSlow = Indicators.MovingAverage(Bars.ClosePrices, 20, MovingAverageType.Exponential);
            Diff = CreateDataSeries();
            _emaDiff = Indicators.MovingAverage(Diff, 20, MovingAverageType.Exponential);
        }

        public override void Calculate(int index)
        {
            // Calculate value at specified index
            Diff[index] = _emaFast.Result[index] - _emaSlow.Result[index];
            Result[index] = _emaDiff.Result[index];
        }
    }
}

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 


@PanagiotisChar

ys2310
03 Mar 2023, 10:14

Hello Panagiotis,

Thank you for the indicator! But when I tried use it from cBot like below, I got a run-time error: 

26/10/2020 07:00:00.210 | Crashed in OnStart with NullReferenceException: Object reference not set to an instance of an object.

26/10/2020 07:00:00.210 | CBot instance [Test, XAUUSD, m1] crashed with error "Crashed in OnStart with NullReferenceException: Object reference not set to an instance of an object."

---

 private MovingAverage _emaSignal;
 private NewIndicator7 _emaDiff;

 protected override void OnStart()
 {
      _emaSignal = Indicators.MovingAverage(_emaDiff.Result, 9, MovingAverageType.Exponential);
 }

---

What might be wrong in here?

Thank you gain in advance.


@ys2310

PanagiotisChar
03 Mar 2023, 11:14

Hi there,

You need to initialize the _emaDiff indicator. Also _emaSignal is not necessary. The result is already contained in _emaDiff.Result.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us


@PanagiotisChar