Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How To Access Previous Day Open or Close??

In my algorithm I want to use yesterday's open or close. How do I access these events?

2 responses

Craig,

You can either just store the values in the context object or use a batch transform (see the docs: https://www.quantopian.com/help#ide-batch-transforms). Batch transforms let you define a trailing window, and then your code can operate on the window.

If you really just want the prior day, storing in context is simpler. You can just define any property you wish:

# do something like this in your initialize  
context.prices = {}

# and then this in your handle_data  
context.prices[sid(123)] = data[sid(123)].price  

hth,
fawce

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.

Craig,

Here's a batch transform example. Look over the code and then have a look at the log output:

2013-03-25handle_data:24DEBUG[ 93.91 155. ]  
2013-03-25handle_data:26DEBUG[[ 94.12 155.51] [ 93.91 155. ]]  
2013-03-26handle_data:24DEBUG[ 94.27 156.14]  
2013-03-26handle_data:26DEBUG[[ 93.91 155. ] [ 94.27 156.14]]  
2013-03-27handle_data:24DEBUG[ 94.32 156.13]  
2013-03-27handle_data:26DEBUG[[ 94.27 156.14] [ 94.32 156.13]]  
2013-03-28handle_data:24DEBUG[ 94.36 156.58]  
2013-03-28handle_data:26DEBUG[[ 94.32 156.13] [ 94.36 156.58]]  
End of logs.  

You can see that you have access to the prior day's open/close. Just change the window length (W_L) to capture more than just the current and prior days.

Grant