Connors RSI Plus free

by jani in category Oscilator at 31/10/2021
Description

=============================================================================================
=======================================  Connors RSI  =======================================
=============================================================================================

The first component of the Connors RSI is the classic Relative Strength Index (RSI) itself. 
This indicator is also commonly referred to as Wilder’s RSI. The RSI can return a value 
of 0 to 100. Connors uses a default value of 3 Periods for his RSI Calculation.

The second component of the Connors RSI is called the “streak”. This basically determines 
a numerical value based on the relationship of close price values. Positive numbers indicate 
an upward streak (up bar), and negative numbers indicator a downward streak(down bar). 
Once the streak duration values have been determined, the RSI is applied to the streak, 
similar to applying RSI to price values. Connors uses a 2 period RSI by default in order to
evaluate the streak.

The third component of the Connors RSI is to look at the size of the current day’s price 
change relative to previous price changes. This calculation is referred to as “Percent Rank”,
or percentile. This tells us the percentage of values in the look-back period that are less 
than the current value.
So the Percent Rank is the number of values in the look back period that are less than the
current value, divided by the total number of values
Connors uses a default look back period of 100 bars for his Percent Rank calculation.


- 20200118  Added Up/Down bar counter condition to include flat bars according to the previous bar
- 20200210  Added Tick Volume feature
- 20200210  Added MA Smoothing
- 20200309  Added  "Use NetBars RSI" feature. Counting RSi of the Net Bars

 

Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgocTrader

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

