Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
TA-LIB RSI using something other than close price?

I'm fooling around porting a system to quantopian and am noticing wildly differing results in the ta-lib RSI indicator than expected. I'm testing against data from both yahoo and tradingview.com. The RSI derived from those 2 sites line up pretty well with each other but the RSI numbers I'm seeing in quantopian are much different. Anyone have any ideas why? Is the ta-lib RSI calculated using a price other than close?

14 responses

The discrepancy may be caused by not "warming up" the exponential moving averages used in the RSI indicator. Try calculating the TaLib RSI over a larger period eg. if your interested in RSI over 2009, start calculating it from 2008.

This problem might be fixed by the Draft Proposal of New Data History API.

I think Aidan's probably got the right guess. If you're doing a 60 day window, for instance, the first 60 days aren't going to match, but the 61st on will. If that doesn't help, share a backtest with the bad results, and we can take a closer look.

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.

Hello Aidan/Dan,

I think you are in the right area but not right. Quantopian implements TA-Lib functions to not have a 'memory' beyond the period used for calculations. For example this is the 14-period RSI of SID(2) on 10/21/2013 in four different backtests:

Start         End            RSI  
09/13/2013    10/21/2013     64.48  
06/13/2013    10/21/2013     64.48  
03/13/2013    10/21/2013     64.48  
10/22/2012    10/21/2013     64.48  

The issue is the way Yahoo and others calculate their RSI and other indicators with a 'memory' like MACD. Eddie described this here:

https://www.quantopian.com/posts/ta-dot-macd-values-dont-match-google-or-yahoo-finance-charts

and TA-Lib describes it here:

http://ta-lib.org/d_api/ta_setunstableperiod.html

P.

Hi all, thanks for your input. Peter, I understand what you are saying but I guess still not sure how it's an issue in this case. I'm testing with an RSI(3) and do see how the values are consistent regardless of the timeframe selected. What I guess I don't understand is how the values could be so different from yahoo given that I'm looking at such a short term. I would expect the beginning of each series to be off but it should converge pretty quickly. As an added note, the 10 period MA seems to line up pretty well with other sources.

I attached the code I'm using to compare quantopian AAPL rsi to yahoo and tradingview.com. Check out the log output. Just eyeballing it, this RSI seems more sensitive than the others.

Hello Beau,

            Quantopian  Yahoo       TradingView   Wealth-LAB  
Date        RSI         RSI         RSI           RSI  
09/03/13    -           N/A         N/A           N/A  
09/04/13    -           N/A         N/A           N/A  
09/05/13    -           N/A         N/A           N/A  
09/06/13    79.39       79.25       66.98         66.97  
09/09/13    76.23       76.12       83.13         83.12  
09/10/13    48.48       48.60       40.28         40.28  
09/11/13    17.17       17.13       14.35         14.35  
09/12/13    11.24       11.46       27.32         27.33  
09/13/13    12.34       12.54       20.16         20.16  
09/16/13    17.71       18.08       11.55         11.55  

Are these what you see?

P.

Hi Peter, yes as a matter of fact that is exactly what I'm seeing. I'm using AmiBroker (with Yahoo data, I realize now the error in my previous messages. Using yahoo data but AmiBroker is calculating RSI) whose numbers line up with TradingView and WealthLab in your example.

So now that that's cleared up, why the difference in how these systems are calculating RSI?

Hello Beau,

I believe the Quantopian RSI does not use EMA but uses this formula:

              100  
RSI = 100 - --------  
             1 + RS

RS = Average Gain / Average Loss

(See: http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:relative_strength_index_rsi)

The Quantopian closing prices are:

2013-09-11 467.83
2013-09-12 472.69 Gain = 4.86 Loss = 0.00
2013-09-13 464.90 Gain = 0.00 Loss = 7.79
2013-09-16 450.11 Gain = 0.00 Loss = 14.79

Because it's a 3 period RSI:

