StructureX

Home › Guides › Platforms

Pine Script alerts: alertcondition() vs alert(), explained simply

You can't watch every chart all day. A Pine Script alert watches for you, and taps you when your condition happens. There are 2 ways to build one. Here's which to use, and why.

Beginner guide · 8-minute read · Updated 2026-10-21

What a Pine Script alert does

An alert is a message TradingView sends you when something happens on a chart. A pop-up, a sound, an email or a phone notification.

Pine Script lets you build alerts for your own conditions. For example: "the candle closed above my average".

There are 2 tools for this in Pine Script version 6:

Both work. They suit different jobs. New to Pine Script? Start with Pine Script for beginners first, then come back.

You need a computer for this part

You write and save the code in the Pine Editor, and that only exists on a computer. Once the alert is created, it runs on TradingView's servers. So it can reach your phone even when your computer is off.

Way 1: alertcondition()

Here's a whole indicator. It draws a 20-candle average and offers an alert for when a candle closes above it:

//@version=6
indicator("Close above average alert", overlay=true)
length = input.int(20, "Length")
avg = ta.sma(close, length)
plot(avg, color=color.orange)
crossUp = ta.crossover(close, avg)
alertcondition(crossUp, "Close above average", "{{ticker}} closed above the average at {{close}}")

What each line does, in plain words:

The words in double curly brackets are placeholders. TradingView swaps them for real values when the alert fires. {{ticker}} becomes the symbol, and {{close}} becomes the closing price.

Two rules for alertcondition()

Turning it on

The code alone sends nothing. You still create the alert:

  1. Save the script and add it to your chart.
  2. Open TradingView's Alerts panel and create a new alert. Look for a clock or bell icon.
  3. For the condition, pick your indicator, then "Close above average".
  4. Choose how often it can fire. Look for Once per bar close.
  5. Pick how you want to be told, then save.

Why "once per bar close"? A live candle keeps moving. Price can cross the line, then fall back before the candle ends. "Once per bar close" waits until the candle is finished.

That matches a rule we use when reading charts: only a candle body closing past a level counts. A spike that pokes through and comes back doesn't.

Way 2: alert()

alert() lives inside your code. You decide exactly when it fires, and the message can include any value your script works out.

Add these 2 lines to the bottom of the script above:

if crossUp
    alert(syminfo.ticker + " closed above the average at " + str.tostring(close), alert.freq_once_per_bar_close)

In plain words:

The other choices are alert.freq_once_per_bar (the first time it happens in a candle, even mid-candle) and alert.freq_all (every time the code runs, which can be many times a candle).

To turn it on, create an alert as before. For the condition, pick your script, then look for Any alert() function call.

Which one should you use?

alertcondition()alert()
Works in indicatorsYesYes
Works in strategiesNoYes
Where you write itTop level onlyAnywhere, often inside "if"
MessageFixed text + placeholdersAny text your code builds
Many conditionsOne menu choice eachOne alert covers them all
Best forSimple, single conditionsDetailed messages, many conditions

A simple rule: one clear condition → alertcondition(). Several conditions, or a strategy → alert().

Edited the script? Make the alert again

When you create an alert, TradingView saves a copy of your script as it was at that moment. If you edit the code later, the old alert keeps running the old version.

So after any change: delete the old alert and create a new one. This catches many people out.

Turn your pre-trade checklist into an alert

An alert works best as a reminder to look, not as a signal to act. Try this:

  1. Write down the one thing that makes a chart worth checking. For us, that's a candle body closing past the last place price turned around.
  2. Code it as one condition.
  3. When the alert fires, open the chart and run your pre-trade checklist.

The free StructureX chart tool marks those turning points for you, if you'd rather not code them yourself.

An alert is a tap on the shoulder, not a decision. It can't tell you if the bigger chart agrees, where your Stop loss goes, or how big to trade. You still check all of that.

Common mistakes

Test it on past charts first. Use bar replay to see where your condition would have fired. If the marks look noisy, the alerts will be too. Pine Script plotshape shows you how to draw those marks.

Learning from a real script

Short examples teach one idea at a time. A finished indicator shows how alerts, settings and drawings work together.

Our course includes the full source code of the StructureX chart tool, to learn from and customize, including adding your own alerts. It's source code, not a ready-made trading bot.

Questions

What is the difference between alert() and alertcondition() in Pine Script?

alertcondition() adds a fixed choice to TradingView's alert menu and only works in indicators. alert() fires from inside your code, can build a custom message, and works in indicators and strategies.

Why is my Pine Script alert not working?

Common causes: the alert was created before you edited the script, alertcondition() sits inside an "if" block, or it's in a strategy. Recreate the alert after every edit.

How do I stop alerts firing before the candle closes?

Choose "Once per bar close" when you create the alert. With alert(), pass alert.freq_once_per_bar_close as the second value.

Can Pine Script alerts reach my phone?

Yes. Alerts run on TradingView's servers, so they can send a notification to the TradingView phone app. You still need a computer to write the code.

Want the next step? We'll walk you through it.

Four free lessons by email, one every few days — each with a 10-minute exercise on a real chart.

Send Me the Free Lessons 4 short emails · No spam · Unsubscribe anytime

Or see the whole method, step by step:

See What’s Inside the Course Try everything free first. Decide when you're ready. · Open the free chart tool (works in the TradingView phone app)

One chart idea a day, in plain English. Follow along:

Follow on XFollow on TradingView