Back to Community
Calculating RSI2 for present date & day before

Hey guys,

Currently I'm trying to use the RSI function that can be imported from quantopian.pipeline.factors. My question is, when I call RSI(window_length=2), how does it know what stock to calculate the RSI for? The universe I'm currently using in my pipeline is Q500US.

Also, what I'm trying to do with my algorithim is calculate the RSI2 of the present date (not the actual present date, but the date the algorithim is currently on when it's backtesting) and compare it with the RSI2 of the day before the present date. How can I accomplish something like this?

Thanks for the help,
Thomas

4 responses

I've also tried to get the RSI2 of the present date and previous date using ta-lib like so here. However, the logs seem to show that the RSI2 of yesterday is different than what my output is displaying it actually was yesterday. After further investigation, the stock prices for the previous day seems to very slightly off from what it actually was the previous day (As small as -0.04 off on some days). Any ideas why?

Clone Algorithm
2
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 58ace7bdda97665e4c5c3898
There was a runtime error.

Thomas,

RSI calculation just needs more data than rsi_period.
Try this:

import talib

def initialize(context):  
    schedule_function(record_rsi, date_rules.every_day(), time_rules.market_close())

def record_rsi(context, data):  
    stock = symbol('SPY')  
    rsi_period = 2  
    offset = 1  
    bars =  rsi_period + offset + 2

    prices = data.history(stock,'price', bars,'1d')  
    rsi_today = talib.RSI(prices[-4:-1], rsi_period)[-1]  
    rsi_yesterday = talib.RSI(prices[-5:-2], rsi_period)[-1]  

    print("-------------")  
    print("RSI Yesterday")  
    print(rsi_yesterday)  
    print("RSI Today")  
    print(rsi_today)

    record( rsi_today =  rsi_today,  rsi_yesterday = rsi_yesterday)  
Clone Algorithm
5
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 58adc2174100a461aab66150
There was a runtime error.

Vladmir, thank you so much!!

Thomas

Here is an implementation of Wilder's RSI(n) as a pipeline factor.

Clone Algorithm
2
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 58b013eddfbcda61b4ed8003
There was a runtime error.