Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Why does RSI backtest results change when data.history bar count changes

why does the size of the bar count in data.history change the outcome of talib factors such as MACD and RSI? This is driving me crazy!! If i setup, for instance an RSI function such as
RSI = talib.RSI(price[stock], timeperiod = 18)[-1]
it should have the same backtest results if the data.history is
price = data.history(context.sec, 'price', 22, '1d') or if it reads price = data.history(context.sec, 'price', 100, '1d') or any other variant of bar count amount.

I don't know if I'm thinking about this the wrong way, but as long as the bar_count is longer than the RSI timeperiod, than the only thing that should change the backtest results should be a change in timeperiod. Is this not right? Can anyone explain this to me???

5 responses

Look up the definition of the RSI algorithm. I believed it computes continual running average all the way back to the start of the series
as opposed to the specified interval.

RSI is based on Exponential moving average. Since the calculation for EMA is cyclical in that includes the EMA from the prior day it has to start with a Simple Moving Average. The more data you provide, the further away that SMA point is, giving you a more accurate RSI. I believe anything over 250 bars makes no difference to the RSI.

I should add that when I calculate RSI with 250 bars, the RSI in Quantopian matches the RSI in TDAmeritrade's Thinkorswim

Ben,

Try this:

import talib  
def initialize(context):  
    schedule_function(trade, date_rules.every_day(), time_rules.market_close(minutes =15))  
def trade(context, data):  
    stock = symbol('SPY')  
    period = 18

    prices = data.history(stock, 'price', period+1, '1d').iloc[-(period+1):]  
    RSI = talib.RSI(prices, period)[-1]  
    record(rsi=RSI,)  

and the results will be the same as in this:

import talib  
def initialize(context):  
    schedule_function(trade, date_rules.every_day(), time_rules.market_close(minutes =15))  
def trade(context, data):  
    stock = symbol('SPY')  
    period = 18

    prices = data.history(stock, 'price', period+100, '1d').iloc[-(period+1):]  
    RSI = talib.RSI(prices, period)[-1]  
    record(rsi=RSI,)  

I do not see any problem.

I don't think 250 bars is a correct solution. It depends on the rsi-period. See my post here:
https://www.quantopian.com/posts/using-the-talib-dot-rsi-is-danger