Guide

Pine Script alert.freq: Once Per Bar vs Every Update

What alert.freq_all, alert.freq_once_per_bar and alert.freq_once_per_bar_close do on a live bar, and how to pick the one your Pine Script alert needs.

•11 min read

You write an alert, test it on a quiet chart, and it behaves. Then a volatile hour arrives and your phone buzzes eleven times for the same condition. Or the opposite: the condition was true for two minutes in the middle of a candle, nothing fired, and when you look at the closed bar the condition is gone. Both stories come down to one argument that most scripts leave at its default: the freq parameter of alert(), and the three alert.freq_* constants that feed it.

This guide explains exactly what each constant does on a live bar, why the answer depends on how the bar is still moving when the call runs, and how to pick the one that matches the question you are asking. It also covers the places where the argument is silently ignored, so you do not spend an evening tuning a setting that never applied.

What alert.freq controls

The alert() function takes two arguments: alert(message, freq). The message is a series string, so it can be built at runtime. The freq argument tells TradingView how often the alert is allowed to fire, and it accepts exactly three values:

  • alert.freq_all: every alert() call made during the realtime bar triggers the alert.
  • alert.freq_once_per_bar: only the first call during the realtime bar triggers the alert. This is the default, so a call written as alert("text") behaves like alert("text", alert.freq_once_per_bar).
  • alert.freq_once_per_bar_close: the alert fires only when the realtime bar closes, and only if an alert() call runs on that final iteration.

The key phrase in all three is "realtime bar". A script executes once for every historical bar when it loads, then runs again on every price update of the bar that is still forming. Alerts only trigger from that realtime stage. Historical bars never fire anything, which is why a freshly added alert does not spam you with every past crossover. If you want the full picture of that loop, the execution model post walks through it, and it is the context you need for the rest of this page.

The reason the three constants differ at all is that a realtime bar is not one event. It is a sequence of updates. On a one-minute chart, a liquid market can deliver dozens of ticks inside a bar, and your script reruns on each of them. Each rerun is a chance for alert() to be called, and freq decides which of those calls count.

One bar, three behaviors

Take a simple condition: a fast EMA crosses above a slow EMA. Here is the same logic with the default frequency, written out explicitly so you can see where the argument goes:

pine
//@version=6
indicator("alert.freq demo: once per bar", overlay = true)

fast = ta.ema(close, 9)
slow = ta.ema(close, 21)
crossedUp = ta.crossover(fast, slow)

plot(fast, "Fast EMA", color.teal)
plot(slow, "Slow EMA", color.orange)

if crossedUp
    alert("EMA 9 crossed above EMA 21 on " + syminfo.ticker + " " + timeframe.period, alert.freq_once_per_bar)

Now follow one realtime bar through it. At the open, the fast EMA sits just under the slow one. A tick pushes the close up, the fast EMA edges over, and ta.crossover becomes true on that update. The script reruns, the if branch executes, and alert() is called. Under alert.freq_once_per_bar, that first call fires the alert. A few ticks later the price slips back, the fast EMA drops under the slow one, and the condition is false again. Your alert is already out the door. The bar closes with no crossover at all, and the chart shows nothing where your notification claimed there was something.

That is not a bug. It is the exact definition of the constant: the first call during the bar triggers the alert, whatever happens to the bar afterward. Once per bar means you will hear about the bar at most once, and you will hear about it early.

Now consider alert.freq_once_per_bar_close. The same script, with one word changed, behaves differently:

pine
//@version=6
indicator("alert.freq demo: bar close", overlay = true)

fast = ta.ema(close, 9)
slow = ta.ema(close, 21)
crossedUp = ta.crossover(fast, slow)

plot(fast, "Fast EMA", color.teal)
plot(slow, "Slow EMA", color.orange)

if crossedUp
    alert("EMA 9 crossed above EMA 21 on " + syminfo.ticker + " " + timeframe.period, alert.freq_once_per_bar_close)

During the bar, the mid-candle cross runs the alert() call, but nothing is sent. The alert only fires when the bar closes, and only if an alert() call runs on that final iteration of the script. In the story above, the price slipped back, so the last iteration has crossedUp false, the call does not run, and no alert is sent. If the cross is still standing at the close, you get exactly one alert, and the chart agrees with it. The cost is time: you hear about the cross when the bar ends, not when it happens. On a four-hour chart that can be a long wait.

