Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
CustomFactor RSI n-days ago

Hi,

There is a built-in factor RSI. This is useful. But for me, many times I do want to have the RSI values of n-days ago.

I wrote a custom factor for this purpose. Hope you enjoy it. Any critics and sujustions are welcome.

Cheers

Thomas

Here is the code:

def offset_rsi_factor(days, **kwargs):  
    kwargs['window_length'] += days  
    class OffsetRSI(CustomFactor):  
        def compute(self, today, assets, out, closes):  
            diffs = np.diff(closes, axis=0)  
            ups = np.nanmean(np.clip(diffs[:-days], 0, np.inf), axis=0)  
            downs = abs(np.nanmean(np.clip(diffs[:-days], -np.inf, 0), axis=0))  
            out[:] =  100.0 - (100.0 / (1.0 + (ups / downs)))  
    return OffsetRSI(**kwargs)        

How to use it? Simply as follow:

rsi_offset_1 = offset_rsi_factor(1, inputs=[USEquityPricing.close], window_length=context.rsi_lookback)  
1 response

Thomas,

Have you thought about how to implement the averaging of ups and downs using an exponential weighted moving average?
This would allow the function to work for short rsi_lookback e.g. 2 or 3.

Steve