Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
ValueError: The truth value of a Series is ambiguous.

I cannot execute this code. It says ValueError: The truth value of a Series is ambiguous. (for "if" statement line)... Please help!

def initialize(context):

    context.security = symbol('AAPL')  
    schedule_function(handle_entry, date_rules.every_day(), time_rules.market_open(minutes=30))  
def handle_entry(context, data):  
    close_price = history(2, '1d', field='price')  
    open_price = history(2, '1d', field='open_price')  
    gap = close_price[context.security] - open_price[context.security]  
    log.info(gap, gap)  
    if gap > 0:  
       order(sid(24), 50)  
def handle_data(context, data):  
    pass  
2 responses

I just figured it out. i was comparing an array to a single value. The correct code should be :


def initialize(context):

    context.security = symbol('AAPL')  
    # Run handle_entry at 10:00 every day  
    schedule_function(handle_entry, date_rules.every_day(), time_rules.market_open(minutes=30))  

def handle_entry(context, data):  
    #Go long if open price is higher than prior day close  
    close_prices = history(3,'1d', field='close_price')[context.security]  
    open_prices = history(2, '1d', field='open_price')[context.security]  
    close_price_stock = close_prices[-1]  
    open_price_stock = open_prices[-1]  
    log.info(close_price_stock, close_price_stock)  
    log.info(open_price_stock, open_price_stock)  
    gap = close_price_stock - open_price_stock  
    log.info(gap, gap)  
    if gap > 0:  
      order(sid(24), 50)  

def handle_data(context, data):  
    pass  

What actual changes did you make the code looks the same or did you assign "gap" as an array and changed it?