It will be a challenge getting the notebook code 'as is' to work with a variable universe. The reason is the securities in the universe are not fixed. One day there may be 3200 securities the next there may be 3150. Additionally, the notebook is calculating returns based on pricing returned by pipeline. Pipeline prices are adjusted as of each day in the pipeline. To calculate returns one should use the get_pricing
method which are adjusted as of a single date.
So, perhaps a different approach... To quickly check if a particular factor or signal has any merit we strongly urge using Alphalens
. There is a good tutorial here https://www.quantopian.com/tutorials/alphalens . All one needs to do is encapsulate their signal into a pipeline factor and Alphalens
does the rest. In this case one could create a custom factor something like this
class RSI_Open_Close(CustomFactor):
"""
Custom factor to short when RSI crosses above 90
long when it crosses below 30. The output is 1, -1, 0
for long, short, hold.
"""
inputs = [RSI()]
# Below is the window length of this factor and not the RSI
window_length = 2
def compute(self, today, asset_ids, out, rsi):
low_rsi = 30
high_rsi = 90
rsi_today = rsi[-1]
rsi_yesterday = rsi[0]
# set crossing up assets to -1, crossing down to 1, others will be 0
crossing_up = np.where((rsi_today > high_rsi) & (rsi_yesterday <= high_rsi), -1, 0)
crossing_down = np.where((rsi_today < low_rsi) & (rsi_yesterday >= low_rsi), 1, 0)
# combine the two series. this works because they are exclusive of each other.
combined_up_down = crossing_up + crossing_down
out[:] = combined_up_down
Running Alphalens
it does appear there is alpha but it's the reverse of what was expected. Positive returns when the RSI crosses above 90 and negative when it crosses below 30. However, the alpha is small and the alpha decay is substantial with really only the 1 day returns being good.
I know this wasn't the original question but the tools on Quantopian are designed to answer questions like this with minimal effort.
The attached notebook has the Alphalens
analysis with some comments in the last cell. It's also a good example (with a small binning trick) of using Alphalens with factors which produce a discrete buy/sell signal. Play around with the notebook. Change the crossing points. Also change the stock universe. The number of periods and the length of periods in the analysis can also easily be changed. There are a number of different strategies associated with the RSI. Try inverting the logic to go long when crossing above 30 and short when crossing below 90.
Good luck.