Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Count number of holdings in an Universe

Dear Experts,

This is a sample code from long short. After defining a universe, how do I know the number of stocks in the universe? I tried to replace TOTAL_POSITIONS with len(universe) but it is not working as shown below

Nuniverse1 = len(universe1) //2  
Nuniverse2 = len(universe2) //2  
    # longs1 = combined_factor.top( Nuniverse1, mask=universe1)  
    # longs2 = combined_factor.top(Nuniverse2, mask=universe2)  

Thank you
L

6 responses

I'd look at the output Pipeline. For example, I have:

    longs = combined_alpha.top(NUM_LONG_POSITIONS)  
    shorts = combined_alpha.bottom(NUM_SHORT_POSITIONS)

    long_short_screen = (longs | shorts)

    pipe = Pipeline(columns = {  
        'combined_alpha':combined_alpha,  
        'beta':beta,  
    },  
    screen = long_short_screen)  
    return pipe  
def before_trading_start(context, data):

    context.pipeline_data = pipeline_output('long_short_equity_template')  
    context.risk_loading_pipeline = pipeline_output('risk_loading_pipeline')  

Then do len(context.pipeline_data.combined_alpha).

This should work. If you can't figure it out, just let me know, and later I can provide full running algo code.

Hi

Is it possible to write the codes into "def make_pipeline()" ? This is because I will define a couple of universe in that section of codes. So it is easier to count the number of stocks within each universe and used it later to extract the top and bottom as shown in my code earlier.

Thanks for your help
L

Hi Grant,

Would you be able to show your example?

I have the following error, AttributeError: 'Series' object has no attribute 'top'.
I use a large number so that the mask can successfully select all the stocks within the universe to be counted.

def before_trading_start(context, data):

    context.pipeline_data = pipeline_output('long_short_equity_template')  
    context.risk_loading_pipeline = pipeline_output('risk_loading_pipeline')  
    Nuniverse1 = len(context.pipeline_data.combined_factor.top(100000000000, mask=universe1))  
    Nuniverse2 = len(context.pipeline_data.combined_factor.top(100000000000, mask=universe2))  

Hi Leo -

Not sure exactly what you are trying to do, but the attached code illustrates how to apply len to the output of Pipeline. See the record(n_stocks = len(combined_alpha)) line.

Note you can also use:

record(num_positions=len(context.portfolio.positions))  

I tried to choose the top and bottom stocks

I found a way. But I cant view the results to check whether it is extracting the stocks in the right group. Any ideas?

    # Build Filters representing the top and bottom baskets of stocks by our  
    # combined ranking system. We'll use these as our tradeable universe each  
    # day.  
    longs_1 = combined_factor.percentile_between(97, 100, mask=universe1)  
    log.info("Longs universe 1:")  
    log.info(context.longs_1 )  

Thanks for your prompt reply. Your algo looks impressive!!
L

Have a look at the attached backtest. I think it is extracting what you want. --Grant