Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Issue with RSI

Hi,
I am trying to understand why the below script is printing values for dates where i know the RSI is not above 98 for the SPY. For example on 5/7/2013, the RSI(2) for the SPY was 94.82.
Additionally, why does it always seem to be printing out value of "100" for the RSI?

import numpy as np  
import math

rsi = ta.RSI(timeperiod=2)

def initialize(context):  
  context.sec = sid(8554)  
  context.TRIGGER_RSI = 98.0  
def handle_data(context, data):  
  rsi_data = rsi(data)  
  price = data[context.sec].close_price  
  sec_rsi = rsi_data[context.sec]  
  if not np.isnan(sec_rsi):  
      if (sec_rsi > context.TRIGGER_RSI):  
            log.info("RSI :" + str(sec_rsi) + ", price:" + str(price))  

Thanks much for any help in understanding this...

11 responses

Hi Mike,

I added some plotting to get a feel for the movement in the RSI, and I thought it might be useful for you.
How are you checking the values for the RSI(2)?

thanks,
fawce

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

John,
thanks for adding the plot. Regarding your question, i assume that line 18 (in your updated code) is what checks whether rsi(2) for a given date is greater than 98 and if so, log the rsi and price. Based on the plot you added, for 5/7/2013, RSI reading is 100 (which is incorrect), so that explains why it is being logged.
When i look at this chart in Ameritrade, the RSI(2) reading for SPY on 5/7/2013 is 94.82.
So this looks like a data issue or an issue with the RSI calculation. The ameritrade RSI is using the Wilder's calculation.

Hi Mike,

Our RSI comes from ta-lib, which is also based on Wilder's formula. However, the implementation (source code) includes this comment:

''' /* Often documentation present the RSI calculation as follow: * RSI = 100 - (100 / 1 + (prevGain/prevLoss))
*
* The following is equivalent:
* RSI = 100 * (prevGain/(prevGain+prevLoss))
*
* The second equation is used here for speed optimization.
*/
'''

As you can tell, the RSI we calculate will hit the ceiling value (100) whenever the prevLoss is zero. Here we are using 2 for the period count, which is quite small, and as a result two up days in a row will mean zero value for prevLoss. In the attached backtest I added a calculation for the average losses over the past two days, to confirm the behavior. So, the stretches of 100 are at least the correct behavior for the data we are feeding the indicator.

Unfortunately, this doesn't account for the difference between our RSI and the ameritrade RSI. I suspect that the discrepancy is due to the price inputs. I have two theories:
- In these tests we are using last trade price to create the close price on the daily bar. Perhaps ameritrade is using higher frequency data, or the official close price from the exchanges, which includes some trades that settle after the close. Could you compare the prices for SPY on 5/3 and 5/6 between Quantopian and Ameritrade?
- Is it possible Ameritrade is running the RSI on higher frequency data? maybe tick?

thanks,
fawce

The backtest referenced above:

i checked against Stockcharts.com and it looks like they also are showing an RSI of ~94.8 for 5/7/13. However, when i check BarCharts or finance.yahoo, i see that they show 100. I think this may be a result of the "smoothing" technique used for calculating the average gain/loss mentioned here: http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:relative_strength_index_rsi

You can see that stockcharts uses a standard average formula for calculating the first Gain and Loss values, but then use more of an EMA formula for calculating the rest of the values. They also do this over a 250 point set. So if one uses a standard average formula for calculating RS for every point and the period is very small, it is conceivable that gain and loss could be zero often, in which case the RSI would be 100 (or approach 100).
I feel that the approach amtd and stockcharts uses is a better one...but just my opinion.

re-visting this thread. How would one re-create the RSI calculation described here: http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:relative_strength_index_rsi?

Hello Mike,

You may get some ideas here where a chap has reproduced some of TA-Lib in Python.

P.

Thank you and i'll look. There are number of quant/analytic software or sites that use the EMA version (AmiBroker, Fidelity WL, StockCharts, Ameritrade)...i think Quantopian should offer a version of the RSI that does the same.

Hello Mike,

As a fan of TA-Lib I think Quantopian are doing the right thing in supporting it. I also think that they are right in using TA-Lib slightly differently to others i.e. because this is a primarily a backtesting platform you want the same results irrespective of the backtest start date. If this makes them "crazy" - see http://ta-lib.org/d_api/ta_setunstableperiod.html - then so be it.

Personally, I am happy to stick with TA-Lib and with the Quantopian implementation. I can see no benefit in something like ultra-finance which looks like a waste of a few hundred man-days.

P.

I hear you..the problem is, i have a bunch of RSI(2) studies that just doesn't work here given the very short time frame.
It's not about replacing what is there, what i'm saying is there should be an option to use a version of the RSI w/ ema smoothing given that clearly it is a popular option used by some notable software and sites.

You could write code to generate any RSI version you want, including Cutler's RSI. Obviously, the ta-lib version is not the standard one.