RSI Last method, what's the value exactly mean?

RSI Last method, what's the value exactly mean?

Hello,

I have the code below, which intent to get RSI value based on the previous bar's close price, or past 2nd's 3rd's etc.. I understand rsi.Result.LastValue return the current moment RSI value in the bar. So, I can set a double RsiPreviousBarOpen to record it each time when a new bar start.

But I want to test Last method, like rsi.Result.Last(1) or rsi.Result.Last(3)... to get all data I need in the first run of OnBar(). Amazingly, the rsi.Result.Last(1) didn't get the same value as RsiPreviousBarOpen recorded. 

My question is based on the close price of the last bar how can I get RSI value via Last method, if it is possible? And what exactly rsi.Result.Last(1) return means?

Thanks,

Lei

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 TestingRSI : Robot
    {
        [Parameter("Source", Group = "RSI")]
        public DataSeries Source { get; set; }

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

        public double RsiPreviousBarOpen;

        private RelativeStrengthIndex rsi;

        protected override void OnStart()
        {
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
            
            Print("rsi.Result.Last(0)    : {0}", rsi.Result.Last(0));
            Print("rsi.Result.LastValue: {0}", rsi.Result.LastValue);
            
            RsiPreviousBarOpen = rsi.Result.LastValue;
        }

        protected override void OnBar()
        {   
            Print("rsi.Result.Last(0)    : {0}", rsi.Result.Last(0));
            Print("rsi.Result.LastValue: {0}", rsi.Result.LastValue);

            Print("rsi.Result.Last(1)  : {0}", rsi.Result.Last(1));
            Print("RsiPreviousBarOpen  : {0}", RsiPreviousBarOpen);
            
            RsiPreviousBarOpen = rsi.Result.LastValue;
       }

    }
}

RE:

PanagiotisCharalampous said:

Hi Delphima,

Last(x) uses x as a backwards index counter.  Last(1) will return the value for the previous bar, Last(2) for two bars before etc.

Best Regards,

Panagiotis 

Join us on Telegram

Hi Panagiotis,

Thanks for replying. 

Yes, I know Last(x) method could be used to get bars' high/low/open/close value. I wonder if it can also be used to get indicator's history value, in my example to get RSI's history value?

And, I tested with above code, it can return some value, but it's not what I expected. Can you have a look again?

Thanks,

Lei

RE:

PanagiotisCharalampous said:

Hi Delphima,

The method has the same function for indicator data series as well. You code has a logical mistake though. It prints the last value when a bar is created but then compares this value with the value the indicator when the bar closed. The value of the indicator can change in the meanwhile.

Best Regards,

Panagiotis 

Join us on Telegram

Hi Panagiotis,

Yes, we are quite close to the focus of my concern.

As you said, below code 'It prints the last value when a bar is created' and 'The value of the indicator can change'. I want to know the code below return RSI value based on last bar, but depend on which moment in the bar. Is it open price or high/close/low? I didn't figure it out in the comparison. Please give more information.  Thanks.

Print("rsi.Result.Last(1) : {0}", rsi.Result.Last(1));