Pine Script alert() Function for TradingView to MT4/MT5 Automation

20 May. 2026
 

Pine Script alert() Function for TradingView to MT4/MT5 Automation

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.


Contact Us for Inquiries

Register for call back

What Does the Pine Script alert() Function Do?

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 string
  • The frequency setting

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.

Why Traders Use alert() Instead of alertcondition()

The alert() function is more flexible than the older alertcondition() method.

Feature

alert()

alertcondition()

Dynamic Messages Yes Limited
Works in strategies?YesNo
Works in IndicatorsYesYes
Supports VariablesYesLimited
Custom Logic Advanced Basic

With alert(), you can create dynamic TradingView alerts using the following:

  • Current symbol
  • Stop loss
  • Take profit
  • Risk percentage
  • Indicator values
  • Real-time prices

This feature makes it ideal for TradingView to MT5 automation.



Contact Us for Inquiries

Register for call back

Step 1: Confirm Your Pine Script Version

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.


Step 2: Identify Whether Your Script is a Strategy or an Indicator

Your TradingView script will usually begin with either:

strategy(...)

or

indicator(...)

This step is important because the alert setup process differs slightly.

Strategy Script Example

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.


Step 3: Add plotshape() for visual confirmation.

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.


Step 4: Add the alert(). Function

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.



Contact Us for Inquiries

Register for call back

How to Create Dynamic TradingView Alerts

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:

  • EURUSD
  • GBPUSD
  • Golden
  • NAS100
  • Crypto pairs

without changing the code manually.

Dynamic Stop Loss and Take Profit

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.


How to Connect TradingView Alerts to MT4/MT5

Once your script is ready, create the TradingView alert.

Open TradingView Alert Dialogue

Press:

  • Alt + A (Windows)
  • Option + A (Mac)

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:

  • TradingView sends the webhook.
  • MetaConnector receives the signal.
  • MT4/MT5 executes the trade automatically.


What is repainting in Pine Script?

Repainting happens when an indicator changes signals during a live candle.

This is common in:

  • RSI
  • MACD
  • SuperTrend
  • EMA crossover strategies

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.

Automating Locked TradingView Scripts

Sometimes traders use protected TradingView indicators whose source code is hidden.

In that case, you can still check the following:

  • Whether the script supports strategy()
  • Whether it exposes a licence ID input
  • Whether it includes alertcondition()

If available, you can still automate TradingView alerts using MetaConnector without modifying the original Pine Script.

Check out the pricing details

Price

SuperTrend Strategy Example with alert()

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.


Benefits of Using MetaConnector for TradingView Automation

MetaConnector helps traders:

  • Connect TradingView to MT4/MT5
  • Automate Pine Script strategies
  • Execute trades instantly
  • Reduce execution delay
  • Use webhook automation
  • Send alerts directly to MetaTrader
  • Manage risk automatically
  • Trade multiple brokers

It is especially useful for:

  • Forex trading
  • Scalping
  • Algo trading
  • Automated trading systems
  • TradingView strategy automation


Conclusion

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:

  • EMA crossover strategies
  • SuperTrend indicators
  • Pine Script signals
  • Scalping systems
  • Algo trading strategies

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.


Check out the pricing details

Price

FAQs


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.