Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to closeout at EOD

Question for anyone listening. I have an algo that does intraday trading for a single security. Is there a simple if/then statement I can write that tells it to closeout at 10 minutes before the close?

6 responses

I am also interested in this. Thanks for posting, Michael

One way is to set the time that you want to closeout. In this example, it would be 10 minutes before close

from pytz import timezone  
if loc_dt.hour == 15 and loc_dt.minute == 50:  
     sell  

Note that you need to consider scheduled early closes (e.g. https://www.quantopian.com/posts/get-early-closes-function). There is more extensive guidance posted on the forum (although you may need to use google to find it if it is old). When I get the chance, I'll see if I can find the specific post I am recalling. --Grant

Thanks Mike and Grant.

Mike: Quantopian is returning a build error when I try to insert your code. Note that I've imported timezone from pytz (tried including it in initialize and handle_data).

It gives me this error: "Runtime exception: NameError: global name 'loc_dt' is not defined"
And it gives me this warning: "Undefined name 'loc_dt'"

For the sake of debugging, I've removed everything else from my algo and have gotten the same error from the following:

def initialize(context):  
    import datetime  
    from pytz import timezone  
    context.aapl = sid(24)


def handle_data(context, data):  
    if loc_dt.hour == 14 and loc_dt.minute > 50:  
        order_target_value(context.aapl,0)  

Thanks for the helps guys.

Here's the reference I'd recalled, posted by Seong Lee (I think he was an intern with Quantopian):

https://www.quantopian.com/posts/market-on-close-orders

Enjoy!

Grant

Hi Michael,

Here is simple code that will close the position at 3:50PM. Grant posted a great link to a previous post that has more thorough coverage and it includes early market closes. In that thread you'll notice that Seong mentioned the NYSE accepts MOC orders until 3:45pm EST.

from pytz import timezone  
import datetime


def initialize(context):  
    context.stock = sid(24)

# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    #set timezone to EST  
    exchange_time = get_datetime().astimezone(timezone('US/Eastern'))  
    #Sell position at 3:50PM  
    if exchange_time.hour==15 and exchange_time.minute == 50:  
        order_target_percent(context.stock,0)  
        log.info("Sold entire position at %s" % (exchange_time))  

Cheers,
Alisa

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.