Category Trend  Published on 26/03/2016

ZLEMA

Description

This is a Zero lag EMA as described by John Ehlers (without lag and overshoot). The code is available for several trading platforms but I found no version for cAlgo. So this is my first try to get this working. Feel free to improve it.

Screenshot:

 


using System;
using cAlgo.API;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class ZeroLagEMA : Indicator
    {
        [Parameter()]
        public DataSeries Source { get; set; }

        [Parameter(DefaultValue = 14, MinValue = 5)]
        public int Period { get; set; }

        [Parameter(DefaultValue = 5, MinValue = 1, MaxValue = 10)]
        public double GainLimit { get; set; }

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

        private cAlgo.API.Indicators.ExponentialMovingAverage ema;
        private double error, leastError;
        private IndicatorDataSeries EMA;

        protected override void Initialize()
        {
            base.Initialize();
            EMA = CreateDataSeries();
        }

        public override void Calculate(int index)
        {
            Period = Math.Max(Period, 1);
            leastError = 1000000;
            double Gain, BestGain = 0;
            double alpha = 2.0 / (Period + 1);
            ema = Indicators.ExponentialMovingAverage(Source, Period);
            EMA[index] = alpha * Source.LastValue + (1 - alpha) * ema.Result.Last(1);

            for (Gain = -GainLimit; Gain < GainLimit; Gain += 0.1)
            {
                if (index == 0)
                    ZLEMA[index] = Source.LastValue;
                else
                    ZLEMA[index] = alpha * (EMA[index] + Gain * (Source.LastValue - ZLEMA.Last(1))) + (1 - alpha) * ZLEMA.Last(1);

                error = Source.LastValue - ZLEMA[index];

                if (Math.Abs(error) < leastError)
                {
                    leastError = Math.Abs(error);
                    BestGain = Gain;
                }
            }
            if (index == 0)
                ZLEMA[index] = Source.LastValue;
            else
                ZLEMA[index] = alpha * (EMA[index] + BestGain * (Source.LastValue - ZLEMA.Last(1))) + (1 - alpha) * ZLEMA.Last(1);

        }
    }
}



TB
tbrbde

Joined on 26.03.2016

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Zero Lag EMA.algo
  • Rating: 0
  • Installs: 3092
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
No comments found.