The alertcondition() function creates an alert condition in the chart’s ‘Create Alert’ window (TradingView, n.d.). At this time, only indicator script can create alert conditions.

The alertcondition() function has the following default syntax (TradingView, n.d.; TradingView Wiki, 2017a):

alertcondition(condition, title, message)
  • condition: a required true/false value that can make the alert fire (true) or not (false). Alerts only trigger on the last bar of the chart and when the manual alert settings allow the alert to be generated. And so an alert doesn’t always fire just because the condition argument of the alertcondition() function is true.
  • title: optional string argument that sets the alert’s name. A descriptive name helps to find a specific alert in the ‘Create Alert’ window.
  • message: optional string argument with the alert message. An informational text makes it easier to know which alert fired and why. We can also type in an alert message in the ‘Create Alert’ window by hand.

Example: trigger moving average TradingView alerts

The example indicator below triggers alerts when the 9-bar Exponential Moving Average (EMA) crosses the 28-bar EMA. But first we set the indicator settings with the study() function. Then we make two input options with the input() function: the first sets the length of the fast moving average while the second specifies how many bars the slower MA uses.

Next we use the ema() function to calculate the EMA values based on closing prices (close). We refer here to the input options to determine how many bars the moving averages should calculate on. Then we plot the EMAs on the chart with the plot() function. The faster moving average shows with the orange colour while the slower one is thicker (linewidth=4) and coloured with teal.

Then we make two alert conditions with the alertcondition() function. The condition of the first is when the faster moving average (fastMA) crosses above the slower moving average (slowMA). We test whether that condition is true or false with the crossover() function. We set this alert’s title to "EMA Cross Above". And with the message argument of alertcondition() we say that the faster EMA crossed over the slower one.

The second alertcondition() function call is much the same, except now we monitor if the fast 9-bar EMA crossed under the slower 28-bar EMA. We check for that condition with the crossunder() function.

//@version=3
study(title="Reference indicator - alert example", overlay=true)

// Input options
maLen1 = input(title="Fast MA", type=integer, defval=9)
maLen2 = input(title="Slow MA", type=integer, defval=28)

// Calculate values
fastMA = ema(close, maLen1)
slowMA = ema(close, maLen2)

// Plot values
plot(series=fastMA, color=orange, linewidth=2)
plot(series=slowMA, color=teal, linewidth=4)

// Create alert conditions
alertcondition(condition=crossover(fastMA, slowMA), 
     title="EMA Cross Above",
     message="The quick EMA crossed over the slow EMA.")

alertcondition(condition=crossunder(fastMA, slowMA),
     title="EMA Cross Under",
     message="The quick EMA dropped below the slow EMA.")

The two EMAs plotted by the example indicator look like:

Plotting Exponential Moving Averages (EMAs) in TradingView Pine

When one of the alert conditions triggers, the alert pop-up message looks like:

Generating an alert in TradingView Pine

Notes for alertcondition()

Even though the alertcondition() function creates an alert condition programmatically, there are still several manual steps before the alert actually appears.

We first have to add the indicator that uses the alertcondition() function to the chart. Then we click on the ‘Alerts’ button in the right-hand side of the screen:

Opening the Alerts side panel in TradingView

Doing so opens the alert panel in the right of the chart. Next we click on the ‘Set Alert’ button in that panel (or press the Alt - A keyboard shortcut):

Opening the 'Create Alert' window in TradingView

This brings up the ‘Create Alert’ window. Here we create the actual indicator alert, but we first click on the pull-down menu behind ‘Condition’ to select our indicator whose code uses the alertcondition() function. That way we don’t generate alerts based on the instrument.

Selecting the indicator to generate a TradingView alert with

After we select the indicator script that has the alertcondition() function in it, we can configure the alert settings in the ‘Create Alert’ window:

Configuring an alert in TradingView Pine

Then after we confirm the settings and activate the alert, it appears in the ‘Manage Alerts’ side panel:

Overview of all alerts in TradingView Pine

TradingView alerts only trigger for the chart’s last price bar and when there’s incoming real-time data. Without incoming price ticks, the alert condition isn’t checked and therefore the alert itself can’t fire. This is an important point to keep in mind when we trigger alerts based on time.

Since we don’t know how often and when our instrument has a price update, we can’t say how often the alert condition is checked. And so it’s a guess in which time period our alert gets checked. Our intra-bar alert might be evaluated every second (when the instrument trades actively) or it could be very 20 seconds or so when the instrument trades slowly. (We could fix this by checking the alert condition based on a time interval, like every second. But that’s currently not possible in TradingView Pine.)

