• Workshop
  • Books
  • Trading Tips
  • Resources
  • About
KJ Trading Systems
  • Workshop
  • Books
  • Trading Tips
  • Resources
  • About

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.

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).


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.

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:

Picture
​
​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.


Return To Articles

Picture
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.



Home            Strategy Workshop            Books           Subscribe            Resources           Calculators           Articles           Best Of Blog           About



Data, information, and material (“content”) is provided for informational and educational purposes only. This material neither is, nor should be construed as an offer, solicitation, or recommendation to buy or sell any securities. Any investment decisions made by the user through the use of such content is solely based on the users independent analysis taking into consideration your financial circumstances, investment objectives, and risk tolerance. Neither KJ Trading LLC (KJ Trading) nor any of its content providers shall be liable for any errors or for any actions taken in reliance thereon. By accessing the KJ Trading site, a user agrees not to redistribute the content found therein unless specifically authorized to do so. 

Individual performance depends upon each student’s unique skills, time commitment, and effort. Students sharing their stories have not been compensated for their testimonials. Student stories have not been independently verified by KJ Trading. Results may not be typical and individual results will vary. 

​U.S. Government Required Disclaimer - Commodity Futures Trading Commission. Futures and options trading has large potential rewards, but also large potential risk. You must be aware of the risks and be willing to accept them in order to invest in the futures and options markets. Don't trade with money you can't afford to lose. This website is neither a solicitation nor an offer to Buy/Sell futures or options. No representation is being made that any account will or is likely to achieve profits or losses similar to those discussed on this website. The past performance of any trading system or methodology is not necessarily indicative of future results. 

CFTC RULE 4.41 - HYPOTHETICAL OR SIMULATED PERFORMANCE RESULTS HAVE CERTAIN LIMITATIONS. UNLIKE AN ACTUAL PERFORMANCE RECORD, SIMULATED RESULTS DO NOT REPRESENT ACTUAL TRADING. ALSO, SINCE THE TRADES HAVE NOT BEEN EXECUTED, THE RESULTS MAY HAVE UNDER-OR-OVER COMPENSATED FOR THE IMPACT, IF ANY, OF CERTAIN MARKET FACTORS, SUCH AS LACK OF LIQUIDITY, SIMULATED TRADING PROGRAMS IN GENERAL ARE ALSO SUBJECT TO THE FACT THAT THEY ARE DESIGNED WITH THE BENEFIT OF HINDSIGHT. NO REPRESENTATION IS BEING MADE THAT ANY ACCOUNT WILL OR IS LIKELY TO ACHIEVE PROFIT OR LOSSES SIMILAR TO THOSE SHOWN. 

Testimonials appearing on this site are actually received via email submission or web survey comments. They are individual experiences, reflecting real life experiences of those who have used our products and/or services in some way or other. However, they are individual results and results do vary. We do not claim that they are typical results that consumers will generally achieve. The testimonials are not necessarily representative of all of those who will use our products and/or services. 
The testimonials displayed are given verbatim except for correction of grammatical or typing errors. Some have been shortened, meaning; not the whole message received by the testimony writer is displayed, when it seemed lengthy or the testimony in its entirety seemed irrelevant for the general public.

Email: kdavey at kjtradingsystems.com
(c) Copyright - KJ Trading Systems. All Rights Reserved Worldwide.
  • Workshop
  • Books
  • Trading Tips
  • Resources
  • About