I am having an error running this algo. I used it 2 weeks ago and it was working just fine. Now it give len() of unsized object at line 44.
from zipline.utils.tradingcalendar import get_early_closes
def initialize(context):
context.stock = sid(2) # AAPL
set_commission(commission.PerTrade(cost=0.00))
set_slippage(slippage.FixedSlippage(spread=0.00))
set_benchmark(context.stock)
schedule_function(check_and_buy,
date_rule=date_rules.every_day(),
time_rule=time_rules.market_open(minutes=1))
schedule_function(sell_all,
date_rule=date_rules.every_day(),
time_rule=time_rules.market_close(minutes=5))
context.early_closes = get_early_closes(
context.stock.start_date, context.stock.end_date).date
def check_and_buy(context, data):
if get_datetime().date() in context.early_closes:
log.info('Early close: no trading')
else:
if context.stock in data:
highs = history(bar_count=200, frequency='1d', field='high')
lows = history(bar_count=200, frequency='1d', field='low')
opens = history(bar_count=5, frequency='1d', field='open_price')
close = history(bar_count=5, frequency='1d', field='close_price')
for s in data:
retns = (close[s][-2]-opens[s][-2])
ret = ((highs-lows)/lows)*100
retstd = ret.std()
retavg = ret.mean()
over = retavg+retstd
negover = -1*over
for s in data:
prev_ret = ret[s][-2]
# Insert your calculations here, and then depending
# on the result, set "buy" to True or False
if (retns > 0 and prev_ret > over):
order_target_percent(context.stock, -1)
record(avg=retavg)
else:
if (retns <0 and prev_ret < negover):
order_target_percent(context.stock, 1)
def sell_all(context, data):
if context.stock in data:
if context.portfolio.positions[context.stock].amount != 0:
order_target_percent(context.stock, 0)
def handle_data(context, data):
pass