Category Trend  Published on 20/09/2021

Trend Direction & Force Index Mod v1

Description

The TDF Index is an oscillator indicator that identifies the direction of the trend and measures its strength. It ranges between 1 and -1, oscillating above and below 0 to signal the direction of the trend. There's a filter level at 0.05 and -0.05 to identify sideways markets.

This indicator is a modification of the one coded by xabbuhttps://ctrader.com/algos/show/2616

TDFI Indicator

Indicator Parameters


using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TDFIMODv1 : Indicator
    {
        [Parameter("Lookback", DefaultValue = 13, MinValue = 2, MaxValue = 30, Step = 1)]
        public int lookback { get; set; }

        [Parameter("MMA Length", DefaultValue = 13, MinValue = 2, MaxValue = 30, Step = 1)]
        public int mmaLength { get; set; }

        [Parameter("SMMA Length", DefaultValue = 13, MinValue = 2, MaxValue = 30, Step = 1)]
        public int smmaLength { get; set; }

        [Parameter("SMMA Mode", DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType maType { get; set; }

        [Parameter("N Length", DefaultValue = 3, MinValue = 1, MaxValue = 30, Step = 2)]
        public int nLength { get; set; }

        [Parameter("Filter Distance", DefaultValue = 0.05, MinValue = 0, MaxValue = 0.5, Step = 0.005)]
        public double filterHigh { get; set; }

        [Parameter("Arrows", DefaultValue = true)]
        public bool arrows { get; set; }

        [Parameter("Arrow Distance", DefaultValue = 1)]
        public double arrowDistance { get; set; }

        private MovingAverage mma;
        private MovingAverage smma;
        private AverageTrueRange atr;
        private double impetmma;
        private double impetsmma;
        private double divma;
        private double averimpet;

        private IndicatorDataSeries tdf;
        private IndicatorDataSeries tdfabs;
        private IndicatorDataSeries result;

        [Output("Highfilter", LineColor = "FFC0C0C0", LineStyle = LineStyle.DotsRare, Thickness = 1)]
        public IndicatorDataSeries highFilter { get; set; }

        [Output("Lowfilter", LineColor = "FFC0C0C0", LineStyle = LineStyle.DotsRare, Thickness = 1)]
        public IndicatorDataSeries lowFilter { get; set; }

        [Output("Up Histogram 1", LineColor = "FF008080", PlotType = PlotType.Histogram, Thickness = 4)]
        public IndicatorDataSeries Up1 { get; set; }

        [Output("Up Histogram 2", LineColor = "FF77B0B2", PlotType = PlotType.Histogram, Thickness = 4)]
        public IndicatorDataSeries Up2 { get; set; }

        [Output("Down Histogram 1", LineColor = "FFA52A2A", PlotType = PlotType.Histogram, Thickness = 4)]
        public IndicatorDataSeries Down1 { get; set; }

        [Output("Down Histogram 2", LineColor = "FFC4898A", PlotType = PlotType.Histogram, Thickness = 4)]
        public IndicatorDataSeries Down2 { get; set; }

        [Output("Flat Histogram", LineColor = "FFC0C0C0", PlotType = PlotType.Histogram, Thickness = 4)]
        public IndicatorDataSeries Flat { get; set; }

        protected override void Initialize()
        {
            mma = Indicators.MovingAverage(Bars.ClosePrices, mmaLength, maType);
            smma = Indicators.MovingAverage(mma.Result, smmaLength, maType);
            atr = Indicators.AverageTrueRange(27, MovingAverageType.Simple);

            tdf = CreateDataSeries();
            tdfabs = CreateDataSeries();
            result = CreateDataSeries();
        }

        public override void Calculate(int index)
        {
            impetmma = mma.Result.Last(0) - mma.Result.Last(1);
            impetsmma = smma.Result.Last(0) - smma.Result.Last(1);
            divma = Math.Abs(mma.Result.Last(0) - smma.Result.Last(0));
            averimpet = (impetmma + impetsmma) / 2;
            tdf[index] = (Math.Pow(divma, 1) * Math.Pow(averimpet, nLength));
            tdfabs[index] = Math.Abs(tdf[index]);
            result[index] = tdf[index] / tdfabs.Maximum(lookback * nLength);
            if (result[index] > filterHigh)
            {
                if (result[index] >= result[index - 1])
                {
                    Up1[index] = result[index];
                    Up2[index] = double.NaN;
                }
                else
                {
                    Up2[index] = result[index];
                    Up1[index] = double.NaN;
                }
                Down1[index] = double.NaN;
                Down2[index] = double.NaN;
                Flat[index] = double.NaN;
            }
            else if (result[index] < filterHigh * -1)
            {
                if (result[index] <= result[index - 1])
                {
                    Down1[index] = result[index];
                    Down2[index] = double.NaN;
                }
                else
                {
                    Down2[index] = result[index];
                    Down1[index] = double.NaN;
                }
                Up1[index] = double.NaN;
                Up2[index] = double.NaN;
                Flat[index] = double.NaN;
            }
            else
            {
                Flat[index] = result[index];
                Up1[index] = double.NaN;
                Up2[index] = double.NaN;
                Down1[index] = double.NaN;
                Down2[index] = double.NaN;
            }

            highFilter[index] = filterHigh;
            lowFilter[index] = filterHigh * -1;

            // Arrows
            if (arrows)
            {
                if (result[index] > filterHigh && result[index - 1] < filterHigh)
                {
                    Chart.DrawIcon("TDFIup" + Bars.OpenTimes.Last(0).ToString(), ChartIconType.UpArrow, Bars.OpenTimes.Last(0), Bars.LowPrices.Last(0) - (arrowDistance * atr.Result.Last(1)), "Teal");
                }
                else
                {
                    Chart.RemoveObject("TDFIup" + Bars.OpenTimes.Last(0).ToString());
                }
                if (result[index] < filterHigh * -1 && result[index - 1] > filterHigh * -1)
                {
                    Chart.DrawIcon("TDFIdown" + Bars.OpenTimes.Last(0).ToString(), ChartIconType.DownArrow, Bars.OpenTimes.Last(0), Bars.HighPrices.Last(0) + (arrowDistance * atr.Result.Last(1)), "Brown");
                }
                else
                {
                    Chart.RemoveObject("TDFIdown" + Bars.OpenTimes.Last(0).ToString());
                }
            }
        }
    }
}







danieljclsilva's avatar
danieljclsilva

Joined on 15.03.2019

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: TDFI MOD v1.algo
  • Rating: 5
  • Installs: 2225
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
IA
IandelMar · 1 year ago

Great Indicator, makes my Seteup complete. in Renko perfect :-)

JU
juliansega · 1 year ago

Muchas gracias Daniel x este gran indicador.