Quantopian's community platform is shutting down. Please read this post for more information and download your code.
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

5 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?

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)  

Vladmir, thank you so much!!

Thomas

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

Hello,

Is there a way to calculate WRSI n-days ago ?

I found this function

class Prev_N_RSI(CustomFactor):  
    """  
    Remember to set the window length to N + what the window really is  
    If the desired lookback N is 2 days and the RSI window is 14 days,  
    Then set the window_length to 2+14=16  
    """  
    inputs = [USEquityPricing.close]  
    # Default window length RSI window = 14 / N = 0  
    # Override this when instantiating the factor  
    window_length = 14  


    def compute(self, today, assets, out, closes):  
        # Below is just for readibility RSI length assumed to be 14 days  
        rsi_window_length = 14  
        diffs = np.diff(closes[0:rsi_window_length], axis=0)

        ups = np.nanmean(np.clip(diffs, 0, np.inf), axis=0)  
        downs = abs(np.nanmean(np.clip(diffs, -np.inf, 0), axis=0))  
        out[:] = 100 - (100 / (1 + (ups / downs)))  

But it does not calculate well the past RSI (n-days ago). The actual RSI is pretty close from RSI we can find on Finviz or softwares but the RSI 4 days ago for example is very different. See my post (last one) here for more details : https://www.quantopian.com/posts/coding-a-custom-indicator-rsi-ema

I believe it is because it is not Wilder's RSI but then how can we calculate a RSI n-days ago which is equivalent to "popular" (Finviz, Investing, softwares etc) RSI ?

I want to return stocks of which RSI 5 days ago (e.g.) was higher than actual RSI, which is around 50.
I am not looking to backtest for the moment, just to build a screener. Is it possible to do this in a notebook, just with a pipeline ? Using USEquityPricing.

Do you have any idea how can I do that ?

*Edit : I tried to do like in Prev_N_RSI function, that is putting 16 (14+2) for rsi_len for RSI 2 days ago, but it does not work very well.

I noticed RSI does not vary anymore from window_length=75-80.

But I'm glad the actual RSI is far better calculated than with the built-in function !