Skip to main content
On this page

Pine Script alertcondition(): Custom Alerts and Bar-Close Timing

· 5 min read
Pineify Team
Pine Script and AI trading workflow research team

alertcondition() exposes a named alert condition from a Pine indicator. Adding it to a script does not start a running alert: you must create the alert in TradingView and choose its condition, frequency, and notification settings. The condition describes an event, not a recommendation to trade.

alertcondition() and alert() have different interfaces​

alertcondition(condition, title, message) takes a series boolean condition and constant strings for the title and message. Put the call in global scope. Each call can become a separate choice in an indicator's alert menu.

A constant message can contain supported placeholders, including {{ticker}} and {{close}}, whose values TradingView substitutes when the alert fires. You cannot build that message by concatenating an arbitrary series string. Use alert() when you need a dynamically constructed message.

Capabilityalertcondition()alert()
Indicator alertsSeparate named conditionsEvents from selected alert() calls
Strategy alertsDoes not create usable strategy alert conditionsSupported, alongside order-fill alert options
MessageConstant text with supported placeholdersCan use series strings
ScopeGlobal scopeCommonly inside a conditional block
Running alertMust be created in TradingViewMust also be created in TradingView

Neither function bypasses the alert setup interface. TradingView's official alert documentation explains these distinctions and the separate behavior of strategy order-fill events.

Complete moving-average example​

This educational indicator exposes two confirmed crossover events. The lengths are adjustable examples, not tested trading settings. Both cross calculations run on every bar; the alert conditions then require bar confirmation.

//@version=6
indicator("Confirmed MA Cross Alerts", overlay=true)
fastLength = input.int(20, "Fast MA Length", minval=1)
slowLength = input.int(50, "Slow MA Length", minval=1)
fastMA = ta.sma(close, fastLength)
slowMA = ta.sma(close, slowLength)
crossUp = ta.crossover(fastMA, slowMA)
crossDown = ta.crossunder(fastMA, slowMA)
confirmedUp = crossUp and barstate.isconfirmed
confirmedDown = crossDown and barstate.isconfirmed
plot(fastMA, "Fast MA", color=color.blue)
plot(slowMA, "Slow MA", color=color.orange)
plotshape(confirmedUp, title="Up cross", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.tiny)
plotshape(confirmedDown, title="Down cross", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.tiny)
alertcondition(confirmedUp, title="Confirmed Up Cross", message="{{ticker}}: fast MA crossed above slow MA. Close: {{close}}")
alertcondition(confirmedDown, title="Confirmed Down Cross", message="{{ticker}}: fast MA crossed below slow MA. Close: {{close}}")

The triangle marks the same boolean used by the corresponding alert condition. Historical markers help inspect the logic, but they do not mean historical notifications were sent. Running alerts trigger on realtime bars.

Create and maintain a running alert​

  1. Save the script in Pine Editor, add it to the intended chart, and confirm the symbol, timeframe, and input values.
  2. Open TradingView's Create Alert dialog and select the indicator and one of its named conditions.
  3. Choose a frequency appropriate to the rule. For this confirmed-bar example, use Once Per Bar Close.
  4. Configure the available notification channels and create the alert.
  5. Check the alert log when the next qualifying realtime event occurs.

TradingView saves a copy of the script and settings with the alert. Editing the chart script or its inputs does not automatically update that saved copy. Recreate the alert when you need it to use the changed configuration.

Once Per Bar is not repeated intrabar firing​

Once Per Bar limits the alert to the first qualifying event in a bar. It can act on a condition that later disappears before the bar closes. Once Per Bar Close waits for the closing evaluation. The tradeoff is earlier notification versus confirmation, not unlimited notifications versus one notification.

If a condition stays true on successive bars, a once-per-bar alert can still fire again on each new bar. For an event such as crossing a threshold, define the crossing rather than merely testing whether price remains above the threshold.

Troubleshoot the condition and the setup separately​

If the named condition is missing, check that the compiled script is an indicator, the call is in global scope, and the correct script instance is on the chart. If it appears but never fires, inspect the plotted condition and confirm that a qualifying realtime event actually occurred after activation.

For unexpected messages, check placeholders and the alert's saved settings. For multiple notifications, inspect the alert list for duplicate running alerts as well as the condition and frequency. Adding more filters may reduce event frequency, but it does not establish fewer losing trades or better timing.

Frequently asked questions​

Can alertcondition() include the current price?

Yes, through supported placeholders such as the close placeholder used in the example. Its message argument itself must remain a constant string.

Does alert() create a running alert automatically?

No. You must create an alert in TradingView that listens to the script's alert() calls.

Can I use alertcondition() in a strategy?

It does not provide usable strategy alert conditions. Use supported alert() calls or strategy order-fill alerts according to the event you need.

Does an alert prove a good entry?

No. It reports that the coded event occurred. Signal quality and any execution process require separate evaluation.

The Best Pine Script Generator