Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Volume in research

Hi,

can someone tell me how can I get volume data in the research environment
or if there is an import for data.

4 responses

Hello,

It's in the built-in equity pricing dataset: USEquityPricing.volume

can you show me an example please

@ JJ, you will need to import the dataset below.

from quantopian.pipeline.data.builtin import USEquityPricing

# 1-month Mean Reversion  
class Mean_Reversion_1M(CustomFactor):  
    #1-Month Mean Reversion:  
    #1-month returns minus 12-month average of monthly returns over standard deviation  
    #of 12-month average of monthly returns.  
    #Notes:  
    #High value suggests momentum (short term)  
    #Equivalent to analysis of returns (12-month window)  
    inputs = [USEquityPricing.volume]  
    window_length = 252  
    def compute(self, today, assets, out, close):  
        ret_1M = (close[-1] - close[-21]) / close[-21]  
        ret_1Y_monthly = ((close[-1] - close[0]) / close[0]) / 12.  
        ret = (ret_1M - np.nanmean(ret_1Y_monthly)) / np.nanstd(ret_1Y_monthly)  
        out[:] = (ret)  

Ok, 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 !