Very cool algorithm. Thanks for posting.
I found a bug... right now you are computing daily_returns as follows:
daily_returns = np.zeros((len(context.securities),window))
# Compute daily returns
security_index = 0;
for security in context.securities:
if data.has_key(security):
for day in range(0,window):
day_of = all_prices[security][day]
day_before = all_prices[security][day-1]
daily_returns[security_index][day] = (day_of-day_before)/day_before
security_index = security_index + 1
The bug happens when day = 0 and we try to get day_before = all_prices[security][day-1]. The day - 1 wraps around and pulls out the data item at the very end of the all_prices window. I would suggest doing it this way:
all_prices = all_prices[context.securities]
daily_returns = all_prices.pct_change().dropna().values.T
The first line makes sure the columns of all_prices matches the order of the securities in context.securities. This may not be necessary, but I don't know the quantopian internals well enough to say. It would be easy enough to verify, but I haven't done it yet.
The second line produces an array that is (len(context.securities), window-1) because the first row is all NaN because we don't have a day_before to get data from. The transpose is so it matches the shape of the original daily_returns array (not sure if this is necessary, but I did it anyway).
You can verify that this gives the same array as before (except for the first, incorrect row), by doing np.allclose(original[:, 1:], new[:, 1;]), where original and new are the daily_returns arrays computed how they were and using my suggested method.