Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about RSI implementation #68

Closed
icamys opened this issue Apr 11, 2022 · 4 comments · Fixed by #71
Closed

Question about RSI implementation #68

icamys opened this issue Apr 11, 2022 · 4 comments · Fixed by #71
Assignees

Comments

@icamys
Copy link

icamys commented Apr 11, 2022

Hi! First of all, I would like to thank you for this great package.

Recently I tested the RSI indicator, and I found some divergence in the RSI values produced by this package and values calculated by myself and other tools. Just wanted to find out why is it so.

Here's an example of closing values:

closing := []float64{
    0.003544, 0.003498, 0.003548, 0.003565, 0.003827, 0.003934, 0.003904, 0.003835, 0.004432, 0.004366, 0.004281, 0.00434, 0.004538, 0.004611, 0.004383, 0.004389, 0.004182, 0.004259, 0.004291, 0.004403, 0.004257, 0.004203, 0.004067, 0.004084, 0.004299, 0.00467, 0.004506, 0.004519, 0.005683, 0.005335, 0.005534, 0.005251, 0.004963, 0.005871, 0.005063, 0.004922, 0.005215, 0.00513, 0.005625, 0.00564, 0.005338, 0.005369, 0.005433, 0.005303, 0.005185, 0.005165, 0.005105, 0.005152, 0.005187, 0.005135, 0.00516, 0.00518, 0.005296, 0.005156, 0.005131, 0.005102, 0.004958, 0.004969, 0.004987, 0.00498, 0.004966, 0.0051, 0.005107, 0.004948, 0.00479, 0.004798, 0.004792, 0.004587, 0.004442, 0.004388, 0.004272, 0.004326, 0.004479, 0.004509, 0.004427, 0.004453, 0.004476, 0.004542, 0.004526, 0.00448, 0.0045, 0.004374, 0.00433, 0.004297, 0.00437, 0.004355, 0.004301, 0.004282, 0.004349, 0.004417, 0.004317, 0.004325, 0.004315, 0.004538, 0.00477, 0.004659, 0.00468, 0.004105, 0.003875, 0.003882,
}

_, rsi := indicator.Rsi(closing)

// Printing out the last ten values
for i := len(rsi) - 10; i < len(rsi); i++ {
    println(fmt.Sprintf("%f", rsi[i]))
}

The code below prints these values:

// last ten RSI values from this package
39.357430
34.252540
34.553441
53.372093
62.593284
63.481552
66.924565
43.908629
35.718407
36.289855

I've calculated the RSI values manually (using google sheets) and got these values:

// last ten RSI values from manual calculations
40.00359988
40.55514538
40.05940381
53.66234853
63.05581295
57.09246379
57.90359501
37.1789257
32.21242822
32.50793062

The document with my calculations is available here.

My calculations match the values from the Binance exchange.

Looking into the implementation of the package, I've found that the moving average values and the RSI values are available starting from the 3rd closing value, which is strange because the average value of the 14 closing values should be available starting from the 15th closing value. These MA issues are the point where my calculations start to diverge with the work of this package.

So I wanted to ask and ensure whether there are no mistakes in the RSI implementation. If not, could you please elaborate on what I've done wrong in my calculations and why they match the Binance exchange values? Thank you in advance.

@cinar
Copy link
Owner

cinar commented Apr 23, 2022

Hi! So sorry for my very late response. I was on a trip, and just had a chance to take a look at this. First of all, thank you very much for taking the time to write such a descriptive bug report. Very much appreciated definitely.

I looked at your calculations document, and I noticed that on Column F, you are calculating the SMA with =SUM(D3:D16)/14 but afterwards, you are using =((F16*13)+D17)/14 for the remaining rows. I guess that is the reason for the difference in the results.

I copied your calculations document, and added the columns J and K to calculate the SMA through the =SUM()/14 method, and I was able to get results aligned with the ones calculated by the page at rows 92 to 101.

Could you take a look? I hope it didn't miss anything.

https://docs.google.com/spreadsheets/d/1e3r1mBAQNXd3yI2l-CiEls0Pfn3mRGv_jxTVJ8kNT9Q/edit?usp=sharing

@cinar cinar self-assigned this Apr 23, 2022
@icamys
Copy link
Author

icamys commented Apr 23, 2022

@cinar Thanks for responding!

you are calculating the SMA with =SUM(D3:D16)/14 but afterwards, you are using =((F16*13)+D17)/14 for the remaining rows

The logic here is that on the 15th closing I calculate the average of the previous 14 values, after that, I calculate the weighted average using =((F16*13)+D17)/14.

I would be glad to take a look if you allow the document for reading :)

@icamys
Copy link
Author

icamys commented Apr 23, 2022

@cinar Looks like I found the answer, to why my calculations are the same as on the Binance but differ from those provided by this library.

The Binance exchange uses the TradingView plots. If we take a look at the TradingView RSI documentation we will see, that in their RSI formula they use the Rolling Moving Average:

image

which in its turn has exact the same formula that I used in my calculations (screenshot is taken from here):

image

While in this library for the RSI calculation the Simple Moving Average is used.

During my further research, I found out, that the first average gain and all subsequent should be calculated slightly differently. Here's what I found on the stockcharts.com:

image

The same is told on quantinsti.com:

image

Not an expert, but it's clear to me that the current RSI implementation differs a bit.

What do you think about it?

@cinar
Copy link
Owner

cinar commented Apr 24, 2022

You got me convinced! :) I will take a look and update the calculation here.

cinar added a commit that referenced this issue Apr 30, 2022
@cinar cinar mentioned this issue May 5, 2022
@cinar cinar closed this as completed in #71 May 5, 2022
cinar added a commit that referenced this issue May 5, 2022
* Adding rolling moving average (RMA) indicator.

* Adding RMA to index.

* Changing RSI to use RMA indicator.

Fixes #68.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants