Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
I cannot get past this

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:

3 responses

Try prices[context.security].values[-1, 0] and prices[context.security].values[-2, 0]

Ricardo,

context.security = symbol('SPY')

or

context.security = sid(8554)  

will solve the problem.

Thanks Vladimir. With your help I got past my first successful algo. Much appreciated.