In the following code I always get the error when I try to run it with the full security universe (as the code shows), but when I run it and give a fixed value for XLF (toggle the commenting on the last three lines of code), I get valid data (no NaN data) for all the desired positions for the whole period through the info log. Any idea what the issue is?
from pandas import rolling_std
def initialize(context):
context.secs = [ symbol('XLY'), # XLY Consumer Discrectionary SPDR Fund
symbol('XLF'), # XLF Financial SPDR Fund
symbol('XLK'), # XLK Technology SPDR Fund
symbol('XLE'), # XLE Energy SPDR Fund
symbol('XLV'), # XLV Health Care SPRD Fund
symbol('XLI'), # XLI Industrial SPDR Fund
symbol('XLP'), # XLP Consumer Staples SPDR Fund
symbol('XLB'), # XLB Materials SPDR Fund
symbol('XLU') ] # XLU Utilities SPRD Fund
context.dev_wins = [15,20,25,30,35]
context.reg_wins = [260]
context.scaling = 0.0225
context.maxdw = max(context.dev_wins)
def handle_data(context, data):
returndata = history(bar_count=781, frequency='1d', field='price').pct_change().ix[1:]
desiredpositions = {}
for security in context.secs:
rmse = {}
for rw in context.reg_wins:
rmse[rw] = rolling_std(returndata[security],rw)
begin = -65
signals = {(rw,dw):0.0 for rw in context.reg_wins for dw in context.dev_wins}
for dt in returndata[security].index[begin:]:
for rw in context.reg_wins:
resids = returndata[security].ix[:dt].ix[-rw:].mean() - returndata[security].ix[:dt].ix[-context.maxdw:]
resids.index = list(range(context.maxdw))
stderr = rmse[rw][dt]
for dw in context.dev_wins:
signals[(rw,dw)] = resids.ix[-dw:].sum()/stderr
desiredpositions[security] = sum(signals.values())/len(signals) * context.scaling
log.info(desiredpositions.values())
for security in context.secs:
order_target_percent(security, desiredpositions[security]) #This is the line that provokes the error
# order_target_percent(symbol('XLF'), 1.0)