Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Open and Close price of day when Backtesting using Minute Data

Is it possible to reliably retrieve the Open and Close price for a given day using the Minute data?

For example, would it be possible to write an algorithm which arbitrarily buys 100 shares long at the beginning of the day and sells the entire amount if the price goes over 1.01 of the purchase price or sells at the end of the day so nothing is ever held overnight.

2 responses

Hi Sheridan,

You could initialize a context variable in initialize and then store the value of the price once per day in handle_data? Does that get to your goal?

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.

Hello Sheridan,

I've thought a bit about this problem in the context of Quantopian. If you want to buy a security when the market opens on Day 1, you have to submit an order when the market closes on Day 0. This means that in the last minute of Day 0, your order needs to be submitted. It will then be fulfilled in the first minute of Day 1. Alternatively, you could submit the order during the first minute of Day 1, and it will be fulfilled in the second minute of Day 1. The latter option is probably the easiest to implement (see paragraph below for an explanation). The above assumes that the security has entries in the Quantopian database for every minute the market is open (not generally true, since some securities are thinly traded).

Getting out of the market at close is kinda tricky. Your sell order has to be submitted in the minute prior to the closing minute, to be fulfilled in the closing minute. So, ahead of time, you need to know the datetime stamp of the closing minute. You can do a study (in Quantopian) to determine the datetime stamps of all of the closing minutes for your securitie(s) over the backtest period (the same could be done for opening minutes). For liquid securities, I suspect that you'll just flag the days the market closes early, according to schedule. However for thinly traded securities (or for ones that have had trading suspended during the day), you'll need the datetime stamps to ensure that your trades are executed properly in the backtest (however, there is a potential look-ahead bias here, since you'd be using future unknown data in your backtest).

Make sense?

Grant