I am trying to calculate the historic, or ex post rate of returns (daily) for each stock in the US Equities data set using the following:
from numpy import nanmean, isnan, nan, nanstd, rate, zeros
import scipy.optimize
from datetime import date, timedelta, datetime
from scipy.stats.mstats import zscore
from quantopian.pipeline import Pipeline
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import CustomFactor
from quantopian.pipeline.data.builtin import USEquityPricing
class AverageRates(CustomFactor):
inputs = [USEquityPricing.close]
window_length = 100
def compute(self, today, assets, out, close):
print close.shape
rows = close.shape[1]
rates = zeros(shape=(rows,))
for row in range(rows):
rates[row] = nanmean([(close[col+1][row]/close[col][row])-1 for col in range(100-1)])
print rates[row]
out[:] = rates
def initialize(context):
pipe = Pipeline()
pipe = attach_pipeline(pipe, name='my_pipeline')
rf = 0.03/252
ind_avg = AverageRates(inputs=[USEquityPricing.close],window_length=100) - rf
pipe.add(ind_avg, 'ind_avg')
def before_trading_start(context,data):
results = pipeline_output('my_pipeline')
print results.head(5)
I'm not sure if that is the fastest way to calculate the daily growth rates of stocks in Quantopian, but it appeared to work initially. At first it printed out the output as expected (from logs):
1970-01-01initialize:56INFO<class 'zipline.pipeline.data.dataset.DataSetMeta'>
1970-01-01initialize:57INFO<DataSet: 'USEquityPricing'>
2011-01-04PRINT(100, 7811)
2011-01-04PRINT0.00409921467387
2011-01-04PRINT0.00492940692112
2011-01-04PRINT0.0028075566189
2011-01-04PRINT-0.000496942946778
2011-01-04PRINT0.00344087158198
2011-01-04PRINT0.00867885646423
2011-01-04PRINT0.000847969853125
2011-01-04PRINT0.00343880618348
2011-01-04PRINT0.0162977327173
2011-01-04PRINT0.0030401659518
2011-01-04PRINT0.000382684323609
2011-01-04PRINT-0.00412265497972
2011-01-04PRINT-0.000416509413396
2011-01-04PRINT0.00214895167804
2011-01-04PRINT-0.000693733932113
2011-01-04PRINT0.00381662803722
2011-01-04PRINT0.00569411326117
2011-01-04PRINT-0.000834365428449
2011-01-04PRINT0.00105938366537
2011-01-04PRINT0.00480923930981
2011-01-04PRINT-0.00040027602352
2011-01-04null:nullWARNnumpy/lib/nanfunctions.py:598: RuntimeWarning: Mean of empty slice
Then everything stopped at that for 45 minutes until I gave up and began to write this post. The Backtesting % is stuck at 0.5%. I'm not sure if it has anything to do with that last log, the RuntimeWarning, but I highly doubt it.