TradingView Pine Script Strategy Alerts Guide: Fire Webhooks to MT5

Pine Script strategy alerts firing on a TradingView chart

Getting a Pine Script strategy to actually place trades on MT5 comes down to one thing: firing the right alert with the right message at the right moment. This guide breaks down the two alert mechanisms in Pine — alert() and strategy alert_message — and shows the exact setup that reliably executes through a webhook copier.

alert() vs Strategy alert_message

Pine Script gives you two ways to fire an alert from a strategy. Choosing the right one is the difference between trades that copy cleanly and alerts that silently never fire.

MethodWhereBest for
alert()Any condition in codeCustom logic, partial closes, multi-step exits
alert_messageOn strategy.entry / exit / closeTying the alert directly to an order fill

Recommended: use alert_message on each strategy order and create the TradingView alert with the "Order fills only" condition. The alert is bound to the actual order event, so what you backtest is what gets sent to MT5.

Method 1 — alert_message on Strategy Orders

Attach a JSON payload to every order. When the order executes, TradingView posts that exact message to your webhook. The format follows our TradingView alert JSON guide.

//@version=5
strategy("Breakout Strategy", overlay=true, calc_on_every_tick=false)

longCond  = ta.crossover(close, ta.highest(high, 20)[1])
exitCond  = ta.crossunder(close, ta.lowest(low, 10)[1])

if longCond
    strategy.entry("Long", strategy.long,
         alert_message='{"action":"buy","symbol":"EURUSD","lot":0.10,"sl":50,"tp":100}')

if exitCond
    strategy.close("Long",
         alert_message='{"action":"close","symbol":"EURUSD"}')

Then in TradingView: open Create Alert, set Condition → your strategy, choose "Order fills only", tick Webhook URL, and paste your copier's URL. Leave the message box as the default {{strategy.order.alert_message}} so TradingView substitutes the JSON you defined.

Method 2 — Manual alert() Calls

When you need full control — for example to scale out with partial closes at custom levels — call alert() directly:

//@version=5
strategy("Custom Exits", overlay=true)

if longCond
    strategy.entry("Long", strategy.long)
    alert('{"action":"buy","symbol":"XAUUSD","lot":0.05,"sl":300,"tp":900}',
         alert.freq_once_per_bar_close)

if high >= strategy.position_avg_price * 1.01 and strategy.position_size > 0
    alert('{"action":"close","symbol":"XAUUSD","percent":50}',
         alert.freq_once_per_bar_close)

For alert() calls, create the TradingView alert with the "alert() function calls only" condition. The message box is ignored — Pine supplies the text.

Using Placeholders for Dynamic Values

Hard-coding the symbol and lot works, but you can make a strategy reusable with placeholders that TradingView fills in at fire time:

PlaceholderResolves to
{{ticker}}Chart symbol (e.g. EURUSD)
{{strategy.order.action}}buy or sell
{{strategy.order.contracts}}Order size
{{strategy.position_size}}Net position after the order
{{close}}Bar close price

Wrap every placeholder in double quotes so the JSON stays valid:

{
  "action": "{{strategy.order.action}}",
  "symbol": "{{ticker}}",
  "lot": {{strategy.order.contracts}}
}

Avoiding Repaint and Duplicate Fires

  1. Use bar-close evaluation — set the alert to Once Per Bar Close and use alert.freq_once_per_bar_close so signals don't repaint intrabar.
  2. Avoid calc_on_every_tick=true unless you specifically want intrabar entries — it can fire and reverse within a bar.
  3. Reference confirmed data — use [1] on series in conditions (e.g. ta.highest(high, 20)[1]) so you act on closed bars.
  4. One alert per strategy — a single "Order fills only" alert covers every entry and exit; don't stack multiple alerts on the same strategy.

Why Strategy Alerts Don't Fire (Checklist)

Still stuck after these? Walk through the full webhook troubleshooting guide.

From Pine Alert to MT5 Trade — Instantly

TradingView Copier Pro turns your strategy alerts into live MT5 orders in under 50ms, with full JSON support, partial closes, and symbol remapping.

Get TradingView Copier Pro →

Frequently Asked Questions

What is the difference between alert() and strategy alerts in Pine Script?

alert() is a function you call manually inside your code at any condition, giving you full control over the message and when it fires. Strategy alerts use the alert_message argument on strategy.entry/strategy.exit/strategy.close calls and fire automatically whenever the order executes. For trade copying to MT5, alert_message on strategy orders is the most reliable because the alert is tied directly to the order event.

How do I send a Pine Script strategy alert to MT5?

Add an alert_message to each strategy order (entry, exit, close) containing the JSON your copier expects. Then create one alert in TradingView using the condition 'Order fills only' or 'alert() function calls only', tick 'Webhook URL', and paste your copier's webhook URL. When the strategy fires an order, TradingView posts the alert_message to MT5.

Why is my Pine Script strategy alert not firing?

The most common causes are: the alert was created with the wrong condition (use 'Order fills only' for strategy orders), the chart timeframe differs from the alert, the strategy uses calc_on_every_tick incorrectly, or the alert_message contains invalid JSON. Also confirm the alert is set to 'Once Per Bar Close' to avoid repainting and duplicate fires.