Hi
I am a beginner, learning quant trading, followed this tutorial video of mean reversion strategy, I get syntax error when slicing the list. I commented everything above, below to see what it might be but cant find (is there anyway i could see details of this syntax error?)
Then I commented the whole weighted average function so I can proceed with tutorial then i get error "AttributeError: 'Positions' object has no attribute 'intervalues' " I tried look up documentations, cant find 'intervalues'
Appreciate the help.
Thanks
"""
mean reversion strategy means that stocks that moves away from its mean, revert back to its means.
stocks that trade above its mean are over priced and will be sort
stocks that trade below its mean are uner priced and will be long
"""
"""
mean reversion strategy means that stocks that moves away from its mean, revert back to its means.
stocks that trade above its mean are over priced and will be sort
stocks that trade below its mean are uner priced and will be long
"""
def initialize(context):
context.sids = [sid(351), sid(1900), sid(7671), sid(18529)]
schedule_function(
func = rebalance,
date_rule = date_rules.every_day(),
time_rule = time_rules.market_open()
)
schedule_function(
record_vars,
date_rules.every_day(),
time_rules.market_open()
)
def rebalance(context, data):
weights = calc_weights(context, data)
for security in context.sids:
if data.can_trade(security):
order_target_percent(security, weights[security])
def record_vars(context, data):
longs = shorts = 0
for position in context.portfolio.positions.intervalues():
if positions < 0:
shorts += 1
if positions > 0:
longs += 1
record(
leverage=context.account.leverage,
long_count = longs,
short_count = shorts
)
def calc_weights(context, data):
hist = data.history(context.sids, 'price', 30, '1d')
10_day_prices = hist[-10:]
30_day_prices = hist[-30:]
10_day_mean = 10_day_prices.mean()
30_day_mean = 30_day_prices.mean()
raw_weights = (10_day_mean - 30_day_mean) / 30_day_mean
return raw_weights / raw_weights.abs().sum()