How to Close MT5 Trades from a TradingView Alert

Last updated · By the TradingView Link team
Short answer

Send flat and the symbol, such as flat EURUSD, under your webhook password. The EA closes every open EURUSD trade with the same magic number, longs and shorts. Add t=long or t=short to close one side, cq=50% to close half, or use cancel for pending orders. A new long also closes the symbol's shorts before it opens.

How do you close a trade with a TradingView alert?

Put flat and your broker's symbol on the line under your webhook password:

YourWebhookPassword
flat EURUSD

The EA closes every open EURUSD position with the same magic number, long and short, at the market price. Size and stop settings on the line, such as q= or sl=, are ignored, so flat EURUSD q=0.5 still closes everything, not 0.5 lots. If nothing is open, nothing happens.

Write the symbol exactly as your broker does, suffix included, such as EURUSD.a. A name MetaTrader doesn't know closes nothing, and the EA logs "Symbol not found". This is what each close command does:

Close commands and what they close
CommandWhat it closes
flat EURUSD All EURUSD positions without a magic number
flat EURUSD m=1001 All EURUSD positions tagged 1001
flat EURUSD t=long EURUSD longs only
flat EURUSD t=short EURUSD shorts only
flat EURUSD cq=50% Half of each EURUSD position
cancel EURUSD EURUSD pending orders, not positions

Without m=, a flat only closes trades opened without a magic number. That's the most common reason a close seems to do nothing, as our magic number guide explains.

Does a new entry close the opposite trade?

Yes. A long first closes the symbol's shorts with the same magic number, and a short closes the longs. Our documentation calls this Reverse Trade: with 3 BTCUSD longs open, a short signal closes all 3 and then opens the short. So a strategy that flips between long and short doesn't need a flat in between.

There's one catch. The EA closes the old trades only when it's about to open the new one. If it refuses the new trade, because it falls outside the s= trading hours or the r= price range, or goes over the MaxTradePerSymbol or Pyramiding limit, the old trades stay open as well. If the old position must close either way, put a flat on the line before the entry. The lines of one alert run in order:

YourWebhookPassword
flat EURUSD
short EURUSD q=0.1

How do you close only one side or part of a position?

Add t=long or t=short to close one side, which matters on hedging accounts, where both can be open at once. Add cq= with a percentage to close part of each position: flat EURUSD cq=50% halves every EURUSD trade, and flat EURUSD t=long cq=50% halves only the longs. Always write cq= with the % sign.

The EA rounds each closing size to your broker's lot step and never closes less than the minimum lot, so a 0.01-lot trade closes in full. Taking profit in stages, with two targets or a partial close, is covered in our stop loss and take profit guide.

How do you cancel pending orders?

flat doesn't touch pending orders. Use cancel:

YourWebhookPassword
cancel EURUSD

That deletes every EURUSD limit and stop order with the same magic number. cancel EURUSD t=long deletes only buy limits and buy stops, and t=short only sell limits and sell stops. To make a limit order cancel itself, give it an expiry in minutes when you send it, such as e=15, as the webhook guide shows.

How do you close trades from a Pine Script strategy?

Give every exit its own alert_message, and put {{strategy.order.alert_message}} under your password in the alert's message box:

if exitSignal
    strategy.close("Long", alert_message = "flat EURUSD m=1001")

strategy.exit() and strategy.close_all() take an alert_message the same way. TradingView's strategy docs note that strategy.close_all() "creates a new exit order only if the strategy has an open position", so it sends nothing while the strategy is flat.

If you can't edit the script, put {{strategy.market_position}} in the message box instead. TradingView's placeholder list says it returns "the current position of the strategy in string form", so an exit sends flat. It suits strategies that open and close whole positions, and the strategy guide explains why partial exits break it. Don't use {{strategy.order.action}}: it sends buy or sell, which the EA doesn't accept.

With an indicator, create an alert on its exit condition with a fixed message, such as flat EURUSD.

How do you close every trade on the account?

There's no single command for every symbol. Send one flat line per symbol, in one alert if you like:

YourWebhookPassword
flat EURUSD
flat XAUUSD
flat US500

Each line only closes trades with its own magic number, so add a line for each number you use, plus a plain flat for trades without one.

What happens if MetaTrader is offline when the alert arrives?

Our server holds the close for the Command Timeout set in your dashboard settings, in seconds. If the EA reconnects within that time, it runs the close then. The timeout starts at 0, and at 0 the command expires almost as soon as it arrives, so the trade stays open.

A longer timeout, such as 600 seconds, rescues closes sent during a short disconnect. It applies to entries too, though, and a late entry can open at a very different price, so pick a value that suits both.

Two things protect you either way. A stop loss sent with the entry, using sl= or slp=, sits on your broker's server, so it works while MetaTrader is closed. And a VPS keeps MetaTrader running: see do you need a VPS for TradingView to MT5.

Why didn't the alert close my trade?

The troubleshooting guide walks through each check, with the messages the EA writes in MetaTrader's Experts tab.

Test a close on a demo account
Connect TradingView to MT4 or MT5, open a demo trade and close it with a flat alert. 5 free alerts a month, no credit card.

Frequently asked questions

Does flat close pending orders?

No. flat closes open positions only. Use cancel with the symbol, such as cancel EURUSD, to delete pending limit and stop orders.

Can flat close a set number of lots?

No. flat ignores q= and closes every matching position in full. To close part of a position, use cq= with a percentage, such as flat EURUSD cq=50%.

Do I need to send flat before reversing a trade?

Usually not. A new long closes the symbol's shorts with the same magic number before it opens, and a short closes the longs. Send flat first only if the new trade might be refused, for example outside its trading hours.

Can one alert close trades on several symbols?

Yes. Put one flat line per symbol under your password. The lines run in order, and each one closes only its own symbol.

What happens to the stop loss and take profit when an alert closes the trade?

They go with it. In MetaTrader, a stop loss or take profit is always tied to an open position or a pending order, so closing the position removes them.

Will a close alert work while MetaTrader is closed?

Only if the EA reconnects within the Command Timeout set in your dashboard settings. At 0, the starting value, the close expires almost at once. A stop loss on the trade works either way, because it sits on your broker's server.

Does closing work the same way on MT4?

Yes. The MT4 and MT5 EAs both read flat, t=, cq=, m= and cancel, and close the same trades.

Sources

All checked November 2026. What the EA and our server do with each command comes from our own code and documentation.

Related guides