Closing Trades
Opening a trade is easy - closing it at the right moment, in the right amount, without touching the wrong trades, is where most mistakes happen. This page explains every way a trade gets closed.
In simple words
A trade can be closed by: the broker (stop loss / take profit), a close alert, an alert in the opposite direction, the EA's safety rules, the leader you copy, or you by hand. The magic number decides WHICH trades a close is allowed to touch.All the ways a trade is closed
| Who closes it | How |
|---|---|
| The broker | The price reaches the stop loss or take profit on the trade. Works even when the terminal is offline. |
| A close alert | closelong / closeshort / closeall - all of it or only some lots. |
| An opposite alert | A sell while you hold a buy, with execution mode Close Opposite or Flip. |
| Multiple TPs | The EA closes the TP1 / TP2 / TP3 lots when each level is reached. |
| Breakeven / trailing stop | They move the stop loss; the broker then closes the trade at that stop. |
| Terminal Settings | Max position drawdown closes a trade that is losing too much. |
| The leader you copy | When the leader closes, your copy is closed too (see below). |
| You | By hand in MT5 on your PC or phone - always possible, even with the kill switch on. |
Close alerts
| Alert | Closes |
|---|---|
| closelong | Buy trades on that symbol with the alert's magic. |
| closeshort | Sell trades on that symbol with the alert's magic. |
| closeall | Buy AND sell trades on that symbol with the alert's magic. Without a symbol: on every symbol. |
| cancel | Not a close: deletes waiting (pending) orders on that symbol with the alert's magic. |
{ "licenseKey": "TB-...", "action": "closelong", "symbol": "XAUUSD", "magic": 5151 }Closing only part - oldest first
Add "close_lots" (in the Alert Builder: Lots to close). The amount is a budget that is taken from your trades oldest first.
You hold 3 buys
0.02 (oldest), 0.03, 0.05
closelong 0.04
budget 0.04
Oldest closed
0.02 closed, 0.02 left
Next one trimmed
0.03 becomes 0.01
Result
0.01 + 0.05 still open
- •If what would be left of a trade is smaller than the broker's minimum lot, the whole trade is closed and the real closed amount is reported.
- •Leave close_lots out to close everything that matches.
The magic number decides what a close can touch
Every trade carries a magic number. A close (and Close Opposite / Flip) only touches trades with exactly the same magic.
| Trades open | Alert | What closes |
|---|---|---|
| Gold buy magic 5151 + gold buy magic 7000 | closelong XAUUSD magic 5151 | Only the 5151 trade |
| Gold buy magic 5151 | closelong XAUUSD (no magic) | Nothing - no magic means the EA default 20240202, which is not 5151 |
| Gold buy you clicked by hand (magic 0) | closelong XAUUSD (no magic) | Nothing - hand trades are never closed by alerts |
| Gold buy opened without a magic | closelong XAUUSD (no magic) | That trade |
Entry and close alerts of one strategy must share the magic
The most common mistake: the entry alert has magic 5151, the close alert has none - the close finds nothing and the Signal Log shows "No matching positions found". Use Duplicate in the Alert Builder to make the close alert from the entry alert.Execution modes: what an opposite signal does
Pine strategies close a buy by sending a SELL (and a sell by sending a BUY). The execution mode tells the EA what to do when such a signal meets an open trade on the same symbol (same magic).
| You hold | Signal | Close Opposite | Flip | MT Native |
|---|---|---|---|---|
| Buy 0.10 | Sell 0.10 | Closes the buy. Nothing new. You are flat. | Closes the buy, opens Sell 0.10. | Opens Sell 0.10 next to the buy. |
| Buy 0.10 | Sell 0.04 | Closes 0.04 of the buy. 0.06 buy left. | Closes the whole buy, opens Sell 0.04. | Opens Sell 0.04 next to the buy. |
| Buy 0.10 | Sell 0.20 | Closes the buy, opens Sell 0.10 (the rest). | Closes the buy, opens Sell 0.20. | Opens Sell 0.20 next to the buy. |
| Nothing | Sell 0.10 | Opens Sell 0.10. | Opens Sell 0.10. | Opens Sell 0.10. |
- •Close Opposite ("smart netting") is right for almost every Pine strategy: the strategy's own sizes decide how much is closed and whether a new trade opens.
- •Flip is for "always in the market" strategies that switch from long to short in one go.
- •MT Native is for hedging or pyramiding on purpose. With a normal strategy it stacks trades - the Alert Builder warns you.
Close Opposite cannot read your mind
If a sell arrives when nothing is left to close (for example because an earlier close already happened), Close Opposite simply opens a new sell. Strategies that scale out of a position in several steps are safer with explicit closelong / closeshort alerts using close_lots.Copied trades: how they are closed
When you follow a copy group, you never need a close alert of your own - the leader's closes are copied:
Leader closes
by alert, SL/TP or by hand
Leader EA reports
ticket + lots closed
TradeHookX finds
your copy of that trade
Your EA closes
exactly that ticket
- •Exact: your copy is closed by its own ticket number, so your other trades are never touched.
- •Partial: if the leader closes 40% of his trade, 40% of YOUR copy is closed (at least 0.01 lot).
- •Copies from an alerts group carry no stop loss or take profit of their own - they close when the leader's trade closes. Copies from a manual trading group can carry the leader's SL/TP as the same distance (your Copy SL / Copy TP settings).
- •Paused the group, stopped following, or were removed by the leader? Copied trades that are still open keep following the leader's closes - only NEW entries stop.
- •Closed your copy by hand already? The leader's close then finds nothing - that is fine.
- •Kill switch ON on your terminal: the leader's closes are blocked too - close those trades yourself.
When a close does not happen
| Signal Log says | Why | Fix |
|---|---|---|
| No matching positions found | No open trade with that symbol, direction and magic. | Check the magic of the entry and the close; check Symbol Mapping. |
| Signal expired | The terminal was offline for more than 55 seconds. | Close by hand; check why the terminal was offline. |
| Kill switch is ON ... | The terminal's kill switch blocks every signal. | Turn it off or close by hand. |
| Market closed | The market for that symbol is closed (weekend, holiday). | Close when it opens. |