RSI indicator faulty

Created at 03 Jul 2024, 12:59
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
KE

kenneyyfx

Joined 12.02.2022

RSI indicator faulty
03 Jul 2024, 12:59


Hi Spotware,

It seems we have a very urgent issue i.e. the RSI indicator sometimes produces no values. This is extremely serious!!!

Occurence: In cTrader desktop 5.0.25 & 4.9.1 when referencing the Rsi value of a higher timeframe than the current chart from a cBot.

I have created a small bot to prove my point - the input values were orginally produced by the optimizer and runs fine in the optimizer, but not when backtesting the bot on its own.

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Faulty_RSI : Robot
    {
        private BollingerBands _bb = null;
        private AverageTrueRange _atr = null;
        private RelativeStrengthIndex _rsi = null;
        private Bars _rsi_bars = null;

        private const int C = 1;

        private double Rsi_UpperThreshold(int idx, double mlt) => 68.0 + (_atr.Result.Last(idx) * mlt);
        private double Rsi_LowerThreshold(int idx, double mlt) => 32.0 - (_atr.Result.Last(idx) * mlt);
        
        private Bars Get_Bars(TimeFrame tf)
        {
            var tmpBars = Bars;

            if (tf.ShortName != Chart.TimeFrame.ShortName)
            {
                tmpBars = MarketData.GetBars(tf);

                while (tmpBars.LoadMoreHistory() > 0) { };
            }

            return tmpBars;
        }

        protected override void OnStart()
        {
            _atr = Indicators.AverageTrueRange(311, MovingAverageType.WilderSmoothing);

            _rsi_bars = Get_Bars(TimeFrame.Day3);
            _rsi = Indicators.RelativeStrengthIndex(_rsi_bars.WeightedPrices, 14);

            _bb = Indicators.BollingerBands(_rsi_bars.WeightedPrices, 20, 2.0, MovingAverageType.Simple);
        }

        protected override void OnBar()
        {
            var lots = Math.Max(Symbol.NormalizeVolumeInUnits(Symbol.QuantityToVolumeInUnits(0.01)), Symbol.VolumeInUnitsMin);

            var rsiBE = _rsi.Result.Last(C).CompareTo(Rsi_UpperThreshold(C, 0.5)) > 0;
            var rsiBU = _rsi.Result.Last(C).CompareTo(Rsi_LowerThreshold(C, 0.5)) < 0;

            var bbBU = _rsi_bars.ClosePrices.Last(C).CompareTo(_bb.Main.Last(C) - ((_bb.Main.Last(C) - _bb.Bottom.Last(C)) / 2.0)) < 0;
            var bbBE = _rsi_bars.ClosePrices.Last(C).CompareTo(_bb.Main.Last(C) + ((_bb.Top.Last(C) - _bb.Main.Last(C)) / 2.0)) < 0;

            var tp = 4235.1;
            var SL_pips = tp * 1.125;

            if (bbBU && rsiBU) { ExecuteMarketOrder(TradeType.Buy, SymbolName, lots, "Faulty_BU", SL_pips, tp, $"BUBU-1"); }
            if (bbBE && rsiBE) { ExecuteMarketOrder(TradeType.Sell, SymbolName, lots, "Faulty_BU", SL_pips, tp, $"BEBE-3"); }
        }
    }
}  

THIS SHOULD NOT HAPPEN !!! - IN THE SCREENSHOT BELOW. IT SHOULD SHOW AN UPWARD SLOPING LINE CONSISTING OF 0VER 300 TRADES

Please try running a Backtest with these parameters

Symbol: BTCUSD

TimeFrame : M5

Start date: 01/01/2020

End date: 03/07/2024

Starting capital : 1000 (one thou)

Data: M1 bars from server (open prices)

Spread: Random from 1 - 260


@kenneyyfx
Replies

PanagiotisCharalampous
04 Jul 2024, 04:44 ( Updated at: 04 Jul 2024, 04:45 )

Hi there,

I have printed the RSI values and the values are there. So probably the problem is with your logic.

Best regards,

Panagiotis


@PanagiotisCharalampous

kenneyyfx
04 Jul 2024, 12:45 ( Updated at: 04 Jul 2024, 12:57 )

RE: RSI indicator faulty

PanagiotisCharalampous said: 

Hi there,

I have printed the RSI values and the values are there. So probably the problem is with your logic.

Best regards,

Panagiotis

Hi and thanks for getting back to me.  

YOU ARE RIGHT - MY LOGIC !!!    - i spend too many hours a day coding, that i couldnt even see what was right in front of me. Thank you so much!!! 


@kenneyyfx