Enter and Exit at the Close in Amibroker and Tradestation (Daily Bars, OPG, and MOC Strategies)

How to enter and exit positions are important in trading – perhaps one of the most important aspects of trading. How do you enter and exit a trade? We all know commissions can eat into the profits, but slippage might be a much bigger cost. If you’re a long-term investor, slippage is a negligible problem but not for short-term traders.

How do you most efficiently enter and exit positions? Should you buy at the close – market-on-close –  (MOC)? Or is it better to buy on tomorrow’s open (OPG)? Or even put in a limit order? In this article, we briefly look at some aspects of these “problems”. We think the most rational approach (most of the time) is to enter at the close.

To buy/enter at the close (by not using an on-the-close order)

Most readers are probably aware that we usually enter a position at the close. This is, of course, kind of impossible because we don’t know the closing price before it has actually happened. If you want to use an at-the-close order you need to send it before the market closes, obviously.

MOC market-on-close order

On most exchanges, there are auctions that set the final price of the day. In the stock market, such an order is a MOC market-on-close order. Such an order needs to be sent a certain time before the close, in most places around 15 minutes. NYSE sets the limit at 15.45 while Nasdaq sets it at 15.50. The order is a market order.

Because we don’t know the closing price at that time, we can’t use such an order.

If it’s impossible to trade on the close, why do we still make strategies based on an entry at the close?


It’s of course easy to test strategies based on the closing prices, but in real trading, you need to program to allow for this. We strongly recommend automating your trading:

How to buy at the close without using an at-the-close order

You can do this in two ways. For example, if you’re using daily bars, you can enter by sending a market order just seconds before the close (less than ten seconds, for example) as long as the criteria are true. We don’t know for sure if the signal is there until after the fact, but our findings say the deviations are small during the last ten seconds. In Amibroker it’s a simple code based on seconds remaining of the bar.

Buy (or sell) after hours

The other option is to buy after the close in the after-hours market. If you’re trading very liquid instruments like the S&P 500 and Nasdaq 100, this is no problem. However, because of imbalances and fast-moving markets you might get a price that deviates more from the actual close than if you buy just five seconds before the close. If you trade stocks it might be huge spreads in the after-hours and thus impossible to execute.

When we did proprietary trading we once in a while had technical issues that made it impossible to exit before the close, and we can confirm it’s difficult to exit positions in after-hours in stocks without incurring huge costs due to slippage.

If you don’t want to use daily bars, you circumvent by changing the code to intraday data, for example, in minutes, and easily program it to send an order at whatever time you like. We use Amibroker and Tradestation, and this is done by coding sections in different timeframes.

How to buy at the close without using an at-the-close order in Amibroker

You don’t need to change to minute bars bar in Amibroker. Amibroker has a function that lets you count the remaining seconds of the bar and thus you can easily use daily bars. In our Amibroker course, we have a separate lesson that covers how to do this.

We believe it’s simpler in Amibroker than in Tradestation to do this:

How to buy at the close without using an at-the-close order in Tradestation

First, you need to count the minutes in the session you want to trade.

For example, if the trading day is from 0930 to 1600 you have 6.5 hours of trading. That is the same as 390 minutes of trading.

Second, you need to make a chart with 390 min bars instead of daily bars. Start with that.

When this is done you need to make a “session” using the session function in Tradestation. For this session, you can use 0930-1559 as a session. You can also use 0930-1558 or any other number that you like better.

When you apply this session to the chart you just made earlier you can trade on close live with Tradestation.

Related Reading: Best Amibroker Course

Enter orders manually

A trading platform might have the option of calculating daily bars in real-time, thus you can enter orders manually just a few seconds before the close.

To buy/enter the next day’s open

For practical reasons, this is the easiest option.

You scan your systems today after the close and figure out the buys and sells. Then you place orders for tomorrow’s open. This is a method that suits most traders, mostly for practical reasons.

