Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to buy in the last minute of the last trading day of the week?

Dear all,

I am trying to implement an algorithm which buys/sells stocks in a weekly basis. Currently I am using tradingcalendar module to tell if the previous day is the last trading day of the week:

    trade_days = calendar.get_trading_days(pd.Timestamp('2000-01-01',tz="US/Eastern"),get_datetime('US/Eastern'))  
    if trade_days[-1].isocalendar()[1] <> trade_days[-2].isocalendar()[1]:  

If it is, then I will buy the stocks on the market open.

However, I would love to try to buy the stocks before the market close of the last trading day of the week. The problem is: Friday is not always the last trading day and sometimes the markets are not closed at 4pm.

Does anyone have any suggestions? Thanks a lot in advance!!

6 responses

Use schedule_function!

schedule_function(trade, date_rules.week_end(), time_rules.market_close())

Check out the Docs on schedule_function

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.

The market-close time rule does not accept a zero minutes keyword argument, only 1 or more. Thus it is impossible to schedule a function to run at market close, normally 4:00pm EST/EDT. While sending orders at market close might not be advisable, this is a good time to run and print daily or other periodic summaries.

This might be useful - Gary shared some elegant code to print summaries at a specific point of a backtest: https://www.quantopian.com/posts/print-run-summary

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.

Dear all,

Thanks for all your kind help!!

Hi everyone,

I ran the backtest and use the schedule_function as suggested by James. A confusion raised however: the transaction time seems not correct (please see the picture from this link.

The algorithm shall buy two minutes before close in the end of week, say, on 2015/8/7. It rebalanced on 2015/8/8 @3:39AM. Does anyone know the reason? (Or is it shown in my local time instead of the exchange time?)

Thanks in advance!!

Your hunch was right, the transaction time is displayed using your local time (based on your browser). If you change the settings to US/Eastern you will see the market time fills.