Back to Community
Sending MKT and MOC orders before markets open on daily bars

Hello,

My strategy uses daily bars and enters each trade at the market open and exits at the close. It never holds a position overnight. Signals are calculated after the close of the current day and sent to my broker before the markets open on the next day. In real life with my IB account, I simply have to send the following two orders between today's close and tomorrow's open: "BUY 100 AAPL MKT" and "SELL 100 AAPL MOC".

How can I replicate that in Quantopian? Thank you.

5 responses

Hey Alexis,

Please see the attached algorithm and backtest. You will need to write and execute an algorithm in minute mode (as opposed to daily mode). The algorithm given here will allow you to set the security, position size, and times at which you would like to enter and exit trades.

Ryan

Clone Algorithm
40
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 52cb0ef826464107527f6586
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.
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.

Thank you for your help, Ryan. The market-on-close order seems fine, but I can't replicate the market-on-open order.

To be quite honest, I think it is a major flaw of the backtesting engine as trading on the open is highly important (at least for me!). Would it be possible to add the option to execute the order either on the open or the close of the next bar, instead of currently the close only? Thank you.

Hey Alexis,

Please see the attached code, which adds additional log information to the algorithm previously posted. I see that the algo performs both the buy and sell orders properly - hopefully you will obtain the same results in the log.

If you are looking to buy exactly at the open of the day (i.e. at 9:30 AM sharp), unfortunately Quantopian does not support that level of specificity yet, and you will only be able to place an order at the end of a bar.

Ryan

Here is the backtest.

Clone Algorithm
40
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 52cb26551b8f900756481104
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

Sorry Ryan if my message wasn't clear - the code you sent along worked well. Any idea when this feature could be implemented? Really looking forward to it!

Cheers!