(That said, often the results deteriorate compared to the trades at the close because you miss the frequent overnight gains.)

Enter the next day with a limit order

If you get a signal, you can enter a limit order the next day. However, you risk not getting a fill. However, please be aware that the low print frequently is a “fake” print and your backtest might not reflect real trades. Also, some low print trade thinly, leaving you with a partial fill or no fill at all.

For this method, you might want to consider using an OPG limit order. For example, if you get a signal and the ETF closes at 100, you can send an OPG order with a limit of 100.2. This means you are filled as long as the open is below this price. But you risk not getting a fill. This simulation is easy to backtest.

Opening order (OPG),  market-on-open (MOO), or limit-on-open (LOO) order

AN OPG order is a market or limit order on the open. The NYSE uses the specialist system, while the Nasdaq uses an “opening cross”. You can send a market order, or you can send a limit order. Moreover, you can send your OPG order to any exchange you want. Mostly, this is either sent to the NYSE or the Nasdaq.

The opening price is simply the price where the greatest number of buyers and sellers are met. This means many orders will not be filled, except for market orders. Up until 2010, it was very profitable to trade the OPG by using limit orders if you used some statistics and other tools. OPG trading strategies worked like a charm.

An OPG is canceled automatically when the stock starts trading.

To buy at open next day often involves a deterioration of the strategy

Now, this is not an option that is without problems. The first problem is that our research suggests most strategies perform worse compared to buy at today’s close. Not always, of course, but most of the time. This is especially true for mean-reverting strategies that are very short-term in nature.

The second issue is liquidity. The markets are deeper in the seconds before the close than in the first minutes of trading. Moreover, the market moves more in the first seconds of the trading day than in the last seconds.

Below is the last and first-minute bar of S&P 500 e-mini:

How To Enter And Exit Positions At The Close In Amibroker And Tradestation

However, you can send an open-only order in stocks (OPG). But again, in stocks, this might be unwise due to liquidity. OPG is a market order, and you never know what kind of price you get. Even if you trade just a small number of shares, your order might push the price some cents up or down. Over many years, this really adds up.

Exit/sell at the close or next open?

When you sell, close, or liquidate a position, the same arguments mentioned above apply.

Create your close print

One option could be to create your “artificial” close.

How can this be done? It’s easy: you use intraday data and create the close at 1558, for example (two minutes before the official close). Then you backtest the strategy using that close and see how it performs compared to the official close two minutes later. You can even make a close at 15:59:30 as well – thirty seconds before the official close.

The law of large numbers

However, whatever method you use to enter or close positions, we believe it’s paramount that you are consistent. Trading is all about the law of large numbers.

A backtest performed over two decades returns many trades. If you add multiple strategies, this normally adds up to thousands of trades. If you enter 5 minutes before the close or five seconds before the close, we believe the result will not differ enormously. You just need to be consistent and look at the big picture.

Conclusions about OPG and MOC orders:

We believe OPG orders (when you buy or sell at the open) and MOC orders (when you buy or sell at the open) are extremely efficient ways to buy or sell. You can even develop an OPG trading strategy or a MOC trading strategy based on the imbalances at the open and close. Both Amibroker and Tradestations let you relatively easily code such capabilities.


– Why are entering and exiting positions crucial in trading?

Entering and exiting positions are fundamental aspects of trading, influencing profitability. Efficient execution is key, considering factors like commissions and, especially for short-term traders, the impact of slippage.

– What is slippage, and why is it a concern for short-term traders?

Slippage refers to the difference between the expected price of a trade and the price at which the trade is executed. While a negligible concern for long-term investors, slippage can significantly impact the costs and profits of short-term traders.

– What is a market-on-close (MOC) order, and how does it work?

An MOC order is placed at the market’s closing price through an auction process. It must be submitted a specific time before the close, usually around 15 minutes, making it challenging to use in real-time trading.

Similar Posts