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.
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:
- alertcondition() adds a choice to TradingView's alert menu. You pick it when you create the alert.
- alert() fires a message from inside your code, whenever your code says so.
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:
- Line 1 says this is Pine Script version 6.
- Line 2 names the indicator and puts it on the price chart.
- Line 3 makes "Length" a setting you can change from the chart. More in Pine Script inputs.
- Line 4 works out the simple average of the last 20 closing prices.
- Line 5 draws that average in orange.
- Line 6 is true on the one candle where the close crosses from below the average to above it.
- Line 7 turns that into an alert choice. The 3 parts are: the condition, the title you'll see in the menu, and the message.
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()
- Indicators only. It doesn't work inside a strategy script.
- Top level only. Write it on its own line, not inside an "if" block.
Turning it on
The code alone sends nothing. You still create the alert:
- Save the script and add it to your chart.
- Open TradingView's Alerts panel and create a new alert. Look for a clock or bell icon.
- For the condition, pick your indicator, then "Close above average".
- Choose how often it can fire. Look for Once per bar close.
- 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:
- Line 1: "if the cross happened on this candle…"
- Line 2: "…send this message." The 4-space indent puts it inside the "if".
- syminfo.ticker is the symbol's name, like "AAPL".
- str.tostring(close) turns the closing price into text, so it can join the message.
- The + signs glue the pieces of text together.
- alert.freq_once_per_bar_close means "only send this once the candle has closed".
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 indicators | Yes | Yes |
| Works in strategies | No | Yes |
| Where you write it | Top level only | Anywhere, often inside "if" |
| Message | Fixed text + placeholders | Any text your code builds |
| Many conditions | One menu choice each | One alert covers them all |
| Best for | Simple, single conditions | Detailed 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:
- 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.
- Code it as one condition.
- 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
- Firing on the live candle. Without "once per bar close", you'll get alerts for crosses that undo themselves seconds later.
- Putting alertcondition() inside an "if". It must sit on its own line at the top level.
- Using alertcondition() in a strategy. Use alert() there.
- Forgetting to recreate the alert after an edit. The old alert runs the old code.
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 anytimeOr 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: