Category Trend  Published on 17/08/2023

John Ehlers Trendflex 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 Trendflex - September 2021. 

In Trendflex 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.

A straightforward strategy involves employing the indicator across two distinct periods: 20 and 50. This approach effectively identifies momentum while mitigating the impact of lag.




mfejza's avatar
mfejza

Joined on 25.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: mEhlersTrendflex.algo
  • Rating: 5
  • Installs: 357
Comments
Log in to add a comment.
mfejza's avatar
mfejza · 10 months 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 mEhlersTrendflex : Indicator
    {
        [Parameter("Period (20)", DefaultValue = 20)]
        public int inpPeriod { get; set; }
        [Parameter("Data Source (close)", DefaultValue = enumPriceTypes.Close)]
        public enumPriceTypes inpPriceType { get; set; }  

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

        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();
            _sum = CreateDataSeries();
            _raw = CreateDataSeries();
            _ms = CreateDataSeries();
            _trendflex = 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);
            _sum[i] = 0;
            for (int j=1; j<inpPeriod; j++)
                _sum[i] += _filt[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);
            _trendflex[i] = _ms[i] != 0 ? _raw[i] / Math.Sqrt(_ms[i]) : 0;

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