Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to get the close price of the day before yesterday?

Hi all, I am new to this forum. So I wonder if anyone will help me with getting the close price of the day before yesterday, two days ago or three days ago?

Thanks.

4 responses

@Cheng Zhou First off, Welcome! While yesterday's close prices can be fetched in pipeline using the latest method, there isn't a built in way to fetch earlier data. So, one needs to write a small custom factor. Something like this

class Close_n_Days_Ago(CustomFactor):  
    # Define inputs  
    inputs = [USEquityPricing.close]  
    # Set window_length to whatever number of days to lookback  
    # Yesterdays data would be a window_length = 1  
    # The day before yesterday would be a window_length = 2 and so on.

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

# Then when defining one's pipeline  
close_day_before_yesterday = Close_n_Days_Ago(window_length=2)

There's a bit more on this approach, along with a notebook, in this post.

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 Dan. Your reply is very helpful. I notice the following code:

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

What if my need now is CloseChange_n_Day_Ago, rather than Close_n_Day_Ago. Given this change, should I just modify he output line as follow:

def compute(self, today, assets, out, close):
out[:] = close[0]-close[-1]

I assume the 0 in close[0] means today's index, while -1 mean the index of the previous day.

Am I right? thanks in advance.

@Cheng Zhou Yes, one can get the change in price in a similar fashion. However, the indexes are just reversed from what you assumed. The latest (ie yesterday) value is always the last value (ie [-1]) while the earliest value is the first value (ie [0]). So, the change in price would be

def compute(self, today, assets, out, close):  
    out[:] = close[-1]-close[0]  

Most of the time one wants the relative and not the absolute change in price so this may be more what is intended

def compute(self, today, assets, out, close):  
    out[:] = (close[-1]-close[0]) / close[0]  

However, if one does actually want the relative, or percent change, then either the built in Returns or PercentChange factors could be used. See the docs here and here. The two are effectively the same but Returns uses the close price as a default input while PercentChange is more generic and doesn't have any default inputs.

Thanks Dan.

I would like to get deeper into this. In order to get the absolute price change, I now know how to do it. What if I want to get the absolute volume change? Should I write another factor, in which I will have to replace 'close' with 'volume'?

By the same token, if I want to get the absolute change in the product of close price and volume, do I have to write one additional factor, in which 'close*volume' will replace either 'close' or 'volume'?

Do we have a more general solution (similar to the Java Interface class) rather than reinventing the wheel?

I am using some other language, in which its platform offers such a function:

REF(A, x), A can be anything (i.e. close, open, close/open), x represent the number of days from today's date.
For example, REF(close, 2) means close price 2 days ago, and REF(close*(close-open)/volume, 10) mean the value of the new factor (close*(close-open)/volume) but 10 days ago.

I don't know whether Quanntopian offers similar function which indeed is very convenient.

Thanks