Pine Script Alerts: The Complete Setup Guide (alertcondition Explained)

March 17, 2026

Pine Script Alerts: The Complete Setup Guide (alertcondition Explained)

March 17, 2026

Pine Script alerts are what turn an indicator from a passive visual tool into an active notification system. But TradingView's alert setup confuses many beginners — there are two different functions (alertcondition() and alert()), the UI dialog has confusing frequency options, and a specific scoping rule about where you can call alertcondition() trips up almost everyone the first time. This complete guide covers everything from basic setup to webhook automation.

Create Alert Condition My Indicator → Signal Triggered Frequency Once Per Bar Close ▾ Message signal at Create Alert
TradingView Create Alert dialog — your alertcondition() calls appear in the Condition dropdown

1. alertcondition() vs alert() — Which to Use

Pine Script v5 has two alerting mechanisms with different use cases:

pine
//@version=5
indicator("Alert Demo", overlay=true)

fastMA = ta.ema(close, 9)
slowMA = ta.ema(close, 21)

// alertcondition(): registers an alert template in the Create Alert dialog
// Must be at GLOBAL SCOPE (not inside if blocks)
// User creates the actual alert in TradingView UI
bullCross = ta.crossover(fastMA, slowMA)
alertcondition(bullCross, title="Bullish Cross", message="EMA crossover on ")

// alert(): fires immediately when condition is true on each bar
// More flexible — can be called anywhere, including inside if blocks
// Works in strategies too
if bullCross
    alert("Bullish EMA cross on " + syminfo.ticker, alert.freq_once_per_bar)

Key differences: alertcondition() just registers a template — the user still has to create the alert in TradingView's UI. alert() fires programmatically without any UI setup required. For strategies or when you want dynamic alert messages, use alert(). For simple indicators that users will add alerts to themselves, alertcondition() is the standard approach.

2. The alertcondition Scope Error (and Fix)

This is the most common Pine Script alert error. alertcondition() must be called at the global scope of your script — not inside if, for, or function blocks:

pine
//@version=5
indicator("My Indicator", overlay=true)

rsiVal = ta.rsi(close, 14)
isOversold = rsiVal < 30

// WRONG — alertcondition inside an if block causes compile error:
// "Function 'alertcondition' should be called on each calculation."
if isOversold
    alertcondition(true, title="Oversold", message="RSI oversold")

// CORRECT — compute condition first, then call at global scope
oversoldAlert = rsiVal < 30
alertcondition(oversoldAlert, title="RSI Oversold", message="RSI below 30 on  at ")

3. Frequency Options Explained

When creating an alert in TradingView's UI, the frequency setting controls how often alerts fire:

  • Once Per Bar — fires on the first tick that meets the condition within a bar. Can fire multiple times per bar if condition briefly becomes false then true again.
  • Once Per Bar Close — fires only when a bar closes with the condition true. Most reliable for avoiding false signals.
  • Once Per Minute — fires at most once per minute while condition is true.
  • Only Once — fires a single time ever, then deactivates. Useful for one-time setup notifications.

For strategies, use alert.freq_once_per_bar_close to avoid partial-bar signals:

// Frequency constants for alert()
alert("Signal", alert.freq_once_per_bar)           // once per bar, first occurrence
alert("Signal", alert.freq_once_per_bar_close)     // only on confirmed bar close
alert("Signal", alert.freq_all)                    // every tick (use carefully)

4. Alert Message Placeholders

TradingView supports dynamic placeholders in alert messages that get replaced with live values:

alertcondition(
    condition = buySignal,
    title     = "Buy Signal",
    message   = "BUY  | Close:  | Time:  | TF: "
)

// Available placeholders:
//     — symbol name (e.g., BINANCE:BTCUSDT)
//   — exchange name
//      — closing price of the bar
// , , 
//     — bar volume
//       — bar time in Unix ms
//    — current time in Unix ms
//   — chart timeframe (e.g., "1D", "60")
//     — value of the first plot() in your script

5. Webhook Integration for Automation

You can send alert messages to any webhook URL — useful for connecting TradingView to trading bots or notification systems:

// Structure your alert message as JSON for webhook consumption
alertcondition(
    condition = buySignal,
    title     = "Buy Signal",
    message   = '{"action":"buy","symbol":"","price":,"time":""}'
)

// In the TradingView Create Alert dialog:
// 1. Set the condition to your alertcondition title
// 2. Under "Notifications", enable "Webhook URL"
// 3. Enter your webhook endpoint (e.g., your bot's /webhook route)
// TradingView will POST the message body as the request body

For building complete strategies with entries and exits that use alerts, see the pine-script strategy.entry guide which covers the alert_message parameter on strategy functions.

Summary

Use alertcondition() at global scope to register alert templates for users; use alert() inside conditional blocks for programmatic firing. Always compute your condition boolean before passing it to alertcondition() — never call it inside if blocks. Choose "Once Per Bar Close" frequency for reliable end-of-bar signals, and use placeholders like and to make alert messages informative.