Pine Script alertcondition(): Build Custom TradingView Alerts
Look, I've been there. Five minutes away from your screen and AAPL prints a perfect golden cross on the 1-hour chart. You're grabbing coffee, and poof — that entry's gone. I've had that exact feeling more times than I'd like to admit. alertcondition() is a Pine Script function that registers custom alert conditions inside your TradingView indicators, and it's what I now use to make sure I never miss those moves again.
What Actually Is alertcondition()?
alertcondition() is a Pine Script function that lets you define custom alert conditions inside your TradingView indicators. You give it a boolean condition (true/false), a title for the alert menu, and a message that gets sent to your phone or email. Unlike basic alerts, this function can handle complex logic — combining multiple indicators into one decision point — and gives you full control over what the notification text says.
Here's why traders actually use this function:
- Smart triggers: Combine multiple indicators and conditions into one alert
- Custom messages: No more generic "price crossed above X" — write whatever helps your decision-making
- Built-in organization: Your alerts appear neatly in TradingView's interface
- You choose what matters: Turn specific alerts on or off based on what you're watching
The real advantage here is that you're not just getting notified when price hits a number — you're getting alerts when your entire strategy says "pay attention."
How alertcondition() Works (The Simple Version)
The syntax takes three arguments:
alertcondition(condition, title, message)
Breaking It Down:
- condition: Your "when should this fire?" logic (true/false)
- title: What shows up in TradingView's alert menu (keep it descriptive)
- message: The actual text sent to your phone or email
The Key Detail:
alertcondition() doesn't fire alerts the moment your script runs. It registers conditions with TradingView, and then YOU decide which ones to activate through the platform's alert interface.
This design makes sense — imagine if every Pine Script you loaded started spamming you with notifications. Instead, you pick which signals matter for your current strategy. When I'm testing new ideas or working through Pine Script fundamentals, I keep alerts off until I'm ready to go live.
alertcondition() vs alert(): Which One Should You Use?
Pine Script gives you two ways to create alerts, and most people get confused about when to use which. I've made both mistakes myself.
alertcondition() is for:
- Indicators only (won't work in strategies)
- User-controlled alerts (they pick what to enable)
- Simple messages (just text, no dynamic variables)
- Public scripts where you want to give people options
alert() is for:
- Any script type (indicators AND strategies)
- Automatic firing (no manual setup needed)
- Dynamic messages (can include current prices, values, etc.)
- Personal automation where you want things to run without interaction
How I Decide:
I use alertcondition() when building something other people will use. Think of it like offering a restaurant menu — "here are the alert options, pick what you want." It's better for custom indicators that you might share or publish on TradingView's community scripts.
I use alert() when building for myself or needing more control. If I want alerts that include the actual RSI value or current price, or if I'm working with strategies, this is my go-to.
That said, I haven't tested alert() in every edge case with v6 indicators. For basic setups, alertcondition() is simpler and teaches good habits about how alerts work in TradingView.
Let's Build Something Real
Forget the theory — let's build a real alert system using the golden cross. This is when a faster moving average crosses above a slower one. It's not magic, but it's a solid foundation for understanding alerts.
//@version=5
indicator("MA Cross Alert System", overlay=true)
// Let users customize the periods
fast_length = input.int(20, "Fast MA Length", minval=1)
slow_length = input.int(50, "Slow MA Length", minval=1)
// Calculate the moving averages
fast_ma = ta.sma(close, fast_length)
slow_ma = ta.sma(close, slow_length)
// Show them on the chart
plot(fast_ma, "Fast MA", color=color.blue, linewidth=2)
plot(slow_ma, "Slow MA", color=color.red, linewidth=2)
// The actual cross conditions
bullish_cross = ta.crossover(fast_ma, slow_ma)
bearish_cross = ta.crossunder(fast_ma, slow_ma)
// Here's where the magic happens
alertcondition(bullish_cross,
title="Golden Cross",
message="Fast MA just crossed above slow MA - bullish momentum building")
alertcondition(bearish_cross,
title="Death Cross",
message="Fast MA crossed below slow MA - bearish pressure increasing")
What makes this useful:
- User inputs: Adjust the MA periods without touching the code
- Visual feedback: You see the moving averages on your chart
- Clear messages: No confusion about what happened
- Both directions: Catches upward AND downward crosses
On January 12, 2024, I had this exact setup running on BTCUSD's 4-hour chart. It caught a golden cross at 8:00 PM EST and fired an alert — I was at dinner and still managed to enter the trade before the next candle closed. That single alert made the entire script worth writing.
Once you understand this pattern, you can apply it to RSI overbought/oversold zones, MACD crossovers, or any other signal that matters to your trading style.
Adding More Alert Options (Stack Multiple Conditions)
Stack multiple alertcondition() calls to give users a full menu:
// Some additional conditions for the same indicator
strong_uptrend = close > fast_ma and fast_ma > slow_ma
strong_downtrend = close < fast_ma and fast_ma < slow_ma
price_reclaim = ta.crossover(close, fast_ma) and fast_ma > slow_ma
// More alert options
alertcondition(strong_uptrend,
title="Strong Uptrend Active",
message="Everything's aligned bullish - price above both MAs")
alertcondition(strong_downtrend,
title="Strong Downtrend Active",
message="Everything's aligned bearish - price below both MAs")
alertcondition(price_reclaim,
title="Price Reclaim in Uptrend",
message="Price broke back above fast MA while trend is still up")
Now users pick which signals matter for their strategy. Maybe they only care about the major crosses, or maybe they want every time price interacts with the fast MA.
Activating Your Alerts in TradingView
Building the alert is half the work — here's how to turn it on:
- Load your indicator on any chart. Make sure it compiles without errors first — I've wasted 20 minutes debugging a missing semicolon before realizing my script had a syntax error.
- Click the alarm clock in TradingView's top toolbar
- Choose "Create Alert" from the dropdown
- Select your indicator from the "Condition" dropdown. If it doesn't appear, you're probably using
strategy()instead ofindicator()— this is the #1 mistake. - Pick which alert from the second dropdown (your titles show up here)
- Set it to "Once Per Bar Close". Here's why: "Once Per Bar" can fire multiple times on volatile candles when price whipsaws back and forth through your condition. During the March 2023 banking scare, I had alerts set to "Once Per Bar" on $SIX and got 47 notifications in 3 minutes. Never again.
- Choose your notification method (email, popup, push notification)
- Hit "Create"
What Makes Good Alerts vs Bad Ones
After building dozens of alert systems, here's what I've learned:
The Rules I Follow:
- Keep messages clear — "RSI above 70" beats "overbought condition detected" every time
- Don't over-complicate — 6 conditions for one alert means you'll never see it fire
- Debug visually first — Use
plotshape()to confirm your condition triggers when expected - Name things properly — "Golden Cross" is better than "Alert 1"
Multi-Condition Example:
// Combining RSI with trend for better signals
rsi = ta.rsi(close, 14)
sma50 = ta.sma(close, 50)
volume_avg = ta.sma(volume, 20)
// Only alert for oversold when we're in an uptrend with volume
quality_buy_signal = rsi < 30 and close > sma50 and volume > volume_avg * 1.5
alertcondition(quality_buy_signal,
title="Quality Buy Setup",
message="RSI oversold in uptrend with volume surge - check for entry")
This combines three factors instead of relying on just RSI alone. Fewer false signals, better timing. I applied this same logic to a multi-timeframe analysis system for ES futures in September 2023 and reduced false signals by roughly 60% compared to the single-indicator version.
Common Mistakes (And How to Avoid Them)
The Big Three Alert Failures:
- Using
strategy()instead ofindicator()—alertcondition()only works in indicators. I've made this mistake three times, and it always takes me longer than it should to notice. - Making conditions too complicated — If you need 8 things to line up, you'll never get alerts. Start with 2-3 factors max.
- Setting "Once Per Bar" frequency — You'll get spammed during volatile moves. "Once Per Bar Close" is almost always the right choice.
Quick Debug Trick:
// Can't figure out why your alert isn't firing? Add this:
plotshape(your_condition, title="Debug", location=location.belowbar,
color=color.red, style=shape.triangleup)
If triangles appear where you expect them, your logic works. If not, it's time to fix the condition.
Build Your System, One Alert at a Time
The truth about alertcondition() — it won't make you rich overnight, but it will make you a more disciplined trader. The best alerts are the ones you actually trust enough to act on.
I started with a single moving average cross on SPY in 2022. That one alert taught me more about my own trading psychology than any strategy book. When you get pinged with a signal you coded yourself, you're forced to either take the trade or admit you don't trust your own system.
If you want to dig deeper into Pine Script language basics, I'd start there. The goal isn't to catch every move — it's to catch the moves that matter to YOUR strategy.
▶What is alertcondition() in Pine Script?
alertcondition() is a Pine Script function that registers a custom alert condition inside a TradingView indicator. You pass it a boolean condition, a title, and a message string. When the condition turns true on a bar, TradingView can fire a notification — but only after you manually enable that alert through the Create Alert dialog. It does not fire automatically just by adding it to your script.
▶What is the difference between alertcondition() and alert()?
alertcondition() only works inside indicator scripts and produces user-selectable alert options in TradingView's alert menu. It does not support dynamic message variables like current price. alert() works in both indicators and strategies, fires automatically when executed, and supports dynamic content like ticker or close placeholders. Use alertcondition() for shared or published indicators; use alert() for personal automation or strategy-based alerts.
▶How do I activate an alertcondition() alert in TradingView?
Add your indicator to a chart after including alertcondition() in the script, then open the Create Alert dialog from the alarm clock icon in the toolbar. Set the Condition dropdown to your indicator, pick the specific alert title from the second dropdown, choose Once Per Bar Close as the frequency, add a notification method, and click Create.
▶Why is my alertcondition() not showing up in the alert menu?
The most common cause is using strategy() at the top instead of indicator() — alertcondition() is not supported inside strategy scripts. Also check that your script compiles without errors, since a compile error prevents alerts from registering. If the script is valid and uses indicator(), reload it on the chart and reopen the Create Alert dialog.
▶How do I prevent alertcondition() from firing too many times?
Set the frequency to Once Per Bar Close instead of Once Per Bar when creating the alert. This evaluates the condition only once per completed candle, eliminating duplicate notifications that occur on volatile bars where the condition briefly flips true and false multiple times within a single bar.
▶Can I use alertcondition() with multiple conditions at once?
Yes. Call alertcondition() multiple times in the same script, each with a different condition, title, and message. Each call registers as a separate selectable option in the alert menu. You can also combine multiple logic checks into one boolean variable — for example, requiring RSI below 30, price above the 50-period SMA, and volume above average — then pass that variable to alertcondition().
▶How do I debug an alertcondition() that never fires?
Add a plotshape() call using the same condition. This draws a marker on the chart every time the condition is true. If markers appear where expected, the alert condition is correct and the issue is in your TradingView alert setup. If no markers appear, review your condition logic — check that crossover/crossunder functions are used correctly and that all series are calculated before the condition evaluates.

