
If your Pine Script strategy generates buy or sell signals but nothing happens in MetaTrader, the missing piece is usually the alert() function.
The Pine Script alert() function provides traders with the ability to set TradingView alerts and automatically send them to MetaTrader 4 or MetaTrader 5 through automated webhook applications such as MetaConnector. With correct configuration, you will be able to use your TradingView strategy for executing automatic trades in MetaTrader without any effort.
This tutorial will teach you about the Pine Script alert() function, create TradingView webhook alerts, connect TradingView to MetaTrader, and automate your MT4/MT5 trading system via MetaConnector.
In Pine Script, the alert() function is used to notify you each time when a specific condition in your script occurs.
On its own, the alert works in the Alerts Log on TradingView. However, when you include the webhook URL in it, the alert turns into a real trading signal that can be sent to your MT4 or MT5 platform by MetaConnector.
Here is the basic syntax:
alert('LicenseID, buy, EURUSD, risk=1', alert.freq_once_per_bar_close)
This function contains two important parts:
The message tells MetaConnector what action to execute, such as buy, sell, stop loss, take profit, or risk management settings.
The second parameter, alert.freq_once_per_bar_close, ensures the alert only triggers after the candle closes. This helps reduce repainting issues and prevents false signals during live market movement.
The alert() function is more flexible than the older alertcondition() method.
Feature | alert() | alertcondition() |
| Dynamic Messages | Yes | Limited |
| Works in strategies? | Yes | No |
| Works in Indicators | Yes | Yes |
| Supports Variables | Yes | Limited |
| Custom Logic | Advanced | Basic |
With alert(), you can create dynamic TradingView alerts using the following:
This feature makes it ideal for TradingView to MT5 automation.
Before creating alerts, open Pine Editor and verify your script version.
Your script should start with:
//@version=5
Pine Script v5 is recommended for TradingView automation because it supports modern alert features and better compatibility with MetaConnector.
Older Pine Script versions may cause webhook errors or broken alerts.
Your TradingView script will usually begin with either:
strategy(...)
or
indicator(...)
This step is important because the alert setup process differs slightly.
If ta.change(direction) < 0
strategy.entry('Long', strategy.long)
Indicator Script Example
long = ta.crossover(ema20, ema50)
short = ta.crossunder(ema20, ema50)
These conditions are what trigger your TradingView alerts.
Before sending live trades to MetaTrader, it is smart to visually verify your entry conditions.
Use plotshape() to display buy and sell arrows on the chart.
plotshape(LongEntryCondition, style=shape.arrowup, location=location.belowbar, colour=colour.blue)
plotshape(ShortEntryCondition, style=shape.arrowdown, location=location.abovebar, colour=colour.red)
This helps confirm that your Pine Script strategy logic is working correctly before enabling automation.
This is the most important step.
For strategy scripts, place the alert directly below strategy. entry().
If ta.change(direction) < 0
strategy.entry('Long', strategy.long)
alert('LicenseID, buy, EURUSD, risk=1', alert.freq_once_per_bar_close)
If ta.change(direction) > 0
strategy.entry('Short', strategy.short)
alert('LicenseID, sell, EURUSD, risk=1', alert.freq_once_per_bar_close)
For indicator scripts:
if LongEntryCondition
alert('LicenseID, buy, EURUSD, risk=1', alert.freq_once_per_bar_close)
if ShortEntryCondition
alert('LicenseID, sell, EURUSD, risk=1', alert.freq_once_per_bar_close)
Replace LicenseID with your actual MetaConnector License ID.
Static alerts work for simple setups, but advanced traders usually prefer dynamic alerts.
Dynamic Symbol Example
alert('LicenseID, buy, ' + syminfo.ticker + ', risk=1', alert.freq_once_per_bar_close)
Now the same script works on:
without changing the code manually.
You can also automate stop-loss and take-profit values.
LongSL = low[1]
LongTP = ta.ema(close, 50)
if LongEntryCondition
alert('LicenseID,buy,' + syminfo.ticker + ',sl=' + str.tostring(LongSL) + ',tp=' + str.tostring(LongTP) + ',risk=1', alert.freq_once_per_bar_close)
This setup allows MetaConnector to execute fully automated MT5 trades with dynamic risk management.
Once your script is ready, create the TradingView alert.
Open TradingView Alert Dialogue
Press:
Configure the settings:
Setting | Value |
| Condition | alert() function calls only |
| Webhook URL | Your MetaConnector webhook URL |
| Trigger | Once Per Bar Close |
When the alert fires:
Repainting happens when an indicator changes signals during a live candle.
This is common in:
Using:
alert.freq_once_per_bar_close
helps reduce repainting because the alert only triggers after the candle closes.
This is considered best practice for automated trading systems.
Sometimes traders use protected TradingView indicators whose source code is hidden.
In that case, you can still check the following:
If available, you can still automate TradingView alerts using MetaConnector without modifying the original Pine Script.
Here is a simple SuperTrend automation example:
If ta.change(direction) < 0
strategy.entry('Long', strategy.long)
alert(str.tostring(LicenseID) + ',buy,' + syminfo.ticker + ',risk=1', alert.freq_once_per_bar_close)
If ta.change(direction) > 0
strategy.entry('Short', strategy.short)
alert(str.tostring(LicenseID) + ',sell,' + syminfo.ticker + ',risk=1', alert.freq_once_per_bar_close)
This setup automatically sends TradingView buy and sell signals to MT4 or MT5 using MetaConnector.
MetaConnector helps traders:
It is especially useful for:
The Pine Script alert() function is one of the most powerful tools for TradingView automation.
Once combined with webhook alerts and MetaConnector, traders can fully automate their MT4 and MT5 strategies without manual execution.
Whether you are using:
The alert() function acts as the bridge between TradingView and MetaTrader.
For quick and efficient TradingView to MT4/MT5 automation, implementing Pine Script alerts via MetaConnector is among the most effective methods out there.
What does the alert() function do in Pine Script?
The `alert()` function creates an alert any time a condition in your Pine Script strategy or indicator turns true.
Can TradingView execute trades automatically?
TradingView does not allow you to execute trades. Nevertheless, with webhook automation services such as MetaConnector, you can automate your trades through TradingView alerts in MT4/MT5.
What is the best alert frequency for automated trading?
Most traders go with:
alert.freq_once_per_bar_close
Since this option minimises repainting issues and fake signals.
Can I use alert() in both indicators and strategies?
Yes. Pine Script's alert() function can be used in both indicators and strategies.
How do I connect TradingView alerts to MetaTrader?
You can link TradingView alerts to MetaTrader by using MetaConnector webhook automation. You will need to configure the webhook URL in the TradingView alert settings and connect it to your MT4 or MT5 terminal.