Stochastic RSI free

by tperalta82 in category Oscilator at 18/04/2021
Description

Stochastic RSI Indicator based on TradingView approach

 

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.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = false, AccessRights = AccessRights.None)]
    public class StochasticRSI : Indicator
    {
        [Parameter("Source", Group = "Base Settings", DefaultValue = "Close")]
        public DataSeries Source { get; set; }

        [Parameter("Oversold Level", DefaultValue = 20, MinValue = 1, Step = 1)]
        public int Oversold { get; set; }

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

        [Parameter("RSI Periods", DefaultValue = 14, MinValue = 2)]
        public int RSIPeriod { get; set; }

        [Parameter("Stochastic K%", DefaultValue = 3, Step = 1, MinValue = 2)]
        public int StochK { get; set; }

        [Parameter("Stochastic D%", DefaultValue = 3, Step = 1, MinValue = 2)]
        public int StochD { get; set; }

        [Parameter("Stochastic Periods", DefaultValue = 14, Step = 1, MinValue = 2)]
        public int StochPeriods { get; set; }

        [Output("Smoothed K", LineColor = "Turquoise")]
        public IndicatorDataSeries SmoothedK { get; set; }

        [Output("Smoothed D", LineColor = "Red")]
        public IndicatorDataSeries SmoothedD { get; set; }

        [Output("Oversold", LineColor = "Turquoise")]
        public IndicatorDataSeries oversold { get; set; }

        [Output("Overbought", LineColor = "Red")]
        public IndicatorDataSeries overbought { get; set; }

        private RelativeStrengthIndex rsi;

        private IndicatorDataSeries StochKSeries { get; set; }
        private IndicatorDataSeries StochDSeries { get; set; }

        protected override void Initialize()
        {
            rsi = Indicators.RelativeStrengthIndex(Source, RSIPeriod);
            StochKSeries = CreateDataSeries();
            StochDSeries = CreateDataSeries();
        }

        public override void Calculate(int index)
        {
            overbought[index] = Overbought;
            oversold[index] = Oversold;

            double LRSI = rsi.Result.Minimum(StochPeriods);
            double HRSI = rsi.Result.Maximum(StochPeriods);

            double K = 100 * ((rsi.Result.LastValue - LRSI) / (HRSI - LRSI));
            StochKSeries[index] = K;

            double d = 100 * (rsi.Result.Maximum(StochD) / rsi.Result.Minimum(StochD));
            StochDSeries[index] = d;

            SimpleMovingAverage ksma = Indicators.SimpleMovingAverage(StochKSeries, StochK);
            SimpleMovingAverage dsma = Indicators.SimpleMovingAverage(ksma.Result, StochD);

            SmoothedK[index] = ksma.Result.LastValue;
            SmoothedD[index] = dsma.Result.LastValue;
        }
    }
}
Comments
0