Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
could not broadcast input array

When I try to add pricing and volume data to my pipeline I keep getting this error:
"could not broadcast input array from shape (2,8247) into shape (8247) "

class Vol(CustomFactor):  
    inputs = [USEquityPricing.volume]  
    window_length = 5  
    def compute(self, today, assets, out, volume):  
        out[:] = np.mean(volume)

class Open_Pricing(CustomFactor):  
    inputs = [USEquityPricing.open]  
    window_length = 5  
    def compute(self, today, assets, out, open_price):  
        out[:] = open_price    

class Close_Pricing(CustomFactor):  
    inputs = [USEquityPricing.close]  
    window_length = 5  
def compute(self, today, assets, out, close_price):  
        out[:] = close_price            

def make_pipeline():  
    pipe=Pipeline()  
    universe = QTradableStocksUS()  
    Vol_metric = Vol(window_length=5) #determines quantity of days  
    pipe.add(Vol_metric,'Vol_metric')  
    Open_price = Open_Pricing(window_length=2) #determines quantity of days  
    pipe.add(Open_price,'Open_price')  
    Close_price = Close_Pricing(window_length=2) #determines quantity of days  
    pipe.add(Close_price,'Close_price')  
    return pipe
3 responses

First, datasets are matrix with shape (number of observations, number of stocks), so you need to specify an axis along which you want to compute the mean:

class Vol(CustomFactor):  
    inputs = [USEquityPricing.volume]  
    window_length = 5  
    def compute(self, today, assets, out, volume):  
        out[:] = np.mean(volume, axis=0)  

Second, you dont need to determine twice the window length of a custom factor, either choose to pass the argument when you call the factor or directly in the customfactor

third your 'Close_Pricing' and 'Open_Pricing' customfactors return two values instead of 1. This is because the window length is set to 2. Recall from the tutorial that you have to use custom factors if you want to do some computations on the dataset. As you just want to get the latest open and close prices, this is not the case here. Use USEquityPricing.close.latest instead.

Here is the final code:

class Vol(CustomFactor):  
    inputs = [USEquityPricing.volume]  
    def compute(self, today, assets, out, volume):  
        out[:] = np.mean(volume, axis=0)      

def make_pipeline():  
    pipe=Pipeline()  
    universe = QTradableStocksUS()  
    Vol_metric = Vol(window_length=5) #determines quantity of days  
    pipe.add(Vol_metric,'Vol_metric')  
    Open_price = USEquityPricing.open.latest  
    pipe.add(Open_price,'Open_price')  
    Close_price = USEquityPricing.close.latest  
    pipe.add(Close_price,'Close_price')  
    return pipe  

Hope it helps !

I used the code and I'm still getting the same error
when I try to view it

result=run_pipeline(make_pipeline(),start_date='2014-05-05', end_date='2015-05-05')  
result  

Nvm it worked I just had to comment and uncomment them one by one, must be a jupyter notebook problem

thank you