The third constant, alert.freq_all, sends an alert for every call made during the realtime bar. If the condition holds across thirty ticks, the branch runs thirty times and you get thirty alerts. That sounds like a mistake to ask for, but there are honest uses, covered further down.

Seeing the difference with a tick counter

It is easier to trust this once you have watched it. The next script counts the updates inside the current realtime bar and puts the count into the alert text, using varip so the counter survives between ticks instead of rolling back each time:

pine
//@version=6
indicator("alert.freq_all tick counter", overlay = true)

varip int ticks = 0
if barstate.isnew
    ticks := 0
ticks += 1

if barstate.isrealtime and close > open
    alert("Update " + str.tostring(ticks) + " with the close above the open", alert.freq_all)

Add this to a one-minute chart of an active market and create an alert from it. While the bar's close is above its open, every update sends a message numbered 1, 2, 3 and so on. Change alert.freq_all to alert.freq_once_per_bar and you receive only the first message of the bar. Change it to alert.freq_once_per_bar_close and you receive a single message, sent when the bar closes, and only when the close finished above the open. The same condition, three different notification streams. If you have not met varip before, the var and varip post explains why the counter needs it.

This little script is also a decent test harness. Swap in your own condition, keep the numbered message, and read the alert log after one volatile bar. You see how many times your branch really runs, which is often more than you assumed.

Which one to choose

The right constant depends on what the alert is claiming. A useful way to decide is to ask whether the statement in your message would still be true when the bar closes.

If the message describes something about a finished candle, such as a close above a level, a completed crossover, or an engulfing pattern, you want alert.freq_once_per_bar_close. Those conditions are only settled at the close, and the alert should say so. This is also the choice that matches what you will see when you scroll back through history, because on historical bars the script only ever sees the closed values. That match is the whole point of avoiding repainting, which the repainting guide covers in depth.

If the message describes something that is permanent once it happens, the first touch can be enough. A price touching a fixed level, a new high of the day, a session opening: once true, they stay true for the rest of the bar in the sense that the event happened. alert.freq_once_per_bar suits these, because the early warning is the useful part and a later retreat does not make the event untrue. You still want to read the condition carefully, though. A touch of a level is permanent; a "close above a level" is not, and using the early frequency for it is how you get notifications that the chart later contradicts.

Reserve alert.freq_all for cases where every update carries information that you actually want to read, such as a numbered message stream you are logging to look at the behavior of a script, or a condition that you deliberately want to keep reporting while it holds. If you point this at a condition that is true for long stretches, you will hit the point where TradingView stops the alert for firing too often, and a stopped alert is worse than a slow one.

Here is a compact table of the three, in the terms you will use when choosing:

ConstantFires onCan fire more than once per barMatches the closed bar
alert.freq_allEvery call on the realtime barYesNo
alert.freq_once_per_barFirst call on the realtime barNoNot guaranteed
alert.freq_once_per_bar_closeClose of the realtime bar, if a call runs on that last iterationNoYes

Letting the user choose with an input

Different people want different trade-offs from the same script, and the freq argument accepts an input string, so you can hand the choice to whoever adds the indicator. The pattern maps a readable dropdown onto the constants:

pine
//@version=6
indicator("Alert frequency input", overlay = true)

freqChoice = input.string("Bar close", "Alert frequency", options = ["Every update", "Once per bar", "Bar close"])
freq = switch freqChoice
    "Every update" => alert.freq_all
    "Once per bar" => alert.freq_once_per_bar
    => alert.freq_once_per_bar_close

basis = ta.sma(close, 50)
plot(basis, "SMA 50", color.blue)

if ta.crossover(close, basis)
    alert("Close crossed above the 50 SMA on " + syminfo.ticker, freq)

The default in this version is the bar close, which is the conservative choice, and the people who want an earlier message can opt in knowingly. If you build a message with several values, the str.format guide shows a cleaner way to assemble the text, and the alert message placeholders post covers the values TradingView can fill in for you.

Where the argument does nothing

The most common waste of time with alert.freq is tuning it somewhere it has no effect. There are three places to watch.

First, alertcondition() has no freq parameter at all. For those alerts, the frequency is chosen by the user in the Create Alert dialog, not by your code, and the message must be a constant string. If you want your script to decide the frequency and build dynamic text, you need alert(). The alertcondition vs alert() comparison lays out when each is the better tool.

