Category Other  Published on 24/04/2023

RSI on Chart

Description

Show RSI indicator on chart (in Pips)


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

namespace cAlgo.Indicators
{
    [Levels(30, 70)]
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class mRSIonChart : Indicator
    {
        [Parameter("Smooth Type (ema)", DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType inpSmoothType { get; set; }
        [Parameter("Periods (14)", DefaultValue = 14, MinValue = 1)]
        public int inpPeriods { get; set; }
        [Parameter("OverBought level (70)", DefaultValue = 70.0)]
        public double inpOverBought { get; set; }
        [Parameter("OverSold level (30)", DefaultValue = 30.0)]
        public double inpOverSold { get; set; }

        [Output("RSI LevelMiddle", LineColor = "Black", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outRSIchMidLevel { get; set; }
        [Output("RSI Level OverBought", LineColor = "Green", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outRSIchOverBought { get; set; }
        [Output("RSI Level OverSold", LineColor = "Green", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outRSIchOverSold { get; set; }

        private IndicatorDataSeries _posdeltaraw, _negdeltaraw, _maxval, _minval, _overbought, _oversold;
        private MovingAverage _posdeltasmooth, _negdeltasmooth;

        protected override void Initialize()
        {
            _posdeltaraw = CreateDataSeries();
            _negdeltaraw = CreateDataSeries();
            _maxval = CreateDataSeries();
            _minval = CreateDataSeries();
            _posdeltasmooth = Indicators.MovingAverage(_posdeltaraw, (2 * inpPeriods - 1), inpSmoothType);
            _negdeltasmooth = Indicators.MovingAverage(_negdeltaraw, (2 * inpPeriods - 1), inpSmoothType);
            _overbought = CreateDataSeries();
            _oversold = CreateDataSeries();
        }

        public override void Calculate(int i)
        {
            _posdeltaraw[i] = Math.Max(Bars.ClosePrices[i] - Bars.ClosePrices[i-1], 0);
            _negdeltaraw[i] = Math.Max(Bars.ClosePrices[i-1] - Bars.ClosePrices[i], 0);
            _maxval[i] = (inpPeriods - 1) * (_negdeltasmooth.Result[i] * inpOverBought / (100 - inpOverBought) - _posdeltasmooth.Result[i]);
            _minval[i] = (inpPeriods - 1) * (_negdeltasmooth.Result[i] * inpOverSold / (100 - inpOverSold) - _posdeltasmooth.Result[i]);
            _overbought[i] = _maxval[i] >= 0 ? Bars.ClosePrices[i] + _maxval[i] : Bars.ClosePrices[i] + _maxval[i] * (100 - inpOverBought) / inpOverBought;
            _oversold[i] = _minval[i] >= 0 ? Bars.ClosePrices[i] + _minval[i] : Bars.ClosePrices[i] + _minval[i] * (100 - inpOverSold) / inpOverSold;

            outRSIchMidLevel[i] = (_overbought[i] + _oversold[i]) / 2;
            outRSIchOverBought[i] = _overbought[i];
            outRSIchOverSold[i] = _oversold[i];
        }
    }
}


mfejza's avatar
mfejza

Joined on 25.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: mRSIonChart.algo
  • Rating: 5
  • Installs: 1257
Comments
Log in to add a comment.
No comments found.