When I try to run the code below, I get the build error "35 Error Runtime exception: ValueError: cannot convert float NaN to integer". Any idea why?
If I comment out the last two lines, the code runs.
Grant
import numpy as np
def initialize(context):
context.stocks = [ sid(19662), # XLY Consumer Discrectionary SPDR Fund
sid(19656), # XLF Financial SPDR Fund
sid(19658), # XLK Technology SPDR Fund
sid(19655), # XLE Energy SPDR Fund
sid(19661), # XLV Health Care SPRD Fund
sid(19657), # XLI Industrial SPDR Fund
sid(19659), # XLP Consumer Staples SPDR Fund
sid(19654), # XLB Materials SPDR Fund
sid(19660) ] # XLU Utilities SPRD Fund
def handle_data(context, data):
prices = history(31,'1d','price')[0:-1]
pct_change_sum = []
for k in range(len(prices)-1):
pct_change_sum.append(prices.pct_change(periods=k+1).sum())
prod = pct_change_sum[0]
for k in range(len(pct_change_sum)-1):
prod = np.multiply(prod,pct_change_sum[k+1])
prod[prod>0] = 0
prod[prod