Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Simple Question - Data.history - Pandas - Making A Pipeline

Hello,

I am trying to create a pipeline and find the percent change from 1, 3, and 6 days ago based on open and close data. Then, I want to use that data as a filter. The filter I would like is based on if the percent change is positive or negative. Additionally, I need help getting the base universe into my pipeline so the pipeline can narrow down stocks.

Hope this should be a quick fix,

Thank you!

2 responses

The first thing to do is to specify the data factors one wishes to use. In this case the close price. Do not use the data.history method. That will immediately fetch the data. Unless one is fetching minute data, generally use the pipeline construct to fetch data. It's faster and more robust. You may want to look at this post and also some of the links in it https://www.quantopian.com/posts/getting-started-struggling-with-pipelines .

To create a factor representing the price change over n days one could create a custom factor. However, there is a built-in factor just for this purpose called Returns (see the docs https://www.quantopian.com/help/#quantopian_pipeline_factors_Returns). The following would get the percent change in close price over 3 days (note the window_length is 1 more than the number of days)

    pct_change_3day_ago = Returns(window_length=4)

Finally, filters can be created with simple operators and then combined using the & and | operators like this

    # Construct a Filter.  
    day6_ago_pos = pct_change_6day_ago > 0  
    day3_ago_neg = pct_change_3day_ago < 0  
    day1_ago_neg = pct_change_1day_ago < 0

    # Set the pipeline screen to return securities which pass all three criteria  
    pipe.set_screen(day6_ago_pos & day3_ago_neg & day1_ago_neg)

Attached is the algo with these changes which should help you start.

Good luck.

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 for the help Dan! One more question if I just wanted to find just the return from just 3 days ago (not including 1 and 2 days ago within that three days just day 3), then would the line of code be:

pct_change_3day_ago = Returns(window_length=4) - Returns(window_length=3)

As this would subtract out the following 2 days worth of returns, correct?