What Are Market On Close (MOC) Orders - How to Use Them
UPDATED January 2023: Market On Close (MOC) orders allow investors and traders to enter orders near the closing price of a trading day. They are a useful tool for investors looking to time their trades more precisely or capitalize on market price swings at the end of the day. Learn more about MOC orders and how they can help your trading strategy.
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?
Market On Close orders are placed near the closing price of a trading day and allow investors to take advantage of intraday volatility or capitalize on market swings. MOC orders allow investors to buy stocks just below the closing price of an index and sell stocks just above it, minimizing the chances of paying more for a security than necessary. The order can also be used to help short-sellers achieve their desired entry point.
So no big surprise- 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.
So no big surprise- 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.
Types of Market On Close Orders Available
There are a few options available when it comes to Market On Close orders. Day orders are those that expire at the end of the trading day, meaning they won't be filled if the market moves too quickly. Limit orders allow you to set a specific price, so that your order is only executed if the asset reaches or crosses this price.
Meanwhile, Fill or Kill orders require your entire order to be fulfilled, or it won’t be filled at all. Finally, Immediate or Cancel orders must reach their price target immediately otherwise they will be canceled.
There is also an order called Trade At Settlement that you can use. You get the settlement price for the instrument for that day. I have never tried getting that to work with Tradestation algo automation though.
How to Use Market On Close Orders for Trading
Market On Close orders are simple to use and can provide excellent execution opportunities for traders. When placing a market on close order, start by selecting the direction you would like your order filled in. Choosing a buy side order means that the order is likely to be filled at or below the closing price of an asset, while sell side orders will be executed at or above it.
Next, decide how many shares you wish to purchase to ensure that your desired price is reached. Finally, enter the MOC order with your broker and wait for it to be executed near market close.
Benefits of Using Market on Close Orders for Trading Strategies
Market On Close orders (MOC Orders) benefit traders because they are able to execute trades quickly and efficiently. Since the orders must be executed near, or even at the market close, this allows systems to take advantage of sudden movements in markets during the closing period rather than waiting for a particular price throughout the day.
Furthermore, since these orders are able to fill multiple trades instead of just one, it can improve efficiency and reduce costs associated with trading.
Risks Associated with Market On Close Orders
Market On Close orders can be risky due to the fact that they are executed near the market close which is a period of increased volatility and uncertainty.
Furthermore, since these orders may fill multiple trades instead of just one, there is an increased risk of unintended trades or incorrect pricing associated with these orders. Thus, it is important to understand the risks associated with these orders before using them in your trading strategy.
The biggest threat is that your order does not get executed, and then you have to hold your position overnight. Many traders do not like this possibility!
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.