Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
order_target(stock, 0) stopped working for me for some reason

Hi,
I must have changed something that now my eyes don't see! It's a simple order_target(stock, 0) that is giving me issues now, though I'm sure it's something else that is causing an error below

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()  
...
USER ALGORITHM:39, in rebalance  
order_target(stock, 0)  

Any idea where I went wrong? Thanks

2 responses

The problem isn't with order_target(stock, 0). The problem is really the line before.
The variable 'macd_raw' is a numpy array while 'macd_pre' is a float64 scaler. The logic condition in your if statement can't compare an array with a scaler.
Below is the original code...


    # Close position for the stock when the MACD signal is negative and we own shares.  
        if macd_raw** < macd_pre and current_position > 0 and data.can_trade(stock):  
            order_target(stock, 0)

        # Enter the position for the stock when the MACD signal is positive and  
        # our portfolio shares are 0.  
        elif macd_raw** > macd_pre and macd_pre < macd_raw[-3] and signal > signal_pre and current_position == 0 and data.can_trade(stock):  
            order_target_percent(stock, context.pct_per_stock)

Not sure what you are trying to do, but simply put an index on the 'macd_raw' array and it works (but maybe isn't what you wanted to do)? Now two scalers are being compared. Note this same error occurs in both conditions.

# Close position for the stock when the MACD signal is negative and we own shares.  
    if macd_raw[0] < macd_pre and current_position > 0 and data.can_trade(stock):  
        order_target(stock, 0)

    # Enter the position for the stock when the MACD signal is positive and  
    # our portfolio shares are 0.  
    elif macd_raw[0] > macd_pre and macd_pre < macd_raw[-3] and signal > signal_pre and current_position == 0 and data.can_trade(stock):  
        order_target_percent(stock, context.pct_per_stock)  

Hi Dan, thanks a lot for this. Now it's pretty clear to me...after you explained it. I just need to use the bracket notation [-n] and use the value I need from the n-th day. I forgot that I need extract the scalar. Thanks again.