/*
=============================================================================================
=======================================  Connors RSI  =======================================
=============================================================================================

The first component of the Connors RSI is the classic Relative Strength Index (RSI) itself. 
This indicator is also commonly referred to as Wilder’s RSI. The RSI can return a value 
of 0 to 100. Connors uses a default value of 3 Periods for his RSI Calculation.

The second component of the Connors RSI is called the “streak”. This basically determines 
a numerical value based on the relationship of close price values. Positive numbers indicate 
an upward streak (up bar), and negative numbers indicator a downward streak(down bar). 
Once the streak duration values have been determined, the RSI is applied to the streak, 
similar to applying RSI to price values. Connors uses a 2 period RSI by default in order to
evaluate the streak.

The third component of the Connors RSI is to look at the size of the current day’s price 
change relative to previous price changes. This calculation is referred to as “Percent Rank”,
or percentile. This tells us the percentage of values in the look-back period that are less 
than the current value.
So the Percent Rank is the number of values in the look back period that are less than the
current value, divided by the total number of values
Connors uses a default look back period of 100 bars for his Percent Rank calculation.




- 20200118  Added Up/Down bar counter condition to include flat bars according to previous bar
- 20200210  Added Tick Volume feature
- 20200210  Added MA Smoothing
- 20200309  Added  "Use NetBars RSI" feature. Counting RSi of the Net Bars
=============================================================================================
=============================================================================================
*/
namespace cAlgo.Indicators
{

    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AutoRescale = false, AccessRights = AccessRights.None)]
    public class ConnorsRSIPlus : Indicator
    {
        [Parameter()]
        public DataSeries Source { get; set; }

        [Parameter("Use RSI ", Group = "--- RSI ---", DefaultValue = true)]
        public bool UseRsi { get; set; }

        [Parameter("Show Connors RSI ", Group = "--- RSI ---", DefaultValue = true)]
        public bool ShowConnorsRsi { get; set; }

        [Parameter("Show Wilder’s RSI", Group = "--- RSI ---", DefaultValue = false)]
        public bool ShowWilderRsi { get; set; }

        [Parameter("RSI Period", Group = "--- RSI ---", DefaultValue = 3)]
        public int Period { get; set; }


        [Parameter("Use ROC Line", Group = "--- ROC % Rank ---", DefaultValue = true)]
        public bool UseRoc { get; set; }

        [Parameter("Show ROC Line", Group = "--- ROC % Rank ---", DefaultValue = false)]
        public bool ShowRoc { get; set; }

        [Parameter("ROC Period", Group = "--- ROC % Rank ---", DefaultValue = 3)]
        public int RocPeriod { get; set; }

        [Parameter("ROC Lookback Period", Group = "--- ROC % Rank ---", DefaultValue = 100)]
        public int RocLBPeriod { get; set; }

        [Parameter("Use Streak feature", Group = "--- Streak ---", DefaultValue = true)]
        public bool UseUpDw { get; set; }

        [Parameter("Show Streak Line", Group = "--- Streak ---", DefaultValue = false)]
        public bool ShowUpDw { get; set; }

        [Parameter("Streak RSI Period", Group = "--- Streak ---", DefaultValue = 2)]
        public int UpDwRsiPeriod { get; set; }

        [Parameter("Use NetBars feature", Group = "--- Net Bars---", DefaultValue = true)]
        public bool UseNetBars { get; set; }

        [Parameter("Show NetBars Line (Non-RSI)", Group = "--- Net Bars---", DefaultValue = false)]
        public bool ShowNetBars { get; set; }


        [Parameter("NetBars Lookback Period", Group = "--- Net Bars---", DefaultValue = 10)]
        public int NetBarsPeriod { get; set; }

        [Parameter("NetBars Ranking Lookback Period", Group = "--- Net Bars---", DefaultValue = 200)]
        public int NetBarsRelativePeriod { get; set; }


        [Parameter("Use NetBars RSI", Group = "--- Net Bars---", DefaultValue = false)]
        public bool UseNetBarsRsi { get; set; }

        [Parameter("NetBars RSI Period", Group = "--- Net Bars---", DefaultValue = 2)]
        public int NetBarsRsiPeriod { get; set; }

        [Parameter("NetBars Weight", Group = "--- Net Bars---", DefaultValue = 1, MinValue = 0)]
        public double NetBarsWeight { get; set; }


        [Parameter("Use Tick Volume ", Group = "--- Tick Volume ---", DefaultValue = false)]
        public bool UseTicks { get; set; }

        [Parameter("Show Tick Volume ", Group = "--- Tick Volume ---", DefaultValue = false)]
        public bool ShowTicks { get; set; }


        [Parameter("Tick Volume Factor ", Group = "--- Tick Volume ---", DefaultValue = 1.0, Step = 0.01, MinValue = 0)]
        public double TickVolumeFactor { get; set; }

        [Parameter("Tick Volume Ranking Lookback Period", Group = "--- Tick Volume ---", DefaultValue = 200)]
        public int TickRankPeriod { get; set; }

        [Parameter("Average Smooth Period", Group = "--- Smoothing & Signal ---", DefaultValue = 1, MinValue = 1)]
        public int SmoothPeriod { get; set; }

        [Parameter("Average Signal Period", Group = "--- Smoothing & Signal ---", DefaultValue = 5, MinValue = 0)]
        public int SignalPeriod { get; set; }

        [Parameter("Average Smooth MA Type:", Group = "--- Smoothing & Signal ---", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MAType { get; set; }

        [Parameter("Levels ", DefaultValue = 80, Step = 1)]
        public int Level { get; set; }



        [Output("level80", LineColor = "Red", LineStyle = LineStyle.Dots, PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries level80 { get; set; }

        [Output("level50", LineColor = "White", LineStyle = LineStyle.Dots, PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries level50 { get; set; }

        [Output("level20", LineColor = "Green", LineStyle = LineStyle.Dots, PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries level20 { get; set; }

        [Output("RSI", LineColor = "LightSkyBlue", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries RSI_ { get; set; }

        [Output("ROC Standardized", LineColor = "Yellow", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries ROCNorm_ { get; set; }

        [Output("Up Down Bars", LineColor = "Orange", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries upDwBars_ { get; set; }

        [Output("Better RSI", LineColor = "Aqua", PlotType = PlotType.Line, Thickness = 2)]
        public IndicatorDataSeries Average { get; set; }

        [Output("RSI Signal", LineColor = "White", LineStyle = LineStyle.Lines, Thickness = 1)]
        public IndicatorDataSeries AverageSignal { get; set; }

        [Output("Net Bars", LineColor = "Orchid", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries NetBars { get; set; }

        [Output(" Ticks", LineColor = "Khaki", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries Ticks { get; set; }

        /////////////////////////////

        //private MovingAverage MA1;
        // private MovingAverage _MA1;


        // private static double KijunFactor;

        public RelativeStrengthIndex rsi;

        public RelativeStrengthIndex UpDownRsi;

        public RelativeStrengthIndex NetBarRsi;

        public IndicatorDataSeries ROCNorm;
        public IndicatorDataSeries ROCNorm2;

        public IndicatorDataSeries RSI;

        public IndicatorDataSeries _Average;
        public IndicatorDataSeries _ROC;


        public PriceROC ROC1;
        public PriceROC ROC100;

        public int UpBars;
        public int DownBars;

        public double UpDownResult;
        public IndicatorDataSeries UpDownResult2;

        public IndicatorDataSeries Plus;
        public IndicatorDataSeries Minus;

        public int _UpBars;
        public int _DownBars;

        int UseRoc_;
        int UseUpDw_;
        int UseNetBars_;
        int UseTicks_;
        int UseRsi_;

        public IndicatorDataSeries NetBarResult;
        public IndicatorDataSeries NetBarResultFinal;
        public IndicatorDataSeries NetBarResultFinal2;

        private IndicatorDataSeries UpTick;
        private IndicatorDataSeries DnTick;
        private IndicatorDataSeries NetTick;
        private IndicatorDataSeries TicksFinal;

        private MovingAverage MA;
        private MovingAverage MA2;
        private MovingAverage MASignal;
        private MovingAverage MA2Signal;

        protected override void Initialize()
        {

            UseRoc_ = UseRoc ? 1 : 0;
            UseUpDw_ = UseUpDw ? 1 : 0;
            UseNetBars_ = UseNetBars ? 1 : 0;
            UseTicks_ = UseTicks ? 1 : 0;
            UseRsi_ = UseRsi ? 1 : 0;

            TickRankPeriod = Math.Min(Bars.Count, TickRankPeriod);
            NetBarsRelativePeriod = Math.Min(Bars.Count, NetBarsRelativePeriod);

            UpTick = CreateDataSeries();
            DnTick = CreateDataSeries();
            NetTick = CreateDataSeries();


            ROC1 = Indicators.PriceROC(Bars.ClosePrices, RocPeriod);
            ROC100 = Indicators.PriceROC(Bars.ClosePrices, 100);

            ROCNorm = CreateDataSeries();
            ROCNorm2 = CreateDataSeries();
            RSI = CreateDataSeries();

            _Average = CreateDataSeries();
            _ROC = CreateDataSeries();

            rsi = Indicators.RelativeStrengthIndex(Source, Period);


            Plus = CreateDataSeries();
            Minus = CreateDataSeries();

            UpDownResult2 = CreateDataSeries();

            NetBarResult = CreateDataSeries();
            NetBarResultFinal = CreateDataSeries();
            NetBarResultFinal2 = CreateDataSeries();

            TicksFinal = CreateDataSeries();


            UpDownRsi = Indicators.RelativeStrengthIndex(UpDownResult2, UpDwRsiPeriod);
            NetBarRsi = Indicators.RelativeStrengthIndex(NetBarResultFinal, NetBarsRsiPeriod);

            MA = Indicators.MovingAverage(_Average, SmoothPeriod, MAType);
            // MA2 = Indicators.MovingAverage(NetBarRsi.Result, SmoothPeriod, MAType);
            MASignal = Indicators.MovingAverage(MA.Result, SignalPeriod, MAType);
            MA2Signal = Indicators.MovingAverage(MA2.Result, SignalPeriod, MAType);

        }

        public override void Calculate(int index)
        {


            double TickFactor = UseTicks ? Bars.TickVolumes[index] * Symbol.TickSize * TickVolumeFactor : 0;



            NetTick[index] = Bars.ClosePrices[index] >= Bars.OpenPrices[index] ? Bars.TickVolumes[index] : -Bars.TickVolumes[index];


            ////////////////////// Calculate Ticks Ranking //////////////////////////////
            if (UseTicks || ShowTicks)
            {

                double CountLowTicks = 0;
                for (var k = 1; k <= TickRankPeriod; k++)
                {
                    if (NetTick[index - k] < NetTick[index])
                        ++CountLowTicks;
                }

                TicksFinal[index] = (CountLowTicks / TickRankPeriod) * 100;

            }
            else
                TicksFinal[index] = 0;


            ////////////////////// Calculate Up/Down Bars //////////////////////////////  

            DownBars = 0;
            UpBars = 0;
            for (var i = 1; i <= 20; i++)
            {
                if (Bars.ClosePrices[index - i] > Bars.OpenPrices[index - i])
                {
                    ++UpBars;
                    DownBars = 0;
                }
                else
                    break;
            }

            for (var i = 1; i <= 20; i++)
            {
                if (Bars.ClosePrices[index - i] < Bars.OpenPrices[index - i])
                {
                    ++DownBars;
                    UpBars = 0;
                }
                else if (Bars.ClosePrices[index - i] == Bars.OpenPrices[index - i])
                {
                    UpBars = 0;
                    DownBars = 0;
                }
                else
                    break;
            }
            UpDownResult2[index] = DownBars > 0 ? -DownBars : UpBars > 0 ? UpBars : 0;

            // ChartObjects.DrawText("RLabels", " TickRankPeriod " + TickRankPeriod + "  //  " + Bars.Count, StaticPosition.TopLeft, Colors.White);


            ////////////////////////////////////////////////////////////////////////////////

            ////////////////////// Calculate ROC Ranking //////////////////////////////
            double CountLowRoc = 0;
            for (var i = 1; i <= RocLBPeriod; i++)
            {
                if (ROC1.Result[index - i] < ROC1.Result[index])
                    ++CountLowRoc;
            }


            ////////////////////////// Calculate Net bars //////////////////////
            //  int netbars = 0;
            double count = 0;

            _UpBars = 0;
            _DownBars = 0;


            for (var i = 1; i <= NetBarsPeriod; i++)
            {
                if (Bars.ClosePrices.Last(i) > Bars.OpenPrices.Last(i))
                {
                    ++_UpBars;
                }

                else if (Bars.ClosePrices.Last(i) < Bars.OpenPrices.Last(i))
                {
                    ++_DownBars;

                }
                count++;
            }
            // netbars = (_UpBars - _DownBars);

            // Counting number adjusted, showing too mild on high lookback//   NetBarResult[index] = (((_UpBars - _DownBars) / count * 100) + 100)/2;

            NetBarResult[index] = ((_UpBars - _DownBars) / count);

            // Counting value in relation to last 100 or so values
            double CountLowNetBars = 0;
            for (var i = 1; i <= NetBarsRelativePeriod; i++)
            {
                if (NetBarResult[index - i] < NetBarResult[index])
                    ++CountLowNetBars;
            }

            NetBarResultFinal[index] = (CountLowNetBars / NetBarsRelativePeriod) * 100;
            NetBarResultFinal2[index] = !UseNetBarsRsi ? NetBarResultFinal[index] : NetBarRsi.Result[index];


            //////////////////////////////////////////////////////////////////////////////


            //////////////////////// Drawing /////////////////////////////
            RSI[index] = rsi.Result[index];

            if (ShowRoc)
                ROCNorm_[index] = (CountLowRoc / RocLBPeriod) * 100;

            if (ShowNetBars)
                NetBars[index] = NetBarResultFinal[index];

            if (ShowUpDw)
                upDwBars_[index] = UpDownRsi.Result[index];

            if (ShowWilderRsi)
                RSI_[index] = RSI[index];

            if (ShowTicks)
                Ticks[index] = TicksFinal[index];

            if (ShowConnorsRsi)
                _Average[index] = ((((CountLowRoc / RocLBPeriod) * 100) * UseRoc_) + (NetBarResultFinal2[index] * UseNetBars_ * NetBarsWeight) + (UpDownRsi.Result[index] * UseUpDw_) + (RSI[index] * UseRsi_) + (TicksFinal[index] * UseTicks_)) / (UseRsi_ + UseRoc_ + UseUpDw_ + (UseNetBars_ * NetBarsWeight) + UseTicks_);

            //  Average[index] = !UseNetBarsRsi ? MA.Result[index] : MA2.Result[index];
            Average[index] = MA.Result[index];

            // AverageSignal[index] = SignalPeriod > 0 ? !UseNetBarsRsi ? MASignal.Result[index] : MA2Signal.Result[index] : double.NaN;
            AverageSignal[index] = SignalPeriod > 0 ? MASignal.Result[index] : double.NaN;

            //  ChartObjects.DrawText("RLabels", " UseRoc_ + UseUpDw_ + UseNetBars_: " + countall + "  // count  : " + count, StaticPosition.TopLeft, Colors.White);
                        /**/
level80[index] = Level;
            level50[index] = 50;
            level20[index] = 100 - Level;

            //   level50[index] = 0;

        }
    }
}
Comments
0