Category Oscilators  Published on 25/01/2023

RSI Weighted

Description

The Weighted Relative Strength Index (RSI) serves as a momentum indicator, calculated as the ratio between the sums of positive and negative bars in a specified lookback period. A significant challenge in using RSI as a momentum-based indicator lies in the fact that the optimal lookback period appears to evolve over time, resulting in losses with the current chosen lookback period.

To address the limitations associated with a single lookback period, we introduce a custom indicator. This indicator calculates a weighted average across all relevant lookback periods for the tradable asset. The custom indicator provides insights into the RSIs for both the close price and weighted momentum. Confluence, observed when both RSI components are either above or below the level 50, is considered a trading zone.


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

namespace cAlgo
{
    [Cloud("Polarity", "LevelZero", FirstColor = "Green", SecondColor = "Red", Opacity = 0.1)]
    [Levels(30,50,70)]
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class mRSIweight : Indicator
    {
        [Parameter("RSI Period (10)", DefaultValue = 10)]
        public int inpPeriodRSI { get; set; }
        [Parameter("Momentum Period (10)", DefaultValue = 10)]
        public int inpPeriodMomentum { get; set; }

        [Output("Polarity", LineColor = "Silver", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries outPolarity { get; set; }
        [Output("LevelZero", LineColor = "Transparent", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries outLevelZero { get; set; }
        [Output("RSI WeightMomentum", LineColor = "Black", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries outRSIWeight { get; set; }
        [Output("RSI", LineColor = "Red", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries outRSI { get; set; }

        private RelativeStrengthIndex _rsi, _rsipc;
        private IndicatorDataSeries _weightmomentum, _pol;
        private double _weight, _summom, _sumweight;

        protected override void Initialize()
        {
            _weightmomentum = CreateDataSeries();
            _pol = CreateDataSeries();
            _rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, inpPeriodRSI);
            _rsipc = Indicators.RelativeStrengthIndex(_weightmomentum, inpPeriodRSI);
        }

        public override void Calculate(int i)
        {
            _summom = _sumweight = 0;
            for (int j=0; j<inpPeriodMomentum && (i-j-1) >= 0; j++)
            {
                _weight = Math.Sqrt(j+1);
                _summom += (_rsi.Result[i] - (i>i-j-1 ? _rsi.Result[i-j-1] : 50)) / _weight;
                _sumweight += _weight;
            }
            _weightmomentum[i] = (_sumweight != 0) ? _summom / _sumweight : 0;
            _pol[i] = 
                    _rsipc.Result[i] > 50 && _rsi.Result[i] > 50 ? Math.Min(_rsipc.Result[i], _rsi.Result[i])
                  : _rsipc.Result[i] < 50 && _rsi.Result[i] < 50 ? Math.Max(_rsipc.Result[i], _rsi.Result[i])
                  : 50; 

            outRSIWeight[i] = _rsipc.Result[i];
            outRSI[i] = _rsi.Result[i];
            outPolarity[i] = _pol[i];
            outLevelZero[i] = 50;
        }
    }
}

mfejza's avatar
mfejza

Joined on 25.01.2022

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