A consequence is that we cannot trigger TradingView alerts with a specific point in time, like firing an alert 15 seconds before the bar closes. To increase the chance of such alerts actually firing, we can create a wide range (such as triggering an alert 45 to 15 seconds before the bar closes).


A single alertcondition() function call only creates one alert condition that we can then add to the chart. If we want to create different alerts based on other conditions, we’ll have to use the alertcondition() function repeatedly in our script (TradingView Wiki, 2017a).


We can use the alertcondition() function in strategy scripts and save the code without getting an error message. But when we add the strategy script to the chart the alert condition won’t appear in the ‘Create Alerts’ window.

That unfortunately means we cannot trigger alerts with strategy scripts. And so for now we’ll have to spend time and effort in replicating our strategy code in an indicator, and use the exact same settings in both scripts. (Unfortunately, not all strategy data is available to indicators and so this workaround doesn’t work 100% of the time.)


At this time we cannot use the tostring() function to create dynamic alert messages. That means things like the current price, an indicator value, or price target cannot be included in the message created by alertcondition().

Tips for alertcondition()

The alertcondition() function only creates an alert condition; it does not trigger alerts (TradingView, n.d.; TradingView Wiki, 2017a). All that alertcondition() does is make an extra alert condition in the ‘Create Alert’ window, which we then can configure and enable by hand. So even though we can program an alert condition in the code, there still several manual actions needed to have the script trigger the actual alert.


When we configure and create an alert, TradingView remembers the alert settings even when we change the chart or adjust the indicator settings (TradingView Wiki, 2017b). This way we don’t need to keep the chart open or keep using the same indicator for the alert to fire.

But unfortunately, adjusting alert conditions is a bit cumbersome: first we’ll have to remove the old alert, then reconfigure the chart and indicator for the new alert settings, and then we re-create the alert with the ‘Create Alert’ window. So it’s best to double check your alert settings before you change the original chart and/or the indicator settings.


While we create an alert condition with alertcondition(), this function cannot show us on which historical bars the alert would have fired. But to troubleshoot alert code, it really helps to see on which bars the alert could have fired.

Luckily, we can highlight those alert bars in different ways. One approach is with the bgcolor() function, which colours the chart’s background from top to bottom. Now to have that function only colour when there’s an alert condition on that bar, we can set it to a conditional colour with the conditional operator (?:). An example of that is the following script, which colours the background orange whenever the upCross or downCross variables are true. (When those variables are false, we use na to turn off colouring with bgcolor() on that bar.)

//@version=3
study(title="Reference indicator - alert example", overlay=true)

// Input option
maLen = input(title="MA Length", type=integer, defval=9)

// Calculate value
maValue = sma(close, maLen)

// Plot value
plot(series=maValue, color=purple, linewidth=2)

// Create alert conditions
upCross   = crossover(close, maValue)
downCross = crossunder(close, maValue)

alertcondition(condition=upCross, 
     title="Cross Above Close",
     message="The bar's closing price crossed above the MA.")

alertcondition(condition=downCross,
     title="Cross Under Close",
     message="The close dropped below the MA.")

// Colour when an alert condition happens
bgcolor(color=upCross or downCross ? orange : na,
     transp=90)

When added to a chart, we see that this indicator highlights all price bars where the bar’s closing price crossed above the 9-bar SMA:

Example of highlighting historical alerts in TradingView Pine

This approach does have the limitation that we cannot see inside historical bars to check whether a ‘Once’ or ‘Once per bar (on condition)’ alert condition would have triggered. Instead, at this time an indicator can only calculate on the close of historical bars and so we can only highlight those bars on which an on-bar-close alert triggered.

Summary

Features of alertcondition()
Name alertcondition()
Description Creates an alert condition that we can enable in the ‘Create Alert’ window after adding the indicator to the chart.
Category Alerts
Type function
Returns Nothing
Usable in Indicator (study) scripts
Available since (beta release date) TradingView Pine Version 2 (January 22, 2016)

References

TradingView (n.d.). Pine Script Language Reference Manual. Retrieved on October 16, 2017, from https://www.tradingview.com/study-script-reference/

TradingView Wiki (2017a, September 30). Alerts in pine. Retrieved on October 16, 2017, from https://www.tradingview.com/wiki/Alerts_in_pine

TradingView Wiki (2017b, September 22). Creating and Managing Alerts. Retrieved on October 16, 2017, from https://www.tradingview.com/wiki/Creating_and_Managing_Alerts