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

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

Alerts we program in TradingView don"t show on the chart. That makes it cumbersome to test alert code. Thankfully with a bit of extra code we can highlight alert setups ourselves. Let"s see how we use up and down arrows for that.

IN THIS ARTICLE:

  • Highlight TradingView alerts with up and down arrows
  • 3 steps that show TradingView alerts with arrows
    • Step 1: Translate the alert setup into a Boolean true/false variable
    • Step 2: Plot an arrow based on that Boolean variable
    • Step 3: Use the same Boolean variable with alertcondition()
  • Example: plot arrows on the chart that highlight alert setups
  • Other ways to highlight TradingView alerts
  • Summary
# Highlight TradingView alerts with up and down arrows

Coding TradingView alerts is something we do with the alertcondition() function. Then when our script runs on the chart we activate those alert conditions when we enable and configure alerts by hand. From that point on whenever the alert setup occurs we get an alert pop-up, text message, or email.

But alertcondition() does have a limitation: it doesn"t show alerts on the chart (TradingView, n.d.; TradingView Wiki, 2018). That means we"ll have to inspect the chart ourselves to find historical alert setups. Troubleshooting alerts becomes harder as well: we can"t easily see how different parameters affect alert frequency.

Luckily, TradingView Pine also has features that we can use to highlight alerts ourselves. And all that requires is a bit of extra code. One way to see historical alert setups is to plot up and down arrows on the chart. Let"s see how we program that in TradingView.

# 3 steps that show TradingView alerts with arrows

We do three things to highlight alert setups with up and down arrows:

# Step 1: Translate the alert setup into a Boolean true/false variable

First we make a Boolean true/false variable. We set that variable"s value to true when the alert setup occurs. And have it be false when that setup isn"t present. When we store the alert setup in a variable like this, we can be sure that both the plotted arrows and alerts happen at the same time.

If we got two alert setups, the code for this step can be:

rsiCrossDown = crossunder(rsi(close, 7), rsi(close, 21)) rsiCrossUp = crossover(rsi(close, 7), rsi(close, 21))
# Step 2: Plot an arrow based on that Boolean variable

Next we use that Boolean variable with plotarrow(), a function that plots up and down arrows on the chart (TradingView, n.d.). Which arrow appears depends on the function"s series argument. When that argument is a positive value, an up arrow appear. A negative value gets us a down arrow, and with na no arrow appears.

So to make up and down arrows for the Boolean variables of the previous step we code:

plotarrow(series=rsiCrossDown ? -1 : rsiCrossUp ? 1 : na)
alertcondition()""># Step 3: Use the same Boolean variable with alertcondition()

Then we code the alert condition with TradingView"s alertcondition() function. To have that alert trigger at the same moment the plotted arrows appears on the chart, we execute alertcondition() with the Boolean variable we also used in the previous step.

The code of this step can look like:

alertcondition(condition=rsiCrossDown, message="Quick RSI dropped below slow RSI") alertcondition(condition=rsiCrossUp, message="Quick RSI crossed above slow RSI")

There"s a difference between real-time and historical alert conditions. When an indicator processes historical bars, it calculates once per bar (on bar close). But on real-time data an indicator processes every price update that happens in the chart"s last bar.

That difference often gets us more alerts in real-time than historical bars suggest. Whether this makes your historical alert highlights inaccurate depends on the manual alert settings. If an alert is set to fire intra-bar, it triggers more often on real-time data than historical data suggests. But when the alert may only fire on bar close, then historical and real-time alert setups match.

Let"s see how we apply the above three steps to a complete TradingView indicator.

# Example: plot arrows on the chart that highlight alert setups

The example indicator below triggers alerts for inside and outside bar. An inside bar is a bar whose range is fully inside the bar that preceded it. And an outside bar occurs when a bar fully encompasses its previous bar (Pring, 2002). We have the indicator plot up arrows for inside bars and down arrows to highlight outside bars.

The indicator looks like this on the chart:

TradingView chart with up and down arrows that highlight alertsTradingView chart with up and down arrows that highlight alerts

This is the source code of the complete indicator:

//@version=3 study(title="Alerts highlighted with arrows", overlay=true) // Determine alert condition insideBar = (low > low[1]) and (high < high[1]) outsideBar = (low < low[1]) and (high > high[1]) // Plot arrow plotarrow(series=insideBar ? 1 : outsideBar ? -1 : na, colorup=fuchsia, colordown=yellow, maxheight=30, transp=0) // Create alert conditions alertcondition(condition=insideBar, title="Inside bar", message="The current bar is an inside bar") alertcondition(condition=outsideBar, title="Outside bar", message="The instrument just formed an outside bar")

Let"s take the code apart to see what it does. First we define the indicator"s properties with the study() function:

//@version=3 study(title="Alerts highlighted with arrows", overlay=true)

Then we create true/false variables with the alert setups:

