Back to Community
Doing pipeline calculations only on a number of stocks/assets

WARNING: beginner question :-)

I want to do pipeline calculations with custom factors, but only on a limited list of stocks/assets (SIDs).
So i don't want to work with the "universe" but with only a limited list of specific stocks.
Can this be done easily with pipelines ?

I was thinking of creating a custom Filter that would just return 1 if stock.name in a list [] and 0 when not in the list.
Then i would apply this filter to the universe.
Problem is i haven't found a way to get the sid or asset ticker inside a factor calculation (only OLHCV fields)

Any ideas ?

regards,
Geert

6 responses

Hold on.
If i start with

context.stocks = symbols('YHOO','CSCO')

ANSWER: apparently not...it doesn't work

is my pipeline then automatically limited to these stocks ? is it that simple ?

Hello all,

So it seems that i was able to solve this myself.
The solution is not really optimal, but it works.
It does calculations on the whole universe (whole pipeline), but then it filters the results to only look at certains SIDs and take the top X of those, ranked by a calculated factor.
This filtering is done daily in the before_trading_start procedure.

#ALL results of the pipeline are put in context.output  
context.output = pipeline_output('ranked_stocks').fillna(0)

    context.etf_list=    [sid(19662),  # XLY Consumer Discrectionary SPDR Fund  
                       sid(19656),  # XLF Financial SPDR Fund  
                       sid(19658),  # XLK Technology SPDR Fund  
                       sid(19655),  # XLE Energy SPDR Fund  
                       sid(19661),  # XLV Health Care SPRD Fund  
                       sid(19657),  # XLI Industrial SPDR Fund  
                       sid(19659),  # XLP Consumer Staples SPDR Fund  
                       sid(19654),  # XLB Materials SPDR Fund  
                       sid(19660),  
                       sid(8554)] # XLU Utilities SPRD Fund  

   #Filter only the stocks we are interested in  
    x = context.output[context.output.index.isin(context.etf_list)]  
    #sort according to a calculated factor (combo_rank in this case) and get top 10  
    context.stock_list = x.sort(['combo_rank'], ascending=True).iloc[:10]

    #update_universe(context.stock_list.index) - depreciated, use the following instead:  
    context.assets = [context.stock_list.index]  

Is there a way to do this in Research environment?

Matt,

Use pipelines in both the research environment and the trading/algo environment to get data and filter securities. You can filter for a specific list of securities in both environments using a custom filter. See below for a custom filter and attached notebook for how to use. The example sets the pipe screen to return only specified securities. If you have computation intensive factors that time out your algorithm, then you can also use this same filter as a mask for the factors.

Again, you can use the same custom filter and pipe in both environments.

class SidInList(CustomFilter):  
    """  
    Filter returns True for any SID included in parameter tuple passed at creation.  
    Usage: my_filter = SidInList(sid_list=(23911, 46631))  
    """  
    inputs = []  
    window_length = 1  
    params = ('sid_list',)

    def compute(self, today, assets, out, sid_list):  
        out[:] = np.in1d(assets, sid_list)

Hope that helps.

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

Dan Whitnable, you're the man!

I've needed something like this for ages! :)

THANKS!

Just noticed a built in filter that filters to a list of equities. Can be used for a single equity or a list of equities.

from quantopian.pipeline.filters import  StaticAssets

aapl = StaticAssets(symbols(['AAPL']))  
aapl_ibm = StaticAssets(symbols(['AAPL', 'IBM']))