Back to Community
Base universe and masking

Hi

I am using the filters from the Pipeline tutorial, Lesson 11 to create a base universe. (I know there are other default universes I could use but I am trying to understand how masking really works.)

I thought I could pass the base universe to SimpleMovingAverage via the mask parameter to limit the stocks but it does not work.
It returns 8244 securities, with or without the mask parameter.

When I use screen=base_universe in the Pipeline, it reduces the number of securities but I don't understand why it does not work when using the mask parameter on a Factor directly. What am I missing?

Thanks

Loading notebook preview...
Notebook previews are currently unavailable.
1 response

Mike,

Good question. Masking a factor doesn't remove assets from Pipeline output. It only impacts the values for that factor .

From Quantopian's help page (my emphasis):

When passed a mask, a CustomFactor will only compute values over
stocks for which the Filter returns True. All other stocks for which
the Filter returned False will be filled with missing values.

I haven't checked, but if you were to look at your output and filter to only stocks not in universe, all of that particular Factor's values should be nan for those stocks.

To reduce your output to just universe, change

    return Pipeline(  
        columns={  
            'latest_close_price': latest_close,  
            'mean_close_10': mean_close_10,  
        },  
        #screen=base_universe,  
    )  

to

    return Pipeline(  
        columns={  
            'latest_close_price': latest_close,  
            'mean_close_10': mean_close_10,  
        },  
        screen=base_universe,  
    )  

Note that the screen parameter only applies to the output itself. It doesn't impact calculations of, say, the demean() method.

Hope that helps. Here's a little mnemonic device that might help you remember:

Nan nan nan nan, nan nan nan nan, set_screen, goodbye.