Category Trend  Published on 18/08/2023

Trend Continuation Factor indicator

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

The Trend Continuation Factor (TCF) indicator was developed to assist in identifying the market's trend and direction. It was introduced by M.H. Pee in an article titled 'Trend Continuation Factor' in the Stocks & Commodities magazine.

One common strategy for using this indicator involves seeking confluence across four periods (e.g., 20, 50, 100, 200).




mfejza's avatar
mfejza

Joined on 25.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: mTrendContinuationFactor.algo
  • Rating: 5
  • Installs: 441
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 TCF : Indicator
    {
        [Parameter("ROC Period (1)", DefaultValue = 1)]
        public int inpPeriodsROC { get; set; }
        [Parameter("Smooth Period (20)", DefaultValue = 20)]
        public int inpPeriodsSmooth { get; set; }
        [Parameter("Data Source (close)", DefaultValue = enumPriceTypes.Close)]
        public enumPriceTypes inpPriceType { get; set; } 

        [Output("TCF Bulls", LineColor = "Green", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outTCFbulls { get; set; }
        [Output("TCF Bears", LineColor = "Red", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outTCFbears { get; set; }
        
        private IndicatorDataSeries _price, _cp, _roc, _PC, _NC, _PCF, _NCF, _bulls, _bears;
        private MovingAverage _smoothpc, _smoothnc, _smoothpcf, _smoothncf;
        

        protected override void Initialize()
        {
            _price = CreateDataSeries();
            _cp = CreateDataSeries();
            _roc = CreateDataSeries();
            _PC = CreateDataSeries();
            _NC = CreateDataSeries();
            _PCF = CreateDataSeries();
            _NCF = CreateDataSeries();
            _bulls = CreateDataSeries();
            _bears = CreateDataSeries();
            _smoothpc = Indicators.MovingAverage(_PC, inpPeriodsSmooth, MovingAverageType.Simple);
            _smoothnc = Indicators.MovingAverage(_NC, inpPeriodsSmooth, MovingAverageType.Simple);
            _smoothpcf = Indicators.MovingAverage(_PCF, inpPeriodsSmooth, MovingAverageType.Simple);
            _smoothncf = Indicators.MovingAverage(_NCF, inpPeriodsSmooth, MovingAverageType.Simple);
        }

        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;
            }
            _cp[i] = i>inpPeriodsROC ? _price[i-inpPeriodsROC] : (Bars.TypicalPrices[i] + Bars.WeightedPrices[i]) / 2;
            _roc[i] = _cp[i] != 0 ? 100.0 * (_price[i] - _cp[i]) / _cp[i] : 0;
            _PC[i] = _roc[i] > 0 ? _roc[i] : 0;
            _NC[i] = _roc[i] > 0 ? 0 : -_roc[i];
            _PCF[i] = _roc[i] > 0 ? (i>1 ? _PCF[i-1] : 0) + _roc[i] : 0;
            _NCF[i] = _roc[i] > 0 ? 0 : (i>1 ? _NCF[i-1] : 0) - _roc[i];
            _bulls[i] = (_smoothpc.Result[i] + inpPeriodsSmooth) - (_smoothncf.Result[i] + inpPeriodsSmooth);
            _bears[i] = (_smoothnc.Result[i] + inpPeriodsSmooth) - (_smoothpcf.Result[i] + inpPeriodsSmooth);

            outTCFbulls[i] = _bulls[i];
            outTCFbears[i] = _bears[i];
        }
    }
    
    public enum enumPriceTypes
    {
        Open,
        Close,
        High,
        Low,
        Median,
        Typical,
        Weighted
    }
}