How to Automate a TradingView Strategy on MT4 and MT5
To automate a TradingView strategy, give every order in your Pine Script a TradingView Link
command with alert_message. Then create one alert on the strategy, with
{{strategy.order.alert_message}} as its message and your webhook URL. Each time the strategy fills an
order on a live bar, TradingView sends that command and our EA places the trade in MT4 or MT5. You
need a TradingView plan with webhooks (Essential or higher) and MetaTrader running with the EA.
This guide is for traders who already have a strategy that backtests the way they want. The code examples use Pine Script v6; they work the same way in v5. If you haven't connected MetaTrader yet, start with how to connect TradingView to MT5 or how to connect TradingView to MT4.
What do you need?
- A TradingView plan with webhook alerts: Essential, Plus, Premium, Expert or Ultimate. The free Basic plan has no webhooks. Turn on 2-factor authentication too: TradingView only sends webhooks from accounts that use it.
- A strategy you can edit. If the script is protected or invite-only, see what to do when you can't edit the script.
- MT4 or MT5 on a Windows PC or VPS that stays on, with the TradingView Link EA on a chart.
- A TradingView Link account. The free plan includes 5 alerts a month, which is enough to test this setup on a demo account.
How do strategy alerts work?
- An alert on a strategy fires when the strategy fills an order. One alert covers every entry and exit of that strategy on that chart.
- Alerts fire only for orders on live bars. The historical trades in your backtest never send anything.
- The alert saves a snapshot of the script and its inputs. If you edit the script or change an input later, delete the alert and create a new one.
- TradingView stops an alert that triggers more than 15 times in 3 minutes.
- In the alert message,
{{strategy.order.alert_message}}is replaced by thealert_messagetext of the order that just filled. That's how each order sends its own command.
Step 1: Add alert_message to every order
Each strategy.entry, strategy.close, strategy.exit and
strategy.order call accepts an alert_message. Put the TradingView Link
command for that order there, without the password. This example buys 0.1 lots when the fast EMA
crosses above the slow one, with a stop at the lowest low of the last 10 bars, and closes when it
crosses back:
//@version=6
strategy("EMA cross to MT5", overlay = true)
// Your broker's symbol name, which can differ from TradingView's
brokerSymbol = input.string("BTCUSD", "Broker symbol")
fast = ta.ema(close, 9)
slow = ta.ema(close, 21)
stopPrice = ta.lowest(low, 10)
if ta.crossover(fast, slow)
strategy.entry("Long", strategy.long, alert_message = "long " + brokerSymbol + " q=0.1 slp=" + str.tostring(stopPrice, format.mintick))
if ta.crossunder(fast, slow)
strategy.close("Long", alert_message = "flat " + brokerSymbol) - Use your broker's symbol name. It must match MetaTrader exactly, including any suffix, and it can differ from TradingView's ticker. That's why the example uses an input for it.
- Build the text in Pine. TradingView doesn't fill in placeholders such as
{{ticker}}insidealert_message, so usestr.tostring()for prices, as in the example. - Put the stop and target in the entry command with
sl/tp(pips) orslp/tpp(prices). The EA sets them on the order, so your broker holds them even if a later alert is delayed. - Give every order an alert_message. An order without one sends an empty line, which places nothing.
- Going short too? Send
shortfor the short entry. When a short arrives while longs are open, the EA closes the longs with the same magic number first, then opens the short. - Running more than one strategy on a symbol? Add a magic number such as
m=1001to every command of each strategy, includingflat.
The full command syntax, including partial closes, trailing stops and session filters, is in our documentation.
Step 2: Create one alert on the strategy
- Open the chart with the symbol and timeframe you want to trade, add the strategy and set its inputs.
- Open Create alert and pick your strategy as the condition. If the script also calls
alert(), choose Order fills only so that only orders are sent. - Replace the message with your webhook password and the placeholder, on two lines:
YourWebhookPassword
{{strategy.order.alert_message}} - Under notifications, turn on Webhook URL and paste the webhook URL from your TradingView Link dashboard.
- Check the expiration date. Essential and Plus alerts last at most 2 months; Premium and higher plans can keep them open-ended. Note the date so the strategy doesn't stop without warning.
- Create the alert.
Step 3: Test it on a demo account
Before you go live, run the strategy on a demo account for a few trades. To check MetaTrader on its own, send a command from the Send Signal box in your dashboard. Then wait for the strategy's next live order, or test on a low timeframe where orders come quickly. Compare each trade in MT4 or MT5 with the order on your TradingView chart, and read the EA's lines in the Experts tab. If nothing arrives, follow our TradingView to MT5 troubleshooting guide.
Why don't live trades match the backtest?
- Timing. By default a strategy calculates once per bar close, so the order and its alert come after the bar closes, and MetaTrader fills at the price at that moment. TradingView's setting to recalculate on every tick in real time sends orders sooner, but it can repaint: live orders can appear that the backtest doesn't show.
- Costs. Your broker's spread, commission, slippage and swaps differ from the strategy's settings.
- Size. The trade size comes from
q=in your command, not from the strategy's own quantity. - EA limits. The EA skips orders beyond its MaxTradePerSymbol and Pyramiding settings (10 each by default), even if the backtest took them.
- Delivery time. Trades reach MetaTrader in about 20 seconds on the free plan, about 3 seconds on Basic, about 1 second on Standard and under a second on Pro.
What if you can't edit the script?
If the strategy is protected or invite-only, you can't add alert_message. For a
strategy that always opens and closes whole positions, use this alert message instead:
YourWebhookPassword
{{strategy.market_position}} BTCUSD q=0.1 sl=300 tp=600 {{strategy.market_position}} becomes long, flat or short, the
strategy's position after the order. A long entry sends long, a reversal sends
short, and an exit sends flat, which closes the symbol's positions. It
breaks with partial exits: the position is still long, so the alert sends long again and
opens another trade.
Don't use {{strategy.order.action}} for this. It sends "buy" or "sell", and the EA only accepts
long, short, flat, cancel and modify, so
it logs "Invalid order type" and places nothing.
What are the most common mistakes?
| Mistake | What happens | Fix |
|---|---|---|
{{strategy.order.action}} in the message | "Invalid order type" in the Experts tab; no trade | Use alert_message |
| Script edited after the alert was created | The alert keeps running the old version | Delete the alert and create a new one |
| Alert expired | Trades stop on the expiry date | Create the alert again, or move to a plan with open-ended alerts |
| More than 15 orders in 3 minutes | TradingView stops the alert | Use a higher timeframe or send fewer orders |
| TradingView ticker instead of the broker's symbol | "Symbol not found" in the Experts tab | Use the exact name from MetaTrader's Market Watch |
| 2-factor authentication off | TradingView doesn't send the webhook | Turn on 2FA in your TradingView account |
Frequently asked questions
Can TradingView place trades on MT4 or MT5 by itself?
No. TradingView has no built-in link to MetaTrader. A strategy alert can send a webhook, and a bridge such as TradingView Link turns that webhook into an order that an Expert Advisor (EA) places in your MT4 or MT5 terminal.
Do I need to keep TradingView open?
No. TradingView alerts run on TradingView's servers, so you can close the chart and your browser. MT4 or MT5 must stay open with the EA running, which is why many traders use a Windows VPS.
Which TradingView plan do I need to automate a strategy?
A plan with webhook alerts: Essential, Plus, Premium, Expert or Ultimate. The free Basic plan has no webhooks. Alerts on Essential and Plus last at most 2 months, so you have to renew them; Premium and higher plans can keep alerts open-ended. TradingView also requires 2-factor authentication for webhooks.
Why did the strategy trade in the backtest but send no alert?
Strategy alerts fire only for orders filled on live bars, never for the historical trades in a backtest. If your alert is new, wait for the strategy's next live order.
Can I run several strategies on one MT5 account?
Yes. Create one alert per strategy and give each strategy its own magic number, such as m=1001, in every command, including flat. The EA then closes and reverses only the trades with that magic number.
Can I automate an indicator instead of a strategy?
Yes. Create one alert per action on the indicator's condition, each with a fixed message such as long BTCUSD q=0.1 or flat BTCUSD. A strategy is easier because one alert covers every order.
Sources
- PineConnector pricing
- TradingConnector pricing
- MetaConnector plans
- WebhookTrade pricing table
- TradeSgnl pricing
- SignalForge
- TradingView: strategy alerts (live orders only, the 15-triggers-in-3-minutes limit)
- TradingView: variables in alerts (what each placeholder returns)
- Pine Script: alerts
(
alert_messageand order fill events) - Pine Script: alerts FAQ (alerts keep a snapshot of the script)
- Pine Script: strategies (calculation on every tick and repainting)
- TradingView: introduction to alerts (alert lifetime by plan)
- TradingView: webhooks (2-factor authentication)
- TradingView pricing (webhooks by plan)
Checked in November 2026.
Related guides
- How to connect TradingView to MT5
- How to connect TradingView to MT4
- PineConnector alternative with MT4 support
- Best TradingView to MT5 bridges, compared
- Free TradingView to MT5 bridge: what you get for $0
- Free and cheapest TradingView to MT4 bridges
- TradingView to MT5 not working? How to fix it
- Prop-firm EA rules: FTMO, FundedNext and The5ers
- Full documentation: alert syntax, EA settings and troubleshooting
- FAQ: trials, billing and signal limits