Topics like health, travel, finance, home, living, education and careers | Media Search Home

How to use variables with TradingView alerts?

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:

  • Create TradingView alerts with variable data: here"s how
    • Quick example: TradingView alert with variables
  • Overview: placeholder values for TradingView alerts
    • Details on bar-based alert placeholders
    • Time format used by the alert placeholders
    • A closer look at plot placeholders for alert messages
  • How to determine the number of a plot?
  • Example indicator: placeholders with alertcondition()
    • How to code plot names in a text string?
  • Summary
# Create TradingView alerts with variable data: here"s how

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 variables

Let"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:

Creating a TradingView alert with dynamic valuesCreating a TradingView alert with dynamic values

Now when the alert fires, TradingView swaps those placeholders with the real-time information. Here"s how that looks:

TradingView alert with placeholder valuesTradingView alert with placeholder values

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 alerts

All the different placeholders that we can use in our alert messages are (TradingView Blog, 2019):

PlaceholderDescription
{{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 with your plot"s name, such as {{plot("Volume")}} or {{plot("RSI MA")}}.
{{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 placeholders

The time frame for {{open}}, {{high}}, {{low}}, {{close}}, and {{volume}} depends on how the alert was made (TradingView Blog, 2019):

  • Price-based alerts report data from a 1-minute time frame.
  • Alerts based on indicators, drawings, and those on exotic chart types use the chart"s time frame.
# Time format used by the alert placeholders

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:

  • yyyy: the four digit year (such as 2020).
  • MM: month of the year with leading zero for single digits (01 to 12).
  • dd: day of the month with leading zero for single digits (01 to 31).
  • T to separate date from time,
  • HH: hours with a leading zero for single digit hours (00 to 23).
  • mm: minutes with leading zero for single digits (00 to 59).
  • ss: seconds with leading zero for single digits (00 to 59).
  • Z signals that the time has no UTC offset (so it"s reported in the UTC time zone, UTC+0).

Examples of this time format are:

  • 2019-11-18T09: 56: 00Z for November 18, 2019 at 9:56 a.m.
  • 2020-01-08T14: 20: 00Z for 2:20 pm on January 8, 2020.
# A closer look at plot placeholders for alert messages

There are two ways to include plot data in an alert message: the {{plot_N}} or {{plot("")}} placeholders. But what are those ‘plots’ we can include? Well, any made by one of the following functions (TradingView Blog, 2019):

  • Series of data plotted by the plot() function.
  • Shapes made by the plotshape() function.
  • Characters plotted by the plotchar() function.
  • Up and down arrows made by the plotarrow() function.
  • Price bars created by the plotbar() function.
  • And price candles plotted by the plotcandle() function.

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):

  • When one of the above functions uses Boolean true/false values as their plotted series, then TradingView replaces those in the alert message with 0 (false) or 1 (true). This is another way of saying that each plot shows in the alert message as a numerical value.
  • With {{plot_N}} or {{plot("")}} we can only include plots made by the script that generates the alert. So when we make an alert on the Relative Strength Index (RSI) indicator, then we cannot include a plot from the Simple Moving Average (SMA) script.
  • With standard, built-in TradingView indicators we use the English plot name for the {{plot("")}} placeholder. And not the localised, translated plot name. (For custom scripts we use the name specified by the title argument in our code.)
# How to determine the number of a plot?

{{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:

Counting alert plots from TradingView script settingsCounting alert plots from TradingView script settings

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:

Counting alert plots from the Data WindowCounting alert plots from the Data Window

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:

//@version=4 study(title="Alert with placeholders", overlay=true) // Calculate and plot moving average emaValue = ema(close, 20) plot(series=emaValue, title="EMA") // Make alert condition alertcondition(condition=cross(close, emaValue), title="EMA Cross", message="{{ticker}} crossed EMA ({{plot(\"EMA\")}})." + "Price is {{close}}.")

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:

// Make alert condition alertcondition(condition=cross(close, emaValue), title="EMA Cross", message="{{ticker}} crossed EMA ({{plot(\"EMA\")}})." + "Price is {{close}}.")

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:

Example of TradingView alert with indicator variableExample of TradingView alert with indicator variable
FURTHER READING

  • Generate TradingView alerts programmatically
  • Round values in TradingView Pine
# How to code plot names in a text string?

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:

alertcondition(condition=crossover(close, rsi(close, 12)), message="Crossed above {{plot("EMA")}}.")

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:

alertcondition(condition=crossover(close, rsi(close, 12)), message="Crossed above {{plot(\"EMA\")}}.")

The alternative is to use single quotes (") with the alert message, and use double quotes around the plot name:

alertcondition(condition=crossover(close, rsi(close, 12)), message='Crossed above {{plot("EMA")}}.')

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.

# Summary

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.

References

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/

Published December 6, 2019.
# Related TradingView tutorials
  • Tutorial: highlight TradingView alerts by plotting their trigger level on the chart

    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.

  • How to show when alerts happen with TradingView"s plotted shapes?

    TradingView alerts we code with alertcondition() don"t show on the chart. But we can draw shapes when they happen with the plotshape() function.

  • Here"s how to highlight TradingView alert setups with up and down arrows

    The alertcondition() function doesn"t highlight alerts on the chart. But TradingView"s plotarrow() function can note alerts with up and down arrows.

  • How to turn a TradingView alert into a trading strategy?

    When an indicator"s alerts identify trading setups, there are just a few code adjustments to turn that script into a TradingView trading strategy.

  • How to fire TradingView alerts based on data from another indicator?

    In TradingView we can programmatically generate alerts that use values from another indicator. We do that with the indicator on indicator feature.

« All TradingView alerts articles


Email: contact@about.com.vn - Phone: 818.333.007 - Website: Media Search Home
Copyright © 2007 - Noos. All rights reserved