Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to add existing positions to pipeline that are no longer included in your default mask?

Let's say I'm using the Q500US() as the screen for my pipeline. What if, at the end of the month, my algorithm opens a position in a security that is not included in the Q500US() at the start of the next month? Will this security automatically be included in my pipeline output in this new month, or do I manually have to add it to the pipeline? I'm not sure how I would add this security back to the pipeline, as make_pipeline() is only called once in initialize. Am I supposed to re-make and attach the pipeline in before_trading_start()? I haven't tried that, but it doesn't seem like the right way to go about things.

Any help is appreciated, thank you!

5 responses

The purpose of pipeline is to dynamically filter stocks based on changing conditions - you can't try to add stocks that shouldn't be in there inside the pipeline since the filters are defined once at the start of the program.

Instead, if you wanted to add some trading logic regarding the stock that you currently hold a position in but isn't selected by the pipeline you should loop over your context.portfolio.positions variable which automatically tracks your open positions. You should most likely do this inside your rebalance function.

Hope this helps :)!

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.

@Matthew,

Thank you for the reply, I appreciate it.

I do see how the pipeline is a dynamic tool. However, my concern is that if I open a position (based on pipeline output), and I decide what to do with this position (increase or close it out) based on statistics output from the pipeline (like a factor) what am I supposed to do if that instrument is dropped from the pipeline at the beginning of the next month? I see it as I'm more or less "flying blind" with my position. I have some logic in place to close a position out if I hold it for X number of days... but what about other strategies that might look to hold a position for a much longer time frame?

As an aside... my understanding is that it's not possible to set a pipeline screen that statically selects a set of instruments (please correct me if I am wrong). What if I am developing a strategy that I only want to test against SPY? I understand that the pipeline allows me to discover that this strategy might work well against more than SPY, but if the only solution is to calculate all of my factors for the entire 8000+ instruments on Quantopian, and then filter that output in before_trading_start(), I feel like that's a decent waste of computation, especially if your factors are expensive to calculate. Am I incorrect here, or is this the only way to get a pipeline to calculate factors for a fixed set of securities? (this last paragraph was fairly off-topic, I'm sorry)

Don't mean to whine about the platform, I think it's amazing. Just trying to learn! Thank you for the help.

I think I understand your problem. You want to use the pipeline to filter the stocks, but you also want to see data of stocks which don't pass the filter.

The very purpose of pipeline is stock selection is universe filtering, and conveniently not seeing stocks which don't pass the filter.

So if you wanted to see data from stocks that don't pass the filter, then don't filter the pipeline output

Instead, you can add a column in your pipeline which tracks the value of whether the security passes the filter (true or false), and not actually filter the pipeline output (which is set by screen).

Then, for stocks in your existing portfolio, you could check the value of the various factors that were computed in your pipeline, since they haven't been filtered out.

There is something similar to this in this tutorial, where he sets a column named longs and uses the truth value of that column to determine whether to go long on the stock. However, in your case, you wouldn't want to set the pipeline screen (longs | shorts), since you might want to see stocks that don't fit either criteria.

You can use pipeline to statically select instruments although it's not recommended. You can do something like

sym = morningstar.share_class_reference.symbol  
security_filter = sym.element_of(list of stocks you want)  

and screen on security_filter, which will compute pipeline values only for stocks in your list.

Matthew,

Yes! The way you re-worded the problem makes things so much clearer. Thank you! I can use the pipeline to select a universe while still getting data for that universe and any other instruments which may be existing in my portfolio.

I think I was also getting too caught up with trying to optimize pipeline output.

Regardless, thank you very much!

Glad to help :)