Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How do I reference to a Close specifically X days ago?

Is there a function that I can reference to the closing price X days ago?

Thanks.
Brian

4 responses

To get the price N days ago one needs to write a small custom factor. But first, decide if you want the adjusted price (adjusted for splits and dividends) or the historical trading price. More than likely one wants the adjusted price for comparison to the current value. Then something like this will work.

class CloseOnN(CustomFactor):  
    # Define inputs  
    inputs = [USEquityPricing.close]  
    # Set window_length to one more  than the number of days to look back  
    # my_close_on_10 = CloseOnN(window_length = 10+1)  
    def compute(self, today, assets, out, close):  
        out[:] = close[0]

There are several posts here in the forums for how to do this. The first shows how to get the adjusted price (similar to above) the second post shows how to retrieve the actual unadjusted price. Check these out.
https://www.quantopian.com/posts/price-of-security-n-days-ago-from-within-pipeline
https://www.quantopian.com/posts/get-lagged-output-of-pipeline-custom-factor

Attached is a notebook with the above custom factor in action.

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.

Dan, Thank you very much for your help. I will work with your suggestions above.
Brian

Dan,

I have another question. I have now computed a factor ON, the overnight returns (i.e. Open / Close (t-1) ) using your suggestions to reference previous day's close.

Next I would like to create a factor where it references itself's previous value.

For example I want to create a factor called "ON TS", where the initial value is 1.0, and ON TS(t) = ON TS(t-1) * (1+ON). where ON is the factor I created in the notebook.

One can easily get the value of a factor N days ago with the help of a small custom factor. Something along these lines

# Create a factor which returns the previous value of a factor  
# Note that the input factor must be 'window_safe'

class PreviousValue(CustomFactor):  
    # No default inputs  
    # Set default window_length to 2 days which will then return the day before yesterday results  
    # One can set the window_length to return a value before that if desired  
    window_length = 2  
    def compute(self, today, assets, out, factor):  
        out[:] = factor[0]  

This will take any factor (or dataset) and return the previous value. By setting the window_length parameter it can return a value from any previous day. Note that the factor must be set window_safe = True. This implies that the factor will not be impacted by price adjustments.

See this post for more discussion https://www.quantopian.com/posts/get-lagged-output-of-pipeline-custom-factor .

I believe the attached notebook should do what you want. Good luck.