Category Trend  Published on 30/07/2023

Trend Score indicator

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

Trend Score indicator by Tushar S. Chande was described in the article "Rating Trend Strength" in S&C magazine (September 1993). 

It displays both the power and direction of a trend.




mfejza's avatar
mfejza

Joined on 25.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: mTrendScore.algo
  • Rating: 5
  • Installs: 585
  • Modified: 30/07/2023 09:20
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, AccessRights = AccessRights.None)]
    public class mTrendScore : Indicator
    {
        [Parameter("Period (10)", DefaultValue = 10)]
        public int inpPeriod { get; set; }
        [Parameter("OverboughtOversold value (5)", DefaultValue = 5)]
        public double inpOverboughtOversold { get; set; }
        [Parameter("Price Method (close)", DefaultValue = enumPriceMethods.OpenClose)]
        public enumPriceMethods inpPriceMethod { get; set; }
        [Parameter("Use Period (yes)", DefaultValue = true)]
        public bool inpUsePeriod { get; set; }

        [Output("TrendScore", LineColor = "Black", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outTrendScore { get; set; }
        [Output("Overbought", LineColor = "red", PlotType = PlotType.Line, LineStyle = LineStyle.Dots, Thickness = 1)]
        public IndicatorDataSeries outOverbought { get; set; }
        [Output("Oversold", LineColor = "red", PlotType = PlotType.Line, LineStyle = LineStyle.Dots, Thickness = 1)]
        public IndicatorDataSeries outOversold { get; set; }
        
        private IndicatorDataSeries _score, _sum, _trendscore, _prevprice;


        protected override void Initialize()
        {
            _score = CreateDataSeries();
            _sum = CreateDataSeries();
            _trendscore = CreateDataSeries();
            _prevprice = CreateDataSeries();
        }

        public override void Calculate(int i)
        {
            _prevprice[i] = inpPriceMethod == enumPriceMethods.OpenClose ? Bars.OpenPrices[i] : (i>1 ? Bars.ClosePrices[i-1] : Bars.TypicalPrices[i]);
            _score[i] = Bars.ClosePrices[i] > _prevprice[i] ? +1 : Bars.ClosePrices[i] < _prevprice[i] ? -1 : 0;
            if(inpUsePeriod == true)
                _trendscore[i] = _score.Sum(inpPeriod);
            else
            {
                if(Bars.ClosePrices[i] > _prevprice[i])
                    _trendscore[i] = (_trendscore[i-1] >= 0 && i>1 ? _trendscore[i-1] + 1 : 1);
                else 
                    if(Bars.ClosePrices[i] < _prevprice[i])
                        _trendscore[i] = (_trendscore[i-1] < 0 && i>1 ? _trendscore[i-1] - 1 : -1);
                    else
                        _trendscore[i] = i>1 ? _trendscore[i-1] : 0;
            }
            
            outTrendScore[i] = _trendscore[i];
            outOverbought[i+100] = inpOverboughtOversold;
            outOversold[i+100] = -inpOverboughtOversold;
        }
    }
    public enum enumPriceMethods
    {
        OpenClose,
        CloseClose
    }
}