Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Pipeline set_screen by sid?

Hi All,

I am trying to do a very simple thing which is to filter pipeline through their sid and everytime I try, I get this error:

TypeError: zipline.pipeline.pipeline.set_screen() expected a value of type zipline.pipeline.filters.filter.Filter for argument 'screen', but got bool instead.

How do I code this correctly?

pipe.set_screen('sid' == '24')  
8 responses

For the same task I used a custom factor returning the sid and created a filter with it.
Actually I hoped something like that would be builtin but I couldn't find it in the single-mega-ultra-combined-help-tutorial-reference page.

Here's what I mean, the only comment is the bit you asked for, the trick is that the asset parameter to compute() holds all the sids for the pipeline securities:

from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline, CustomFactor

class SidFactor(CustomFactor):  
    inputs = []  
    window_length = 1

    def compute(self, today, assets, out):  
        out[:] = assets

def initialize(context):  
    sid = SidFactor()

    # This is the filter you want  
    my_sid_filter = sid.eq(24)

    pipe = Pipeline()  
    pipe.add(sid, 'sid')  
    pipe.set_screen(my_sid_filter)  
    attach_pipeline(pipe, 'just_my_sid')     

def handle_data(context, data):  
    print(pipeline_output('just_my_sid'))

Thank you so much Andrea, it worked like a charm!
Is it possible to reference it to a list of sids instead of just one?
I'm trying to filter it to show today's open positions.

Try this (I am not sure how fast it can be though):

class SidFactor(CustomFactor):  
    inputs = []  
    window_length = 1

    def compute(self, today, assets, out):  
        out[:] = assets

def create_sid_screen(sid_list):  
    sid_factor = SidFactor()  
    sid_screen = None  
    for s in sid_list:  
        sid_screen = sid_factor.eq(s) if (sid_screen is None) else (sid_screen | sid_factor.eq(s))  
    return sid_screen

sid_list = [1,2,3,4,,5,6,7,8,9....,666]

sid_screen = create_sid_screen(sid_list)

pipe.set_screen(sid_screen)  

Months ago I asked to introduce a feature complementary to this one in Pipeline but no answer yet.

A hack because Quantopian doesn't allow passing arguments to compute(), but it works. There's a note on the numpy in1d documentation that says something along the lines of 'in1d is slow. Use Pandas instead.' But that's left as an exercise for the reader.

from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline, CustomFactor  
import numpy as np

class SidFactor(CustomFactor):  
    inputs = []  
    window_length = 1  
    sids = []

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

def initialize(context):  
    sid = SidFactor()  
    sid.sids = (19662, 40107)

    pipe = Pipeline(  
        columns={ 'sid': sid },  
        screen=sid.eq(1))

    attach_pipeline(pipe, 'pipe')     

def handle_data(context, data):  
    print(pipeline_output('pipe'))  

Hey guys, these are great workarounds! I've submitted an internal feature request for a Pipeline filter for a list of SIDs.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Hi, Zipline has a Pipeline filter for a list of static assets called: StaticAssets in: zipline.pipeline.filters.filter.
I am writing this over half a year after the last post, so I am not sure if StaticAssets existed before this thread or was a product of this thread.

Anyways you can go from a list of sids or tickers to assets then use this filter, it looks like it is available on Quantopian under quantopian.pipeline.filters.

Indeed. It appears that StaticAssets was added to zipline on Nov 1. Much cleaner than the hacks that luca and I did.

btw, syntax is this:

from quantopian.pipeline.filters import  StaticAssets

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

Thanks to let everybody knows, that is a good addition to pipeline indeed.