The typical TradingView alerts display the same message each time. But it"s also possible to include dynamic information in our alerts. Let"s find out how.
IN THIS ARTICLE:
Most TradingView alerts have a simple message about the situation that triggered the alert. But we can also include data from variables in our text. We do that with so-called placeholder values.
We use those placeholder values in our alert message text, and they are phrases between a pair of double braces ({{ and }}) (TradingView Blog, 2019). Then when the alert triggers, TradingView replaces those placeholders with the dynamic value. That way our alerts contain up-to-date information.
# Quick example: TradingView alert with variablesLet"s see how those placeholder values work. Say we want to include the instrument"s symbol, the exchange, and the current price in our alert message. We access those with the {{ticker}}, {{exchange}}, and {{close}} placeholders.
We could, for instance, make the alert message like this:
{{exchange}}:{{ticker}}. Buy signal at price = {{close}}.So let"s type that text inside the ‘Message’ field of the ‘Create Alert’ window:
Now when the alert fires, TradingView swaps those placeholders with the real-time information. Here"s how that looks:
This is how our alerts include instrument and price data. But there is more information we can include. Let"s see what options we have.
# Overview: placeholder values for TradingView alertsAll the different placeholders that we can use in our alert messages are (TradingView Blog, 2019):
Placeholder | Description |
---|---|
{{open}} | Open price of the bar on which the alert triggered. |
{{high}} | High of the bar on which the alert fired. |
{{low}} | Low of the bar that the alert generated on. |
{{close}} | Close of the bar on which the alert triggered. This is also the current, most recent price for bars that haven"t closed yet. |
{{volume}} | Current volume of the bar on which the alert triggered. |
{{time}} | Opening time of the bar on which the alert fired. This time is reported in the UTC time zone. |
{{timenow}} | Exact time of when the alert triggered, with seconds precision. This time is also in the UTC time zone. |
{{plot_N}} | Current value of the specified plot number from when the alert triggered. We can reference up to 20 plots with the {{plot_0}} to {{plot_19}} placeholders. |
{{plot(" | Current value from a specific plot when the alert fired. Replace |
{{ticker}} | Ticker (meaning, symbol) from the instrument on which the alert generated. Examples are AAPL, EURUSD, BTCUSD, and ESTX50. |
{{exchange}} | Exchange at which the instrument trades (such as NASDAQ, NYSE, or AMEX). When the alert fired on delayed data, the exchange name ends with ‘DL’ or ‘DLY’ (such as NYMEX_DL). |
The discussion below has additional details on the alert placeholders in the table above.
# Details on bar-based alert placeholdersThe time frame for {{open}}, {{high}}, {{low}}, {{close}}, and {{volume}} depends on how the alert was made (TradingView Blog, 2019):
The time-based placeholders ({{time}} and {{timenow}}) use the following format: yyyy-MM-ddTHH: mm: ssZ (TradingView Blog, 2019).
The letters in that pattern stand for:
Examples of this time format are:
There are two ways to include plot data in an alert message: the {{plot_N}} or {{plot("
Two of the above functions – plotcandle() and plotbar() – make 4 separate plots (for the open, high, low, and close value). With the {{plot_N}} placeholder we have to take into account each of those plots (TradingView Blog, 2019).
So if a script plots a candle (plotcandle()) and moving average (plot()), then there are 5 different plots: {{plot_0}} through {{plot_3}} for the plotted candle, and {{plot_4}} for the moving average.
The additional details on the plot alert placeholders are (TradingView Blog, 2019):
{{plot_N}} has us specify a plot number. But how do we know which number a plot has? There are three ways to find out.
The first option is with the script"s settings window. To open that window, double-click on a plot, or right-click a plot and choose ‘Settings’, or click on the gear icon besides the plot name. Then select the ‘Style’ tab in the script"s settings.
There you"ll see the different plots and their colour settings. Now the first plot listed there is {{plot_0}}, the second plot mentioned is {{plot_1}}, and so on. For example:
The second option is to consult the chart"s ‘Data Window’. There the plots are listed in the same way as we should reference them. So the first plot mentioned is {{plot_0}}, the second plot is {{plot_1}}, and so on. For example:
The third way to figure out plot numbers is to see where the plot functions appear in the code. The first plot statement makes {{plot_0}}, the second creates {{plot_1}}, and so on.
Speaking of code, let"s see how we use plot placeholders with the alertcondition() function.
alertcondition()""># Example indicator: placeholders with alertcondition()When an indicator codes an alert condition with the alertcondition() function, that alert can also use placeholders. That way scripts can also make alerts with dynamic data. The one requirement is that our script uses Pine version 4 or higher (TradingView Blog, 2019).
Here"s a quick example indicator:
This indicator uses the fourth version of Pine (//@version=4). Then we set the indicator"s properties with the study() function.
Next we calculate a 20-bar Exponential Moving Average (EMA) with the ema() function. The plot() function then displays those values on the chart as a regular line plot.
After that we code an alert condition with the alertcondition() function:
We make this alert fire when the bar"s close (close) crosses above or below (cross()) the moving average (emaValue). In that situation, an alert message displays with three dynamic components.
The first is {{ticker}}, which includes the symbol on which the alert fired. We also include the value of our moving average. Since we cannot insert a variable directly, we include the plot with that moving average value. So we use {{plot(\"EMA\")}}. The last dynamic value is the instrument"s current price. We fetch that one with {{close}}.
When the alerts of this indicator fires, here"s what we see:
So to include the value of a plot we wrap that plot"s name in double quotes (") like so: {{plot("RSI")}}. But what if the alert message that alertcondition() makes a already uses double quotes?
Here"s an example of that situation:
This alert message is between " and ". But the plot name also uses those quotes. That makes TradingView think we got two separate strings here: "Crossed above {{plot(" and ")}}.". That of course gives an error.
There are two ways to fix that situation. The first option is to escape the quoted plot name with a backslash (\) like so:
The alternative is to use single quotes (") with the alert message, and use double quotes around the plot name:
It seems like we always have to use double quotes (") around the plot name.
At the time of writing, placing the plot name in single quotes, while the string is double-quoted, didn"t work (for example, "Plot value = {{plot("EMA")}}"). So instead use one of the two options above.
TradingView alerts can contain dynamic information with so-called placeholder values. Those ‘variables’ between double braces ({{ and }}) are replaced by TradingView with real-time information as soon as the alert fires. That makes it possible to include price and instrument data.
We can also include plot values, either by number ({{plot_0}}) or name ({{plot("RSI MA")}}). When we reference plots by number, we have to use the order in which they appear in the ‘Data Window’ and ‘Style’ settings window.
The alertcondition() function can also use alert placeholders. That way our custom indicators generate alerts with dynamic values.
TradingView Blog (2019, October 31). Introducing variables in alerts. Retrieved on November 12, 2019, from https://www.tradingview.com/blog/en/introducing-variables-in-alerts-14880/
Alerts we code with alertcondition() don"t show on the chart. But we can display an alert"s trigger levels with TradingView"s plot() function.
TradingView alerts we code with alertcondition() don"t show on the chart. But we can draw shapes when they happen with the plotshape() function.
The alertcondition() function doesn"t highlight alerts on the chart. But TradingView"s plotarrow() function can note alerts with up and down arrows.
When an indicator"s alerts identify trading setups, there are just a few code adjustments to turn that script into a TradingView trading strategy.
In TradingView we can programmatically generate alerts that use values from another indicator. We do that with the indicator on indicator feature.