@Dan and @ Sam,
Been working on an algo very similar in strat to this (at least in the rebalance function at the end it is).
Stumbling upon this error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Any idea what could be going on?
The error occurs in the if statement in the re balance function. Specifically, error happens on this line:
if time_momentum < .02: # end function if momentum for long index stocks is not above 2% for the day
This happens in the rebalance function towards the end.
Where this occurs, all I am attempting to do is create a time_momentum that screens for current price and compares it to price 30 minutes ago. If the price increase is more than 2% it gets long, and less than 2% it gets short. However, something may be off with my syntax and hoping you might catch it. I was thinking of posting to forum, but with the low hit ratio, thought it would be much smarter to connect with you first.
Please let me know what you think. Code is below:
def rebalance(context, data):
for security in context.longs.index:
thirty_min_prices = data.history(context.assets, 'price', 30, '1m') # pull prices n minutes ago
thirty_price = thirty_min_prices.ix[0] # prices n minute ago
current_price = thirty_min_prices.ix[-1] # current price
time_momentum = (current_price / thirty_price) - 1 # calculate % change in move for past n minutes
if time_momentum < .02: # end function if momentum for long index stocks is not above 2% for the day
return
if time_momentum > .02: # do whatever logic when time momentum is more than 2%
order_target_percent(security, context.longs[security])
for security in context.shorts.index:
thirty_min_prices = data.history(context.assets, 'price', 30, '1m') # pull prices n minutes ago
thirty_price = thirty_min_prices.ix[0] # prices n minute ago
current_price = thirty_min_prices.ix[-1] # current price
time_momentum = (current_price / thirty_price) - 1 # calculate % change in move for past n minutes
if time_momentum > .02: # end function if momentum for short index stocks is not below 2% for the day
return
if time_momentum < .02: # do whatever logic when time momentum is less than n%
order_target_percent(security, -context.shorts[security])
for security in context.portfolio.positions.iterkeys():
if security not in context.security_list:
order_target_percent(security, 0)