Help with a modified TEMA MA

Created at 14 Feb 2022, 19:19
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!
sirinath's avatar

sirinath

Joined 25.11.2021

Help with a modified TEMA MA
14 Feb 2022, 19:19


This is a follow up on: 

I made some modifications to the initial code in

using System;

using cAlgo.API;

namespace cAlgo {
  [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
  public class Ex2MovingAverage : Indicator {
    [Parameter("Source")]
    public DataSeries Source { get; set; }

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

    [Output("MA1", LineColor = "Magenta", LineStyle = LineStyle.DotsVeryRare)]
    public IndicatorDataSeries MA1 { get; set; }

    [Output("MA2", LineColor = "Cyan", LineStyle = LineStyle.DotsVeryRare)]
    public IndicatorDataSeries MA2 { get; set; }

    [Output("MA3", LineColor = "Yellow", LineStyle = LineStyle.DotsVeryRare)]
    public IndicatorDataSeries MA3 { get; set; }

    [Output("DEMA", LineColor = "Pink")]
    public IndicatorDataSeries DEMA { get; set; }

    [Output("TEMA", LineColor = "Turquoise")]
    public IndicatorDataSeries TEMA { get; set; }

    [Output("MA4", LineColor = "Orange", LineStyle = LineStyle.DotsVeryRare)]
    public IndicatorDataSeries MA4 { get; set; }

    [Output("MA5", LineColor = "Brown", LineStyle = LineStyle.DotsVeryRare)]
    public IndicatorDataSeries MA5 { get; set; }

    [Output("HMA", LineColor = "Red")]
    public IndicatorDataSeries HMA { get; set; }

    private double exp;
    private double expH;
    private double expSR;

    protected override void Initialize() {
      exp = 2.0 / (Periods + 1.0);
      expH = 2.0 / (Periods / 2.0 + 1.0);
      expSR = 2.0 / (Math.Sqrt(Periods) + 1.0);
    }

    public override void Calculate(int index) {
      double p = Source[index];

      if (index <= 0) {
        MA1[0] = p;
        MA2[0] = p;
        MA3[0] = p;
        DEMA[0] = p;
        TEMA[0] = p;

        MA4[0] = p;
        MA5[0] = p;
        HMA[0] = p;

        return;
      }

      int pi = index - 1;

      double pma1 = MA1[pi];
      double d1 = p - pma1;
      double ma1 = pma1 + exp * d1;
      MA1[index] = ma1;

      double pma2 = MA2[pi];
      double d2 = ma1 - pma2;
      double ma2 = pma2 + exp * d2;
      MA2[index] = ma2;

      double pma3 = MA3[pi];
      double d3 = ma2 - pma3;
      double ma3 = pma3 + exp * d3;
      MA3[index] = ma3;

      double dema = 2 * ma1 - ma2;
      DEMA[index] = dema;

      double tema = 3 * (ma1 - ma2) + ma3;
      TEMA[index] = tema;

      double pma4 = MA4[pi];
      double d4 = p - pma4;
      double ma4 = pma4 + expH * d4;
      MA4[index] = ma4;

      double ma5 = 2 * ma4 - ma1;
      MA5[index] = ma5;

      double phma = HMA[pi];
      double dh = ma5 - phma;
      double hma = ma5 + expSR * dh;
      HMA[index] = hma;
    }
  }
}

This is simply renaming variables and cleaning up some code. Now I don't get the MA beyond the 1st 2.

using System;

using cAlgo.API;

namespace cAlgo {
  [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
  public class ETEMAIndicator : Indicator {
    [Parameter("Source")]
    public DataSeries Source { get; set; }

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

    [Output("EMA", LineColor = "Magenta", LineStyle = LineStyle.DotsVeryRare)]
    public IndicatorDataSeries EMA { get; set; }

    [Output("EMA Of EMA", LineColor = "Cyan", LineStyle = LineStyle.DotsRare)]
    public IndicatorDataSeries EMAOfEMA { get; set; }

    [Output("EMA Of EMA Of EMA", LineColor = "Yellow", LineStyle = LineStyle.Dots)]
    public IndicatorDataSeries EMAOfEMAOfEMA { get; set; }

    [Output("TEMA", LineColor = "Red")]
    public IndicatorDataSeries TEMA { get; set; }

    private double exp;

    protected override void Initialize() {
      exp = 2.0 / (Periods + 1.0);
    }

    public override void Calculate(int index) {
      double p = Source[index];

      if (index <= 0) {
        EMA[0] = p;
        EMAOfEMA[0] = p;
        EMAOfEMAOfEMA[0] = p;
        TEMA[0] = p;

        return;
      }

      int pi = index - 1;

      double pma1 = EMA[pi];
      double pd1 = p - pma1;
      double ma1 = pma1 + exp * pd1;
      EMA[index] = ma1;

      Print("1 - {0} - {1}, {2}, {3}", index, pma1, pd1, ma1);

      double pma2 = EMAOfEMA[pi];
      double pd2 = ma1 - pma2;
      double ma2 = pma2 + exp * pd2;
      EMAOfEMA[index] = ma2;

      Print("2 - {0} - {1}, {2}, {3}", index, pma2, pd2, ma2);

      double pma3 = EMAOfEMAOfEMA[pi];
      double pd3 = ma2 - pma3;
      double ma3 = pma3 + exp * pd3;
      EMAOfEMAOfEMA[index] = ma3;

      Print("3 - {0} - {1}, {2}, {3}", index, pma3, pd3, ma3);

      double tema = 3 * (ma1 - ma2) + ma3;
      TEMA[index] = tema;
    }
  }
}

For brevity and clarity I have removed a few more lines of code than I have in my side in the above snipet.


@sirinath
Replies

amusleh
15 Feb 2022, 09:19

Hi,

I see identical results from both indicators, on second indicator you have removed some outputs.

What's the exact issue?

You can't make changes on a code without basic knowledge of the programming language or environment and expect same output.

Please learn the .NET C# basics first before starting to develop indicators/cBots.


@amusleh