Average Gain = (4.86 + 0.00 + 0.00/3 = 1.62
Average Loss = (0.00 + 7.79 +14.79)/3 = 7.52666

RS = 1.62/7.52666 = 0.21523

              100  
RSI = 100 - --------      =  17.71  
             1 + 0.21523

It seems Yahoo does this as well with their prices being very slightly different to Quantopian. We can check that:

The Yahoo closing prices are:

2013-09-11 467.71
2013-09-12 472.69 Gain = 4.98 Loss = 0.00
2013-09-13 464.90 Gain = 0.00 Loss = 7.79
2013-09-16 450.12 Gain = 0.00 Loss = 14.78

Because it's a 3 period RSI:

Average Gain = (4.98 + 0.00 + 0.00/3 = 1.66
Average Loss = (0.00 + 7.79 +14.78)/3 = 7.52333

RS = 1.66/7.52333 = 0.22064

              100  
RSI = 100 - --------      =  18.08  
             1 + 0.22064

The others will be using EMA as in:

RSI = (100 – (100 / (1 + RS)))

RS = 14-day EMA of upday closing gains / 14-day EMA of downday closing losses

(See: http://www.iexplain.org/rsi-how-to-calculate-it/)

EMA has a memory which is as long as all the available data although after 100 days or so it becomes insignificant.

P.

hello,

I am seeing vast differences between Ta lib's RSI value compared to the RSI from reported by yahoo and tdameritrade's think or swim app, both of which agree with each other. does Ta lib have a calculation based on Wilder's formula? Perhaps I am doing something wrong?

Here's the table:TSLA rsi
date quantopian -talib yahoo/TDA
12/01/14 42.4880243867 39.79
12/02/14 33.611510455 39.66
12/03/14 33.5555377748 38.3
12/04/14 29.9794327961 37.63
12/05/14 18.7549120476 34.72
12/08/14 17.3466623052 29.67
12/09/14 15.5894158398 32.53
12/10/14 16.3879705436 28.99
12/11/14 14.7061536715 28.53
12/12/14 15.8651601099 27.62
12/15/14* 8.4190872663 26.19
12/16/14* 5.4267290159 23.45
12/17/14 16.4832643774 33.14
12/18/14 31.8086813823 44.82
12/19/14 39.7940202426 45.67
12/22/14 43.0351570757 48.41
12/23/14 43.3428450678 47.15
12/24/14 45.3033274302 48.3
12/26/14 53.2055876158 53.03
12/29/14 59.8283455793 51.12
12/30/14 54.373371855 48.04
12/31/14 62.1669038383 48.21
01/02/15 59.741111236 45.4
01/05/15 52.4781964785 38.25
01/06/15 56.0670949322 39.57
01/07/15 62.1744374505 39.32
01/08/15 55.2200343597 39.05
01/09/15 34.1835628321 35.9
01/12/15 28.8066150179 32.7
01/13/15 26.5637006301 35.53
01/14/15 21.1256416524 28.27
01/15/15 18.6150302332 27.84
01/16/15 10.7796717172 29.54
01/20/15 11.0512690121 28.84
01/21/15 21.3042694198 35.52

I tweaked the algorithm above to print TSLA RSI from 11/1/14 - 1/31/15:

import numpy as np  
import math

# Set up the Relative Strength Index indicator.  
ma = ta.MA(timeperiod=10)  
rsi3 = ta.RSI(timeperiod=14)


# This is the standard Quantopian function that initializes your data and  
# variables. In it, we define how large of a 'bet' we're making (in dollars) and what  
# stock we're working with.  
def initialize(context):  
    context.stocks = [ symbol('TSLA')]  
    context.bet_amount = 5000  
    context.count = 20  
    context.max_notional = 10000.1  
    context.min_notional = -10000.0  


# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    sym = context.stocks[0]  
    ma_data = ma(data)  
    rsi_data = rsi3(data)  
    op = data[sym].open_price  
    high = data[sym].high  
    low = data[sym].low  
    close = data[sym].close_price  
    curr_ma = ma_data[sym]  
    curr_rsi = rsi_data[sym]  

    print "OHLC: " + str( op ) + ' ' + str( high ) +' ' + str( low ) + ' ' + str( close ) + " | MA10: " + str( curr_ma ) + " | RSI3: " + str( curr_rsi )  

    return  

Hey Gang - I'm new to this site, so don't shoot me! I validated the RSI calcs in TA-LIB - they are exactly as shown by Peter Cawthron (I reviewed the 'C' code and ran it on my debugger).

Question: As I'm new to the real-world application of TA and RSI, what are the Pros/Cons of using a RSI that includes a EMA in the RSI calcs, versus the simple smoothing (Wilder's approach - really a SMA type) method TA-LIB is using (as noted above). Thanks!

@Dale H., Six of one, half dozen of another.

I'll share what I've come to realize as an unconventional truth regarding such price treatments as the RSI: technical indicators contain no absolute information. And that only the barest whisper of value that they may provide comes within their self-fulfilling prophesy. That is, if the industry agreed upon standard of the 14 period RSI for AAPL falls below 50, that that might influence the markets in some possibly predictable manner.
And that's it.
If you write an algo that attempts to use such technical metrics and you're not attempting to capture broad market behavior based on those technical metrics then let fancy play with your creation. A 10 period RSI of the 2,9,25 period KAMA of the 35,13,5 period MACD signal line is just as valid an indicator as any of them. Such a fantasy is just as predictive. For in the end it's all relative. Your RSI based on an EMA may tell you something, while my RSI based on the WMA may tell me something else. Both are just as valid in this space. Do any of them hold a smidgen of predictive power? Only you can discover that.

Hey @Market Tech: Thanks for your perspective - it aligns with mine quite well! All these various TA indications, graphs and what I'll call 'observations' are just that . . . they MIGHT give you some perspective to make your own decisions upon. As I'm really a newbie (but very experienced in business, the nuances of people, etc), I think that the whole world of TA is quite valuable, but is it 'TA' . . . technical analysis. I believe the best decisions I've made in business will take into account the 'TA' of the situation, but must be combined with the reality of the present situation, take stock in the people (could call it 'Emotional Analytics' or EA) and then also the more macro 'Global or Market Analytics' of the situation at hand. My quest is to find some way to meld a combination of 'indicators' and 'insights' into a set of information that doesn't necessarily make a decision for me (buy, sell, hold), but provides me with additional data to create my own perspectives and market position . . . that might just help me 51% of the time . . . to make better decisions.

Given my above babble, am I thinking about this in a realistic or rational manner?
Dale

@Dale H., Sure, I'd say you've got a rational mind toward all of this. Rather like brewing a witch's potion: 3 parts TA, 1 part sentiment, 2 parts macro analysis and 4 parts statistical stratification. Combine all ingredients, stir well, pour into various sized accounts. When they begin to turn green, harvest at your leisure. If they show signs of red, dump quickly and start over. Be sure to use a viable cauldron. Mine's been cracked for ages.

FWIW, it seems that the TA-Lib RSI calculation is performed on a series of prices, just like @Peter Cawthron 's calculation (And as verified by @Dale Hays). However, there are variations I've come across on calculating RSI which uses candles rather than a series of prices (say, close prices). Here's a Quantopian post with pandas-centric code on doing this, and here's the same formula spelled out from Oanda. In these variant formulae, the RSI is taken not from the difference between two separate price times, but between the high/low candle components of a single period (ie a single candle represents a period of time in itself). One may find that Yahoo and Think Or Swim use the candle method (deltas between high/low of a single candle) of calculating RSI rather than the deltas between consecutive data points.