Alerts#
TradingView’s alert system can notify you via push notifications, email, or Webhook when a condition triggers — and can even automatically place orders. This chapter explains how to define alert conditions in Pine Script and how to format Webhook messages.
alertcondition — Defining an Alert Condition#
alertcondition() pre-defines an alert condition in the script that users can subscribe to:
alertcondition(condition, title, message)| Parameter | Description |
|---|---|
condition | Boolean condition that triggers the alert (fires when true) |
title | Name shown in the “Create Alert” dialog |
message | Message sent when the alert fires (must be a constant string) |
alertcondition() has three important restrictions:
messagemust be a constant string: The message content must be determined at compile time; it cannot change dynamically per bar. You can use placeholders like{{close}}to inject values at trigger time.- Can only be called at the global scope:
alertcondition()must be at the top-level of the script — not insideifblocks or functions. - Only for indicators: Strategy scripts can include it without error, but you cannot create an
alertconditionalert from a strategy — use thealert_messageparameter instead.
//@version=6
indicator("MA Crossover Alert", overlay=true)
ma5 = ta.sma(close, 5)
ma20 = ta.sma(close, 20)
goldenCross = ta.crossover(ma5, ma20)
deathCross = ta.crossunder(ma5, ma20)
plot(ma5, "MA5", color=color.blue)
plot(ma20, "MA20", color=color.orange)
// alertcondition must be at the top level (not inside an if block)
alertcondition(goldenCross, "Golden Cross", "MA5 crossed above MA20 — consider going long")
alertcondition(deathCross, "Death Cross", "MA5 crossed below MA20 — consider going short")After applying the script, click the alarm clock icon in the top-right corner → Create Alert, and you will see the alert names defined above in the “Condition” dropdown.
alert() — Triggering Alerts Dynamically#
alert() can fire an alert directly during script execution and supports dynamic message content:
alert(message, freq)freq parameter | Description |
|---|---|
alert.freq_once_per_bar | Fire at most once per bar (default) |
alert.freq_once_per_bar_close | Fire only at bar close |
alert.freq_all | Fire every time the condition is true (may fire multiple times on the realtime bar) |
//@version=6
indicator("Dynamic Alert Message", overlay=true)
rsiValue = ta.rsi(close, 14)
if ta.crossunder(rsiValue, 30)
alert("RSI dropped below 30! Current RSI = " + str.tostring(math.round(rsiValue, 2)) +
", Close = " + str.tostring(close),
alert.freq_once_per_bar_close)Dynamic Placeholders in Messages#
TradingView provides built-in placeholders that are automatically replaced with real values in alert messages:
| Placeholder | Replaced with |
|---|---|
{{ticker}} | Symbol code (e.g. AAPL) |
{{exchange}} | Exchange (e.g. NASDAQ) |
{{interval}} | Current timeframe (e.g. 1D, 60) |
{{open}} | Open price at trigger time |
{{high}} | High price at trigger time |
{{low}} | Low price at trigger time |
{{close}} | Close price at trigger time |
{{volume}} | Volume at trigger time |
{{time}} | Trigger time (Unix timestamp) |
{{timenow}} | Current time |
{{plot_0}} ~ {{plot_19}} | Value of the 1st through 20th plot in the script |
{{plot("title")}} | Value of the plot with the specified title |
alertcondition(goldenCross, "Golden Cross",
"{{ticker}} Golden Cross! Close: {{close}}, Time: {{timenow}}")Webhook Message Format#
Webhook alerts send an HTTP POST request to a specified URL, commonly used to connect to automated order placement systems. Messages are usually in JSON format:
//@version=6
strategy("Webhook Strategy", overlay=true)
ma5 = ta.sma(close, 5)
ma20 = ta.sma(close, 20)
// Entry signal
if ta.crossover(ma5, ma20)
strategy.entry("Long", strategy.long)
alert('{"action":"buy","symbol":"{{ticker}}","price":{{close}},"qty":1}',
alert.freq_once_per_bar_close)
// Exit signal
if ta.crossunder(ma5, ma20)
strategy.close("Long")
alert('{"action":"sell","symbol":"{{ticker}}","price":{{close}},"qty":1}',
alert.freq_once_per_bar_close)For a detailed Webhook auto-trading integration flow, see CH16: Webhook Auto-Trading.
Built-in Strategy Alerts#
When using a strategy() script, TradingView automatically creates corresponding alert conditions for each strategy.entry, strategy.exit, and strategy.close. In “Create Alert”, select “Any strategy() function call” to receive all entry/exit signals.
//@version=6
strategy("Strategy Alert Demo", overlay=true)
ma5 = ta.sma(close, 5)
ma20 = ta.sma(close, 20)
if ta.crossover(ma5, ma20)
strategy.entry("Long", strategy.long,
alert_message='{"action":"buy","ticker":"{{ticker}}","price":{{close}}}')
if ta.crossunder(ma5, ma20)
strategy.close("Long",
alert_message='{"action":"sell","ticker":"{{ticker}}","price":{{close}}}')The alert_message parameter lets you specify a custom Webhook message for each entry/exit order.
Steps to Create an Alert#
- Add the script to the chart
- Click the alarm clock icon (Create Alert) in the top-right corner
- Condition: Select the script name and corresponding alert condition
- Trigger: Choose “Once” or “Every time condition is met”
- Notification: Check App notification, Email, or Webhook URL
- Enter the Webhook URL (if using Webhook)
- Click Save