Market On Close (MOC) Orders
When backtesting algo trading strategies, what kind of market orders can you use?
For most trading platforms, market orders are the “go to” order. While you can use stop orders or limit orders in most platforms, simple market orders are the many times best way to enter or exit the market. The market orders work in stocks, futures and forex.
Of course, there is a cost associated with such convenience. Since prices are normally quoted as two numbers, the bid and ask price, you pay the higher (ask) price when you buy, and you receive the lower (bid) price when you sell. This is commonly called the “spread.” I always look at it as the cost of convenience for immediately getting in or out of the market.
While market orders can be placed anytime during the day, most trading algos execute market orders on the open or close of a bar.
Of course, there is a cost associated with such convenience. Since prices are normally quoted as two numbers, the bid and ask price, you pay the higher (ask) price when you buy, and you receive the lower (bid) price when you sell. This is commonly called the “spread.” I always look at it as the cost of convenience for immediately getting in or out of the market.
While market orders can be placed anytime during the day, most trading algos execute market orders on the open or close of a bar.
What is a market on open (MOO) order?
A market on open order is as simple as it sounds. It executes an order as soon as the market opens (or when the next bar) opens. Typically, a market on open order will look something like this:
Buy next bar at open;
Which is equivalent (in Tradestation) to
Buy next bar at market;
This order will execute when the next bar opens, whether it is the beginning of the day, or the beginning of the next bar in the day (for intraday bars).
Buy next bar at open;
Which is equivalent (in Tradestation) to
Buy next bar at market;
This order will execute when the next bar opens, whether it is the beginning of the day, or the beginning of the next bar in the day (for intraday bars).
What is a market on close (MOC) order?
As you may have already guessed, a market on close order executes when a bar closes.
In Tradestation, you can submit the order
Buy the bar at close;
or the command
SetExitOnClose;
This order will execute as soon as the bar is closed. Unfortunately, there is a slight problem with this type of order. Since it waits until the bar is closed before the order is sent, the order will fail if the bar close is also the close of the day. What happens is the bar is closed, the trading platform recognizes “bar is closed, time for me to send the market on close order” and the platform then sends the order. The order reaches the trading exchange, but since the exchange is closed, the order gets rejected!
I’ll discuss how to get around this dilemma a bit later.
What are issues with market on open orders?
There are very few issues with market orders in practical algo trading. 2 issues I have come across:
1. Sometimes, the trading platform sends the market on open order to the exchange a split second too early. Or, in rare times, the exchange does not open precisely on time. In either event, the scenario is the same: the market on open order is sent to a closed exchange, and the exchange rejects the order, stating in effect “the market is closed, therefore market orders are invalid.”
This is annoying problem to be sure, but usually it can be fixed in the algo strategy code. In my Strategy Factory workshop, I actually reveal 2 code “fixes” to get around this situation.
2. In extreme markets, the market will be in a “locked limit” condition. This used to be fairly common in the agricultural futures markets. When there is a lock limit up, you cannot buy at the market. You could sell at the market, but that would be throwing money away, since the price will likely be much higher once the market opens. You will get a rejected order if you try to buy a locked limit up condition.
Again, this issue could be trapped and identified in code. Practically speaking, it is so infrequent that it is not worth spending a lot of time to fix.
What are issues with market on close orders?
For algo traders, market on close MOC orders are a bit thornier of a problem. If you are using X minute bars in your algo, market on close orders work just fine for every bar except the last bar of the day. If you use daily bars, you have the same issue – MOC orders will not work.
The problem, as described earlier, is that the MOC order gets sent to the exchange after the market is closed!
I have seen this issue in live trading with both Multicharts and Tradestation. It may also occur with other platforms, I cannot say (I’ll leave it to the reader to check). I have heard that Amibroker does not have this issue, although I have never personally tried it.
The sneaky thing about this market on close issue is that in backtest, both of these statements work just fine:
Sell this bar at close;
Setexitonclose;
Pro tip: If someone tries to sell you a trading system with either of those two lines of code in it, and doesn’t explain or account for how they might not work in real trading, RUN!!! That person is not a real trader. REAL traders know that these orders do not work in the live markets.
The problem, as described earlier, is that the MOC order gets sent to the exchange after the market is closed!
I have seen this issue in live trading with both Multicharts and Tradestation. It may also occur with other platforms, I cannot say (I’ll leave it to the reader to check). I have heard that Amibroker does not have this issue, although I have never personally tried it.
The sneaky thing about this market on close issue is that in backtest, both of these statements work just fine:
Sell this bar at close;
Setexitonclose;
Pro tip: If someone tries to sell you a trading system with either of those two lines of code in it, and doesn’t explain or account for how they might not work in real trading, RUN!!! That person is not a real trader. REAL traders know that these orders do not work in the live markets.
How Can I Solve This MOC Order Problem?
If you are creating algo trading strategies, ideally you want to create 1 strategy that works correctly in backtest, and then works correctly in live trading. Here is a chart summarizing the problem:
The question is how do we change the “X” marks to checkmarks? Here are a few real trader tips for users of Tradestation and Multicharts.
As you can see, backtesting works in all situations. It is the live trading you have to account for. Here is how to correct the issues:
X (1) - There are two ways to correct this MOC issue. First, you can create a custom session, one that ends a few minutes before the exchange closes. Then, when the last bar of the day closes, the MOC order will be sent, and since the exchange is still open, the order will execute.
The second method to solve this case is to simply exit on the open of the last bar of the day. Use a time based exit like:
If time= XXXX then sell next bar at open;
Where XXXX is the time of the second to last bar of the day.
X(2) – The only way to use the setexitonclose keyword correctly is to use a custom session as described above.
X(3) and X(4) – Since you cannot create custom sessions for Daily bars (at least in Tradestation), the only alternative is to convert Daily bars to 1440 minute bars. This might dramatically impact your strategy results, though.
The question is how do we change the “X” marks to checkmarks? Here are a few real trader tips for users of Tradestation and Multicharts.
As you can see, backtesting works in all situations. It is the live trading you have to account for. Here is how to correct the issues:
X (1) - There are two ways to correct this MOC issue. First, you can create a custom session, one that ends a few minutes before the exchange closes. Then, when the last bar of the day closes, the MOC order will be sent, and since the exchange is still open, the order will execute.
The second method to solve this case is to simply exit on the open of the last bar of the day. Use a time based exit like:
If time= XXXX then sell next bar at open;
Where XXXX is the time of the second to last bar of the day.
X(2) – The only way to use the setexitonclose keyword correctly is to use a custom session as described above.
X(3) and X(4) – Since you cannot create custom sessions for Daily bars (at least in Tradestation), the only alternative is to convert Daily bars to 1440 minute bars. This might dramatically impact your strategy results, though.
Summing Up Market On Close Orders
As you can see, there are some practical issues with Market On Close (MOC) orders in backtesting and in live trading. Don’t assume that something that works in backtest will behave exactly the same in live trading. The SetExitOnClose keyword in Tradestation and MultiCharts is a good example of that.
Thankfully, with a little preparation and foresight, you can use the guidance in this article to successfully execute you MOC orders.
Thankfully, with a little preparation and foresight, you can use the guidance in this article to successfully execute you MOC orders.

About Author: Kevin Davey is an award winning private futures, forex and commodities trader. He has been trading for over 25 years.Three consecutive years, Kevin achieved over 100% annual returns in a real time, real money, year long trading contest, finishing in first or second place each of those years.
Kevin is the author of the highly acclaimed algorithmic trading book "Building Algorithmic Trading Systems: A Trader's Journey From Data Mining to Monte Carlo Simulation to Live Trading" (Wiley 2014). Kevin provides a wealth of trading information at his website: http://www.kjtradingsystems.com
Copyright, Kevin Davey and KJ Trading Systems. All Rights Reserved. Reprint of above article is permitted, as long as the About The Author information is included.
Kevin is the author of the highly acclaimed algorithmic trading book "Building Algorithmic Trading Systems: A Trader's Journey From Data Mining to Monte Carlo Simulation to Live Trading" (Wiley 2014). Kevin provides a wealth of trading information at his website: http://www.kjtradingsystems.com
Copyright, Kevin Davey and KJ Trading Systems. All Rights Reserved. Reprint of above article is permitted, as long as the About The Author information is included.