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). |
| The Terminals page | Trades → Close or Close part on a position (EA v2.31+), and TP1 - TP3 / breakeven / trailing set with Manage. |
| 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. |
| closelongvol / closeshortvol | A number of lots of the buys / sells, oldest first (EA v2.27+). |
| closelongpct / closeshortpct | A percentage of the buys / sells, e.g. 50 = half (EA v2.27+). |
| 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 alone cannot tell an exit from a new trade
A strategy sends the same "buy 6" to close a short as to open a long. With an older alert message, a buy that finds nothing left to close (because the trade was already closed) opens a NEW buy. Alerts made with the Alert Builder today also send the strategy's position, and then an exit can never open a trade - see the next section.Closing a strategy trade yourself
You can close a trade of your strategy yourself - by hand in MT5, with a close alert, or let its stop loss do it. Your TradingView strategy does not know about that, so it still thinks the trade is open. When the strategy later sends its own exit, TradeHookX sees from the alert that it is an exit, finds nothing open, and does nothing: no trade is opened. The Signal Log shows it as "Strategy exit (short 12 -> flat): nothing open on MT5 - it was already closed outside the strategy. Nothing was opened." Your followers' copies were already closed together with your trade.
| The strategy goes from ... to ... | TradeHookX does | Can it open a trade? |
|---|---|---|
| flat to short 6 (new trade) | Opens the sell, as usual | Yes - it is an entry |
| short 6 to short 12 (adding) | Opens 6 more, as usual | Yes - it is an entry |
| short 12 to short 6 (closing part) | Closes up to 6 of the sells | Never |
| short 6 to flat (closing all) | Closes every trade of that symbol and magic | Never |
| short 6 to long 6 (turning around) | Closes the sells, opens long 6 - the new size of the strategy | Only the new size |
- •This needs the alert message from the Alert Builder (it carries the position of the strategy before and after each order). Made your alert before October 24 2026? Open the template, click Get JSON and paste the new message into your TradingView alert.
- •Closing all also closes a stray trade of the same symbol and magic that the strategy does not know about, so your account matches the strategy again.
- •Trades you open by hand use magic 0, so a strategy exit never touches them.
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: new copies are blocked, but the leader's closes still close your copies.
When a close does not happen
| Signal Log says | Why | Fix |
|---|---|---|
| No matching positions found: you have ... open with magic ... | Your trade has another magic than the close alert, so it is still open. | Use the same magic on the entry and the close. Close the trade by hand if you want it closed now. |
| Nothing to close: no open position matched this alert | No open trade with that symbol, direction and magic. | Nothing to do if the trade was already closed. If not, check the symbol and Symbol Mapping. |
| Strategy exit (...): nothing open on MT5 | The strategy closed a trade you had already closed yourself (or by SL/TP, another alert, the broker). | Nothing to do - nothing was opened. |
| 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 kill switch blocks entries only - this was a new trade, not a close. | Turn the kill switch off if you want new trades again. |
| Market closed | The market for that symbol is closed (weekend, holiday). | Close when it opens. |