Hi,
New to Quantopian. What mistake am I making here?
Final line appears to be where the error is.
Help would be greatly appreciated.
" Mean Reversion Hypothesis: When the 10-day SMA is lower than the 30-day SMA, open a long position. When the 10-day SMA is higher than 30-day SMA, open a short position. Position size is proportional to the difference between the 10- and 30-day SMAs."
def initialize(context):
context.security_list = [sid(5061), sid(7792), sid(1941), sid(24556), sid(1746)]
"rebalance once a week at the beginning of the week at market open"
schedule_function(rebalance,
date_rules.week_start(),
time_rules.market_open())
schedule_function(record_vars,
date_rules.every_day(),
time_rules.market_close())
"will determine whether we take a long or short position as well as the size of the postion we want to take"
def compute_weights(context, data):
hist = data.history(context.security_list, 'price', 30, '1d')
price_10 = hist[-10:]
price_30 = hist[-30:]
sma_10 = prices_10.mean()
sma_30 = prices_30.mean()
raw_weights = (sma_10 - sma_30) / sma_30
normalized_weights = raw_weights / raw_weights.abs().sum()
return normalized_weights
def rebalance(context, data):
weights = compute_weights(context, data)
for security in context.security_list:
if data.can_trade(security):
order_target_percent(security, weights[security])
def record_vars(context, data):
longs = shorts = 0
for position in context.portfolio.position.itervalues():
if position.amount > 0:
longs += 1
elif postion.amount < 0:
shorts += 1
record(leverage=context.account.leverage, long_count=longs, short_count=shorts)