Category Oscilators  Published on 08/09/2021

Connors RSI (CRSI)

Description

This is the Connors RSI (CRSI) custom indicator for cTrader based on Automate API.

Github: spotware/ConnorsRSI: This is the Connors RSI (CRSI) custom indicator for cTrader (github.com)


// -------------------------------------------------------------------------------------------------------------------------------------------
//
//    Connors RSI (CRSI) is a technical analysis indicator created by Larry Connors that is actually a composite of three separate components.
//    This is the Connors RSI (CRSI) custom indicator for cTrader based on Automate API.
//
// -------------------------------------------------------------------------------------------------------------------------------------------

using cAlgo.API;
using cAlgo.API.Indicators;
using System.Collections.Generic;
using System.Linq;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ConnorsRSI : Indicator
    {
        private RelativeStrengthIndex _rsi;
        private RelativeStrengthIndex _upDownRsi;
        private IndicatorDataSeries _upDownSeries;

        [Parameter("RSI Periods", DefaultValue = 3, MinValue = 1)]
        public int RsiPeriods { get; set; }

        [Parameter("UpDown Periods", DefaultValue = 2, MinValue = 1)]
        public int UpDownPeriods { get; set; }

        [Parameter("ROC Periods", DefaultValue = 100, MinValue = 1)]
        public int RocPeriods { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        protected override void Initialize()
        {
            _upDownSeries = CreateDataSeries();

            _rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, RsiPeriods);
            _upDownRsi = Indicators.RelativeStrengthIndex(_upDownSeries, UpDownPeriods);
        }

        public override void Calculate(int index)
        {
            var bar = Bars[index];

            int upDownValue = 0;

            if (bar.Close > bar.Open)
            {
                upDownValue = 1;
            }
            else if (bar.Close < bar.Open)
            {
                upDownValue = -1;
            }

            if (index > 0 && (_upDownSeries[index - 1] > 0 && upDownValue == 1) || (_upDownSeries[index - 1] < 0 && upDownValue == -1))
            {
                _upDownSeries[index] = _upDownSeries[index - 1] + upDownValue;
            }
            else
            {
                _upDownSeries[index] = upDownValue;
            }

            if (index < RocPeriods) return;

            Result[index] = (_rsi.Result[index] + _upDownRsi.Result[index] + GetRoc(index)) / 3;
        }

        private double GetRoc(int index)
        {
            var percentChanges = new List<double>();

            for (int barIndex = index; barIndex > index - RocPeriods; barIndex--)
            {
                percentChanges.Add(GetBarChangePercent(barIndex));
            }

            var currentBarPercentChange = GetBarChangePercent(index);
            var numberOfValuesLessThanGivenValue = percentChanges.Count(iValue => iValue <= currentBarPercentChange);

            return 100.0 * numberOfValuesLessThanGivenValue / percentChanges.Count;
        }

        private double GetBarChangePercent(int index)
        {
            var bar = Bars[index];

            return (bar.Close - bar.Open) / bar.Open * 100;
        }
    }
}

Spotware's avatar
Spotware

Joined on 23.09.2013

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: ConnorsRSI.algo
  • Rating: 5
  • Installs: 1383
  • Modified: 13/10/2021 09:55
Comments
Log in to add a comment.
No comments found.