RSI wrong calculate in cBot. around index 118--120 or something

Created at 17 Jun 2021, 17:55
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!
KY

kyunkakata

Joined 17.06.2021

RSI wrong calculate in cBot. around index 118--120 or something
17 Jun 2021, 17:55


This below is my demo cBot with wrong calculate:

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleRSIcBot : Robot
    {
        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Source", Group = "RSI")]
        public DataSeries Source { get; set; }

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

        private RelativeStrengthIndex rsi;

        protected override void OnStart()
        {
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
        }

        protected override void OnBar()
        {
            Print("CCindex= " + (Bars.Count - 1) + "; rsi= ", rsi.Result[Bars.Count -1] + " ; openPrice = " + Bars.OpenPrices[Bars.Count - 1]);
        }

        protected override void OnTick()
        {
        }

    }
}


It calculates wrong in index=118, 119,.. to realtime. (Start to OnBar first time invoke
See this image:


@kyunkakata
Replies

PanagiotisCharalampous
18 Jun 2021, 07:55

Hi kyunkakata

Can you please explain to us why do you think it is wrong?

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

kyunkakata
18 Jun 2021, 08:03

RE:

PanagiotisCharalampous said:

Hi kyunkakata

Can you please explain to us why do you think it is wrong?

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

The result RSI calculated by cBot is wrong when compared to the RSI indicator. It's slower by 1 or 2 candles for no reason. 


@kyunkakata

PanagiotisCharalampous
18 Jun 2021, 10:14

Hi kyunkakata

This happens because you are printing the value of the RSI at the opening of the candle, where the close price is the same as the open price. Until the candle closes, the value will change. The indicator displays values using the close prices of the candle.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

kyunkakata
18 Jun 2021, 12:17

RE:

PanagiotisCharalampous said:

Hi kyunkakata

This happens because you are printing the value of the RSI at the opening of the candle, where the close price is the same as the open price. Until the candle closes, the value will change. The indicator displays values using the close prices of the candle.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

Thanks for your answer.


@kyunkakata

kyunkakata
18 Jun 2021, 13:33

RE:

PanagiotisCharalampous said:

Hi kyunkakata

This happens because you are printing the value of the RSI at the opening of the candle, where the close price is the same as the open price. Until the candle closes, the value will change. The indicator displays values using the close prices of the candle.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

So which event I can choose when I want to calculate exactly RSI without the OnBar event?


@kyunkakata

PanagiotisCharalampous
18 Jun 2021, 13:37

Hi kyunkakata

If you need the last closed bar RSI, just use the previous index, not the current i.e. Bars.Count -1.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous