Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Include Portfolio Positions in Pipeline?

Hello,

Is there any way to include all positions in the current portfolio in the Pipeline filter?

Thanks.
Wei

5 responses

Anyone has the same issue?

Because pipeline can only be initialized in initialize(), at the time there is nothing in the portfolio so you cannot specifically include any of your portfolio stocks in pipeline over time.

My problem is say on the first day, pipeline screened a list of stocks among which I bought some; then next day pipeline screened another list of stocks that do not include the stocks I bought the day before so I have no updated data for my portfolio stocks.

My workaround is to create another function to get the data I need for the portfolio stocks and merge with the pipeline data.

Anyone has a better or easier solution?
Thanks.

Hi Wei,

As you mentioned, defining a pipeline is done upon initialization of an algorithm, so you can't include position information in it. That being said, I think you can get what you're asking for. Instead of specifying a particular filter to be the screenon your pipeline, you can instead add that filter as another column on the pipeline, and select stocks to trade based on that column in before_trading_start. That way, you still get data for all stocks in before_trading_start, and you still have a way to pick the new stocks that you want to trade.

So instead of having something like this:

my_pipe = Pipeline(  
    columns={  
        'my_column': my_column,  
    },  
    screen=my_final_filter  
)

Add your screen as a column instead:

my_pipe = Pipeline(  
    columns={  
        'my_column': my_column,  
        'my_universe': my_final_filter  
    },  
)

And then in before_trading_start, do something like:

def before_trading_start(context, data):  
    context.pipeline_data = pipeline_output('my_pipeline')  
    context.my_universe = context.pipeline_data[context.pipeline_data['my_universe']]

    # This will have my_column results for all stocks since we removed the screen.  
    context.column_data = context.pipeline_data['my_column']  

Does this help?

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.

Thanks much Jamie for your idea and sample code.

I thought about this but I was concerned about the pipeline time.
I normally screen ~100 stocks out of at least 3000 and the list of ~100 stocks could change everyday.
So if I bring data for 3000 stocks vs. 100 through pipeline, how does this affect the time spend on pipeline?
I haven't tested it but will there be a big running time jump? Time is also a concern to me.

Thanks.
Wei

The final screen on pipeline doesn't affect computation time. It just determines which rows to output. All of the computations are done before that. If you have computationally expensive factors (like a regression term), then you can provide a mask to that term to help save time, but a screen won't help. So you should be ok to remove it!

That's good to know.
I tested it and it works that way.
Very helpful. Thanks again.