Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Error: operands could not be broadcast together with shapes

Hello,

I'm trying to create a MA over 15m bars and keep receiving the following error:

ValueError: operands could not be broadcast together with shapes (15,) (31,)
USER ALGORITHM:41, in handle_data
if context.MA1 >= context.MA2:

I added the code below. Could someone briefly explain tell me why I'm receiving this error? From what I've read it's related to matrices, but don't see why this function returns different dimensions.

def handle_data(context, data):  
    context.price = data.current(context.fut,'price')  
    closes_1 = data.history(context.fut,'price', 15*MA1_Periods*2,'1m')  
    closes_1_15m = closes_1.resample('15T', label='right').mean    

if context.MA1 >= context.MA2:  
    order(context.fut, 1, style=MarketOrder)  
5 responses

Sebastien,

Hope this will help:

# ---------------------------------------  
stock, ma1, ma2 = symbol('QQQ'), 5, 21  
# ---------------------------------------  
def initialize(context):  
    schedule_function(record_MAC, date_rules.every_day(), time_rules.market_open(minutes = 15))

def record_MAC(context, data):  
    mavg1 = data.history(stock, 'price', 15*ma1, '1m').resample('15T').last().mean()  
    mavg2 = data.history(stock, 'price', 15*ma2, '1m').resample('15T').last().mean()  
    record(mavg1 = mavg1, mavg2 = mavg2)  

Thank you Vlad

Hi Vlad,

Using this gave me another error when using talib:

TypeError: Argument 'real' has incorrect type (expected numpy.ndarray, got float)

fut = continuous_future('ES', offset=0, roll='volume', adjustment='mul')  
#Fast MA  
MA1 = data.history(fut, 'price', 15*MA1_Periods, '1m').resample('15T').last().mean()  
wmaA_1     = ta.WMA(MA1, timeperiod=MA1_Periods/2) * 2.0

Do you know why this is? When I try to use .values I get a security violation, and I can't seem to make modifications that will work. I need to do more reading

Sebastien,

You may try this way:

import talib

def initialize(context):  
    schedule_function(record_MAC, date_rules.every_day(), time_rules.market_open(minutes = 15))

def record_MAC(context, data):  
    fut = continuous_future('ES', offset = 0, roll = 'volume', adjustment = 'mul')  
    freq, ma1, ma2 = 15, 5, 21  
    bars = freq*(ma1 + ma2)

    prices_15 = data.history(fut, 'price', bars, '1m').resample('15T').last()  
    mavg1 = talib.SMA(prices_15, ma1)  
    mavg2 = talib.WMA(mavg1, ma2) 

    print (mavg1[-1], mavg2[-1])  
    record(mavg1 = mavg1[-1], mavg2 = mavg2[-1])  

Vlad, you're awesome. Thanks man.