Hey all, I'm just getting started here, I would really appreciate some help figuring out the batch_transform functionality.
My objective is to grab the last 21 days of a pair of stocks, then calculate the ratio of the pair, and then calculate a z-score (Current ratio - average ratio)/(Std of ratio). I'll eventually place an order in both names depending on the zscore, but I'm not worried about that right now. Here is the logic of what I want to do, but I'm not sure how to reference the columns in the dataframe. ratio = prices['19920']/prices['33217'] is wrong, not sure what the syntax is.
Thanks!
import numpy
def initialize(context):
context.qqq = sid(19920)
context.qqqx = sid(33217)
context.prices = []
@batch_transform(window_length=21)
def calc_zscore(datapanel, portfolio):
prices = datapanel['price']
ratio = prices['19920']/prices['33217']
zscore = (ratio[20]-ratio.mean())/numpy.std(ratio)
return zscore
def handle_data(context, data):
if calc_zscore(data, context.portfolio) > 1:
order(context.qqq,-100)
order(context.qqqx,100)