How To Enter And Exit Positions At The Close In Amibroker And Tradestation (Daily Bars)

Last Updated on July 8, 2021 by Oddmund Groette

One of the most important aspects of trading is the buy and exit prices. How do you enter and exit a trade? We all know commissions can eat into the profits, but the slippage is normally 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? Or is it better to buy on tomorrow’s open? 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 order – market on close. 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.

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 traded prop 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 and don’t “waste” time making it more complicated like in Tradestation:

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-1557 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 are able to trade on close live with Tradestation.

To buy/enter 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.

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.

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:

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.

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.

 

Disclosure: We are not financial advisors. Please do your own due diligence and investment research or consult a financial professional. All articles are our opinion – they are not suggestions to buy or sell any securities.