Hey all, whenever my program runs, I get a runtime error on the line (weights = compute_weights(context, data)): TypeError: Expected assets argument to be of type or iterable of type Asset, ContinuousFuture, basestring. Im assuming the error is coming from within the compute_weights(context, data) method, but I have rewritten it over and over and I cannot solve the issue. Does anybody know where my program is going wrong? Thanks!!!
def compute_weights(context, data):
high_history = data.history(252,'1d','high')
price = data.current(context.security, 'price')
# Find highest value in the past 52 weeks
high = max(high_history[context.security][:-1])
# Weights are based on the relative difference current price and 52-Week High (252 Days = 52 Weeks)
raw_weights = price / high
# Normalize our weights
normalized_weights = raw_weights / raw_weights.abs().sum()
# Determine and log our long positions.
long_secs = normalized_weights.index[normalized_weights > 0]
log.info("This week's longs: " + ", ".join([long_.symbol for long_ in long_secs]))
# Return our normalized weights. These will be used when placing orders later.
return normalized_weights
def rebalance(context, data):
"""
This function is called according to our schedule_function settings and calls
order_target_percent() on every security in weights.
"""
# Calculate our target weights.
weights = compute_weights(context, data)
# Place orders for each of our securities.
for security in context.security_list:
if data.can_trade(security):
order_target_percent(security, weights[security])
def record_vars(context, data):
"""
This function is called at the end of each day and plots our leverage as well
as the number of long and short positions we are holding.
"""
# Check how many long positions we have.
longs = 0
for position in context.portfolio.positions.itervalues():
if position.amount > 0:
longs += 1
# Record our variables.
record(leverage=context.account.leverage, long_count=longs)