Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Buy/Sell on Open/Close

I am looking for a better way to handle buy and sell orders on Day Open/Close
So far I use an if statements:

 if( data[cur_sid].datetime.hour==13 and data[cur_sid].datetime.minute==31):

 if( data[cur_sid].datetime.hour==19 and data[cur_sid].datetime.minute==58):  

But this does not work properly in a backtest or LiveTrading due to daylight savings., Any ideas?

Daylight Savings Time bugfix: Right now when you trade, the first
minute bar is marked 14:31 UTC, every day. Unfortunately, that's not
correct - during daylight savings time, the first bar should be 13:31
UTC. That bug will be corrected. The behavior of "sell at close" and
similar strategies will need to be updated after we fix the bug. This
fix will impact some of the end-of-day returns and risk metric
calculations; sometimes the day was "ending" prematurely because of
DST.

4 responses

Unfortunately, the only way to do that right now is to ALSO use the date and calculate whether or not you're in DST. A giant pain, for sure. Plus there are trading holidays and half-days.

We've been talking about ways to make a "day open" and "day close" reference that is easy to use. We won't get to it in the next couple weeks, but I'll see if I can get it this summer.

Dan

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.

@Sanz, you can use the 'pytz' module to do the timezone conversion for you. It is daylight time aware.

import pytz  
EST = pytz.timezone('US/Eastern')  
ESTdate = get_datetime().astimezone(EST)  

Note that market open happens at 9:31 which is the first full minute bar after open. Market close happens whenever trading halts that day (usually 4pm).

Here is a list of all the dates (so far, in the Quantopian dataset) the market has opened late or closed early. Use this with caution as you should also be able to handle emergency closings of the market. But most of these dates would have been announced in advance.

These dates are in EST timezone. Source code attached via backtest.

Late open:

2002-09-11 11:01  
2007-05-11 09:43

Early close:

2002-07-05 13:01  
2002-11-29 13:01  
2002-12-24 13:01  
2003-07-03 13:15  
2003-11-28 13:01  
2003-12-24 13:01  
2003-12-26 13:01  
2004-11-26 13:01  
2005-11-25 13:01  
2006-07-03 13:01  
2006-11-24 13:01  
2007-07-03 13:01  
2007-11-23 13:01  
2007-12-24 13:43  
2008-07-03 13:01  
2008-11-28 13:38  
2008-12-24 13:01  
2009-11-27 14:56  
2009-12-24 13:17  
2010-11-26 13:06  
2011-11-25 13:16  
2012-07-03 13:01  
2012-11-23 13:01  
2012-12-24 13:01  

Thank you Dennis. This is very helpful.