// Determine alert condition insideBar = (low > low[1]) and (high < high[1]) outsideBar = (low < low[1]) and (high > high[1])

An inside bar is fully inside the range of the previous bar. So we look for when the bar"s low is greater than (>) the previous bar"s low (low[1]). And require that the high is less than (<) the previous high (high[1]). When those things happen at the same time, the current bar is an inside bar and our insideBar variable holds true (and false otherwise).

An outside bar has a range outside the preceding bar. So we check if the low is less than the previous low (low < low[1]) while the high is above the previous high (high > high[1]). Since we combine these two comparisons with the and operator, both have to be true before outsideBar becomes true too. When they don"t happen at the same time, outsideBar is false.

Next we use those Boolean variables to plot up and down arrows:

// Plot arrow plotarrow(series=insideBar ? 1 : outsideBar ? -1 : na, colorup=fuchsia, colordown=yellow, maxheight=30, transp=0)

Here we call the plotarrow() function to make up and down arrows. Which of those gets plotted on the current bar depends on its series argument. To set that argument"s value we use two conditional operators here.

The first configuration options checks if insideBar is true. When it is, the operator returns 1 (which gets us an up arrow). Else the second conditional operator (?:) evaluates outsideBar. When that variable is true, this operator returns -1 (for a down arrow). Otherwise when both insideBar and outsideBar are false, we use na to disable a plotted arrow on the current bar.

With the colorup and colordown arguments of plotarrow() we make the arrows appear in fuchsia and yellow. With the maxheight argument we limit the arrows’ size.

Now that we got the arrows on the chart we make the alerts:

// Create alert conditions alertcondition(condition=insideBar, title="Inside bar", message="The current bar is an inside bar") alertcondition(condition=outsideBar, title="Outside bar", message="The instrument just formed an outside bar")

We code alert conditions with TradingView"s alertcondition() function. The first statement here triggers an alert when insideBar is true. The message of that alert says the current bar is an inside bar. Based on our alert settings that message can show as a pop-up, email, or SMS.

The next alertcondition() function call creates an alert condition for outside bars (outsideBar). Note that the conditions we code here only trigger once we create and configure each alert by hand. They won"t automatically fire simply because our script runs on the chart.

# Other ways to highlight TradingView alerts

Besides noting alert setups with up and down arrows, we can bring attention to alerts through other ways. We can use these as an alternative to arrows, or use them alongside arrows. The other options we have are:

  • We can colour the chart"s background when alert setups happen.
  • Or we colour price bars to highlight alerts.
  • A subtle way is to note alert setups with a plotted text character.
  • We can also plot shapes on the chart for alert conditions.
  • We can expand on that by plotting shapes with text for alert setups.
  • But we can also only plot text when alerts happen.
  • And with the plot() function we can show an alert"s trigger level on the chart.

Each of the above approaches also has another benefit: they use an ‘output function’. TradingView requires that each indicator plots or colours something. But alertcondition() does neither. And so an indicator that only generates alerts triggers the ‘script must have at least one output function call’ error. We prevent that error when our code plots or colours on the chart.

# Summary

Alert conditions we code with TradingView"s alertcondition() function don"t appear on the chart. That doesn"t help when we want to test our alert code or need to fix mistakes.

Luckily we can plot up and down arrows to highlight alert setups ourselves. That requires three actions. First we make a Boolean variable for each alert setup. Then we use that variable with TradingView"s plotarrow() function. That function plots up, down, or no arrows based on its series argument. Then in the final step we use the alert setup Boolean variable with alertcondition() to code the actual alert. That way both the arrows and alerts happen simultaneously.

One thing to note is that real-time and historical alert setups can differ. On historical data a TradingView indicator calculates once per bar, on bar close. But with real-time data the script processes every price update that happens in the chart"s last bar. Whether this makes your historical alert highlights underestimate the alert frequency depends on your manual alert settings.

References

Pring, M.J. (2002). Technical Analysis Explained: The Successful Investor"s Guide to Spotting Investment Trends and Turning Points (4th edition). New York, NY: McGraw-Hill.

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

TradingView Wiki (2018, June 29). Alerts in pine. Retrieved on August 8, 2018, from https://www.tradingview.com/wiki/Alerts_in_pine

Published September 21, 2018.
# Related TradingView tutorials
  • Why does my TradingView alert still fire with old settings?

    TradingView remembers script settings when we make an indicator alert. So when we change the indicator, the alert keeps using the old setup.

  • 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 use variables with TradingView alerts?

    TradingView alert messages can include variables with special {{ and }} placeholders. This article show how that feature makes alert messages dynamic.

  • 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 only highlight alerts on real-time price bars in TradingView?

    A real-time TradingView alert can happen with each price update. To only highlight those alerts on the chart, we use the barstate.isrealtime variable.

« All TradingView alerts articles


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