Category Trend  Published on 17/08/2023

John Ehlers Reflex indicator

An update for this algorithm is currently pending moderation. Please revisit this page shortly to access the algorithm's latest version.
Description

John Ehlers indicator "Reflex: A New Zero-Lag Indicator" - February 2020. 

In "Reflex: A New Zero-Lag Indicator," author John Ehlers introduces a groundbreaking averaging indicator meticulously crafted to minimize lag. This indicator represents a significant leap forward in reducing time delays inherent in traditional calculations. According to Ehlers, this innovative indicator holds the capability to produce signals with greater timeliness compared to conventional lagging computations. 




mfejza's avatar
mfejza

Joined on 25.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: mEhlersReflex.algo
  • Rating: 0
  • Installs: 453
  • Modified: 17/08/2023 11:43
Comments
Log in to add a comment.
mfejza's avatar
mfejza · 1 year ago
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
    [Levels(0)]
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class mEhlersReflex : Indicator
    {
        [Parameter("Period (20)", DefaultValue = 20)]
        public int inpPeriod { get; set; }
        [Parameter("Data Source (close)", DefaultValue = enumPriceTypes.Close)]
        public enumPriceTypes inpPriceType { get; set; }  

        [Output("Reflex", LineColor = "Black", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outReflex { get; set; }
        
        private double _a1, _b1, _c1, _c2, _c3;
        private IndicatorDataSeries _price, _filt, _slope, _sum, _raw, _ms, _reflex;

        protected override void Initialize()
        {
            _a1 = Math.Exp(-1.414 * 3.14159 / (0.5 * inpPeriod));
            _b1 = 2 * _a1 * Math.Cos(1.414 * 180 / (0.5 * inpPeriod));
            _c3 = -_a1 * _a1 ;
            _c2 = _b1 ;
            _c1 = 1 - _c2 - _c3 ;
            _price = CreateDataSeries();
            _filt = CreateDataSeries();
            _slope = CreateDataSeries();
            _sum = CreateDataSeries();
            _raw = CreateDataSeries();
            _ms = CreateDataSeries();
            _reflex = CreateDataSeries();
        }

        public override void Calculate(int i)
        {
            switch(inpPriceType)
            {
                case enumPriceTypes.Open:
                    _price[i] = Bars.OpenPrices[i];
                    break;
                case enumPriceTypes.Close:
                    _price[i] = Bars.ClosePrices[i];
                    break;
                case enumPriceTypes.High:
                    _price[i] = Bars.HighPrices[i];
                    break;
                case enumPriceTypes.Low:
                    _price[i] = Bars.LowPrices[i];
                    break;
                case enumPriceTypes.Median:
                    _price[i] = Bars.MedianPrices[i];
                    break;
                case enumPriceTypes.Typical:
                    _price[i] = Bars.TypicalPrices[i];
                    break;
                case enumPriceTypes.Weighted:
                    _price[i] = Bars.WeightedPrices[i];
                    break;
                default:
                    _price[i] = Bars.ClosePrices[i];
                    break;
            }
            
            _filt[i] = _c1 * (_price[i] + _price[i-1]) / 2 + _c2 * (i>1 ? _filt[i-1] : 0) + _c3 * (i>2 ? _filt[i-2] : 0);
            _slope[i] = (_filt[i-inpPeriod] - _filt[i] ) / inpPeriod;
            _sum[i] = 0;
            for (int j=1; j<inpPeriod; j++)
                _sum[i] += (_filt[i] + j * _slope[i]) - (i>inpPeriod ? _filt[i-j] : 0);
            _raw[i] = _sum[i] / inpPeriod;
            _ms[i] = 0.04 * _raw[i] * _raw[i] + (i>1 ? 0.96 * (!double.IsNaN(_ms[i-1]) ? _ms[i-1] : 1) : 0);
            _reflex[i] = _ms[i] != 0 ? _raw[i] / Math.Sqrt(_ms[i]) : 0;

            outReflex[i] = _reflex[i];
        }
    }
    
    public enum enumPriceTypes
    {
        Open,
        Close,
        High,
        Low,
        Median,
        Typical,
        Weighted
    }
}