FaycalAire
FaycalAire

Reputation: 1

How to get the same RSI as Tradingview in Java?

I don't understand why, but my RSI is always different as Tradingview's RSI.

Is use the same period (14 candles of 15min each), I use the same type of value (closes price), I tried to add the last non closed candle, but I never get the same RSI.

Tradingview RSI code:

//@version=4
study(title="Relative Strength Index", shorttitle="RSI", 
format=format.price, precision=2, resolution="")
len = input(14, minval=1, title="Length")
src = input(close, "Source", type = input.source)
up = rma(max(change(src), 0), len)
down = rma(-min(change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
plot(rsi, "RSI", color=#7E57C2)
band1 = hline(70, "Upper Band", color=#787B86)
bandm = hline(50, "Middle Band", color=color.new(#787B86, 50))
band0 = hline(30, "Lower Band", color=#787B86)
fill(band1, band0, color=color.rgb(126, 87, 194, 90), title="Background")

MY code with TA-Lib

MInteger outBegIdx = new MInteger();
MInteger outNbElement = new MInteger();
double[] outReal = new double[array.length-1];
int startIdx = 0;
int endIdx = array.length - 1;

Core core = new Core();
core.rsi(startIdx, endIdx, array, length-1, outBegIdx, outNbElement, outReal);
System.out.println(Arrays.toString(outReal));

return outReal[0];

my custom code without plugin

 double av_gain_up_periods = 0;
 double av_loss_down_periods = 0;
 int gain_count = 0;
 int loss_count = 0;
 double previous_observation = array[0];

 for (int i = 1; i < array.length; i++) {
            if (previous_observation <= array[i]) { // if gain
                double gain = array[i] - previous_observation;
                gain_count++;
                av_gain_up_periods += gain;
            }
            else { // if loss
                double loss = previous_observation - array[i];
                loss_count++;
                av_loss_down_periods += loss;
            }
            previous_observation = array[i];
        }
        av_gain_up_periods = av_gain_up_periods/gain_count;
        av_loss_down_periods = av_loss_down_periods/loss_count;

        // CALCULATE RSI
        double relative_strength = av_gain_up_periods/av_loss_down_periods;
        double relative_strength_index = 100-(100/(1+relative_strength));

        // PRINT RESULT
        return relative_strength_index;

I can grantee you that I have 14 closes price and they are the same as Tradingview's. The difference is in the calculation.

Related to this issue

Upvotes: 0

Views: 1161

Answers (1)

Mina Jahanbakhshi
Mina Jahanbakhshi

Reputation: 21

I think the problem is in RMA (relative moving average) calculation.it happens because of the unmutual starting point for getting RMA. Different starting point for getting RMA will cause big difference in calculated RSI oppose to tradingview RSI. Tradingview is using Realtime data, hence older data. My suggestion is to start with 1000 klines.

Here is the tradingview formula :

len = input(14, minval=1, title="Length")
src = input(close, "Source", type = input.source)
up = rma(max(change(src), 0), len)
down = rma(-min(change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))

according to this formula, first you need to get prices and store them somewhere. Closed candles are either gain or loss. After that you have to calculate the change for gain and loss.

Notice : for a closer result get more klines. you need the older data to have a more accurate calculation.( for example 1000 klines)

Now is the time to calculate RMA. The RMA formula is :

Alpha*source(or as we know, change) + (1-alpha) * previous RMA

Alpha : 1/period(for example 1/14)

At this point we are at the tricky part of calculation. As you can see in the RMA formula, we need the previous RMA to calculate the current one. Assume that we are using 1 hour timeframe and store the klines lets say in an array. Each and every one off array elements has a previous element, hence previous RMA ; But what about the element stored in array[0] ?

It would also be needing previous RMA. Here you will need to calculate SMA(simple moving average).

SMA : the number of prices within a time period is divided by the number of total periods.

The calculated SMA is equal the arrays first member RMA.

Now is the time to calculate RSI. With this method I was able to calculate RSI almost exactly same as tradingview. Personally, I wrote a bot with C++ to calculate RSI according to tradingview, using Binance API. You can see my code here :

https://github.com/Mina-Jahan/RSIgnal_bot

And I would be appreciated to hear your opinion in order to make my code better if there is a way to reform it. Thank you.

Upvotes: 2

Related Questions