Second, in strategies, the default is that scripts run on the bar close. Unless you set calc_on_every_tick = true in the strategy() declaration, every alert() call in a strategy uses the once-per-bar-close frequency, whatever you passed as freq. So a strategy that passes alert.freq_all and never enables that setting still behaves as bar-close. For strategies, you also choose in the alert dialog whether alert() calls, order fill events, or both can trigger the alert. If you want to know more about the setting itself, the calc_on_every_tick post covers what it changes.

Third, anything that runs only on historical bars is invisible to alerts. Alerts only trigger on the realtime bar, so code that you test by scrolling back never proves that an alert will fire. The check that matters happens on the live bar.

Pairing the frequency with barstate.isconfirmed

A common question is whether barstate.isconfirmed is a replacement for alert.freq_once_per_bar_close. They overlap but they are not the same tool. barstate.isconfirmed is true on the last update of a bar, so gating a condition with it makes the whole script see the confirmed value, which also changes what gets plotted and what feeds later calculations. The frequency constant changes only the alert delivery. If you want the chart, the plots, and the alert all to agree, gating the condition is the more thorough fix, and the barstate.isconfirmed guide shows how. If you only need the notification to wait for the close and you are happy for the plots to move during the bar, the frequency constant is the smaller change. And if your alert fires on a bar that looks wrong, the wrong bar post walks through the other usual causes.

Check it on a chart before you trust it

The reliable way to settle a frequency question is to watch the script on a chart and read what it did, rather than reasoning about it in the abstract. PineScripter's chart runs your script on real market data from 308 crypto spot markets, up to 5,000 bars, and bar replay steps through it one bar at a time so you can see exactly where your condition becomes true and where it stops being true. That is the visual version of the story at the top of this post: you can see the mid-candle cross that later disappears, and decide whether you want to hear about it. The editor also checks every script for compile errors the way TradingView does, which helps when you are adding the freq argument to an existing alert and want to know the call still compiles. PineScripter is an independent product and is not made by or affiliated with TradingView, and you paste the finished script into TradingView yourself.

Once the script is live, the alert log is the other half. Give a script a webhook address, paste it into a TradingView alert's webhook URL field, and each firing is recorded with its name, price, and exact time. If you set a frequency and later wonder whether it behaves as you intended, the log tells you how many firings you actually got on a busy bar. A TradingView alert, on a TradingView plan that supports webhooks, is needed to receive anything. For a free route that checks a trigger on the server each time a bar closes and emails you, see the free email alerts post.

A short checklist

Before you ship an alert, run through four questions. Does the message describe a finished bar or an event that stays true once it happens? If it describes a finished bar, use the bar-close constant. Is this an alertcondition(), a strategy without calc_on_every_tick, or something that only runs on history? Then freq is not doing what you think. Have you watched at least one volatile realtime bar to count the real firings? And is the message text something you would still believe if you read it an hour later? An alert is a claim about the chart, and the frequency decides how early you are willing to make that claim.

Disclaimer: PineScripter is a coding tool for Pine Script, not a financial advisor, broker, or signal service. Nothing in this article is trading or investment advice, and no script, alert, or setting described here predicts prices or promises any result. Test every script yourself before relying on it.

Frequently asked questions

What is the default alert.freq in Pine Script?

The default is alert.freq_once_per_bar, so a call written as alert("text") fires only on the first call during the realtime bar.

Does alert.freq work on historical bars?

No. Alerts only trigger on the realtime bar, so scrolling back through history never shows you an alert firing.

Why does my strategy ignore alert.freq_all?

Strategies run on the bar close by default. Unless calc_on_every_tick is true in strategy(), every alert() call uses the once-per-bar-close frequency whatever freq you pass.

Can alertcondition() use alert.freq?

No. alertcondition() has no freq parameter. The frequency is chosen by the user in the Create Alert dialog, and the message must be a constant string.

Which frequency avoids alerts the chart later contradicts?

alert.freq_once_per_bar_close. The alert fires only when the bar closes and only if the call runs on that final iteration, so it agrees with the closed bar.

Can I let the user pick the frequency?

Yes. The freq argument accepts an input string, so an input.string dropdown mapped to the three constants with a switch works.

Keep a log of every alert your script fires →


Disclaimer: PineScripter is a coding tool for Pine Script development. It does not provide financial advice and does not guarantee trading profits. Always backtest strategies thoroughly and understand the risks before live trading.