Pipeline average volume for stocks in universe

In pipeline how can I have a factor which is equal to the average of the volume of all stocks in the universe for a given date.

So if universe is msft and appl and the traded volume is 1000 and 3000 respectively for 1/1/2017, then the factor vale would be 2000 and 2000 for both msft and apply on 1/1/2017.

Saleem

3 responses

Maybe something like the custom factor in the the attached notebook?

6

Please can you explain this --

average_volume = np.nanmean(volume[-1], axis = 0)

I understand np = numpy. nanmean means compute average but skip NANs. The index of -1 on volume and axis is what I don't understand.

A related question is what if my universe was three stocks AAPL, MSFT, QQQ. And I wanted a factor which is the volume of QQQ.

So AAPL volume is 1000, MSFT is 2000 and QQQ is 10,000 on a given date. The value of the factor would be 10,000 for all three on that date.

@Saleem

To understand custom factors one first needs to understand what the parameters to the 'compute' method are - especially the inputs. Look this over from the Q documentation https://www.quantopian.com/help#quantopian_pipeline_CustomFactor . Basically the inputs are 2D numpy arrays. The rows (axis 0) represent trading days. The last row is the last trading day. The number of rows is determined by the value of 'window_length'. The columns (axis 1) represents each of the securities.

'volume[-1]' returns the last row of data from the numpy array 'volume'. Maybe look at some tutorials on python and numpy array indexing and slicing like https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html

The following works too without an index. However, I included the index [-1] in case the window_length was ever changed from 1. In that case this will average across all stocks AND all days.

class Average_Universe_Volume(CustomFactor):
'''
Returns the average volume of the securities passed in mask (or ALL securities if no mask is provided)
The average is returned as the same factor value for all securities
'''
inputs = [USEquityPricing.volume]
window_length = 1
def compute(self, today, assets, out, volume):
# get the average of the latest volumes
average_volume = np.nanmean(volume)
out[:] = average_volume


To return the volume for QQQ as a factor for all stocks, one would do something like this.

class QQQ_Volume(CustomFactor):
'''
This is a factor to return the volume of QQQ for all securities.
Just make sure that QQQ is in the universe of assets passed to the factor (ie don't mask QQQ out)
'''
inputs = [USEquityPricing.volume]
window_length = 1
def compute(self, today, assets, out, volume):
qqq_index = np.where(assets == 19920)    # 19920 is the SID of QQQ
out[:] = close[-1, qqq_index]