Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to save indicator data for all stocks in QTradableStocksUS

Hi all,

I'm quite new to quantopian, but steadily learning more. One problem that I keep running into is how to save data generated by a pipeline function. For example I try to obtain the delta of RSI14 which is simply:

delta_RSI14 = RSI14_t -RSI15_t-1

As such I need to save the obtained RSI14 for one day so I can call it the next. Below I have attached a simple algorithm to calculate RSI14. How can I change this so that it returns the delta of RSI14 instead?

Thanks!

P.S. I tried the history() function but read somewhere that this function only works with a limited number of stocks instead of the whole universe. Is this correct?

2 responses

Generally it's not a good idea to save pricing, volume, and factor data from day to day. The reason is these won't be adjusted properly for splits and dividends. One other issue is securities returned by the pipeline may change from day to day. The saved securities from yesterday may not match the securities from today so there will be missing values. It's best to simply re-calculate the values each day. In this case, calculate the RSI from the previous day.

So, I'd suggest writing a custom factor that returns the previous days RSI. A great starting place is to look at the code for the built in RSI factor (https://www.zipline.io/_modules/zipline/pipeline/factors/technical.html ). Most of the Quantopian code is open sourced in a package called zipline. Simply google something like "RSI zipline" and one should be able to find it.

So, with a little shameless cut and paste from the zipline code and several adjustments, something like this should work

class PreviousRSI(CustomFactor):  
    # Define inputs  
    inputs = [USEquityPricing.close]  


    # Set rsi_window and lookback to whatever number of days one wishes  
    # The default is a 15 day RSI from 1 day ago.  
    rsi_window = 15  
    lookback = 1  
    window_length = rsi_window + lookback  

    def compute(self, today, assets, out, closes):  
        # Use data from beginning of window to 'lookback' days ago  
        diffs = np.diff(closes[:-self.lookback], 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)))

One note. The RSI factor actually is 'window safe' meaning that it won't be affected by splits and dividends so technically it can be saved. However, it's probably just as easy to re-calculate and also won't have the missing security issue.

Finally, FYI the 'data.history' function DOES work with the entire universe of stocks.

Attached is a notebook showing the custom factor in action. Post if you need help getting it to work in your algo. Good luck.

Thank you very much! This was exactly what I was looking for. It seems to work as I intended for my algorithm.