Buy when previous price > price before it.
def initialize(context):
context.security = [sid(8554)]
schedule_function(rebalance, date_rule=date_rules.every_day())
def rebalance(context, data):
prices = data.history(context.security, "price", bar_count=10, frequency="1d")
p1 = prices.ix[-1]
p2 = prices.ix[-2]
# current_price = data.current(context.security, 'price')
if p1 > p2:
order_target_percent(context.security, 1)
log.info("Buying %s" % (context.security.symbol))
elif p1 < p2:
order_target_percent(context.security, 0)
log.info("Selling %s" % (context.security.symbol))
I get the error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
There was a runtime error on line 13.
Line 13 is if p1 > p2: