In this post, we are going to look at Tradingview alerts, how to create them and address some questions you might be asking yourself about them. With that in mind, I think the best place to start is to create simple script an alert in the code. Then we can look at how to subscribe to it and some final points to be aware of.
Creating an alert is nice and easy. There is a built-in function we can call that is appropriately named
alertcondition(). The function itself has 3 input parameters which are well explained in the pine script reference manual. Having said that, it will not will hurt cover them again here:
condition: This is the condition that triggers the alert. For example,
closeis greater than a certain value.
title: This is an optional parameter but in my opinion, it should be mandatory. If you do not provide a title, it becomes very hard to find the right alert when the time comes to subscribe to it.
message: This is again optional. It contains the message that appears on the alert. This can be used to remind you what the alert is signalling.
Briefly going back to the title parameter, an example of why adding a title to the alert is always a good idea is below. The image shows an indicator which contains 4 alerts, each without a title. How do you know which one to subscribe to?
The following example code is a simple indicator that will plot two lines above and below the opening price of a bar. The distance between the lines is controllable by an input. This allows us to easily trigger an alert condition by moving the boundaries closer to price.
study("Alert Tester", overlay=true)
x = input(defval=0.00005, title='Min Pip Movement to trigger altert', type=float)
upper = open + x
lower = open - x
alertcondition(high > upper, title='High > Upper Bound', message='The bar high has broken the upper boundary')
alertcondition(low < lower, title='Low < Lower Bound', message='The bar low has broken the lower boundary')
plot(upper, title='Upper Boundary')
plot(lower, title='Lower Boundary')
As you can see the default value is half a pipette. As such, this script should be used with a Forex pair for testing. Load the indicator up on a chart and you should see something like this:
Next, we need to create an alert. This is a pretty straightforward. A gallery below provides a step by step overview using the code. Before you follow that though, make sure you add the indicator to the chart. The indicator must be on the chart prior to creating the alert.
Some additional information that details the various preferences can be found on the Tradingview wiki here:
Note: The alert in this example is purposefully simple for testing the
alertcondition()function. However, in the real world, there is no point in creating an alert in the code to catch when price crosses a plotted line. This can be done without adding an alert condition in the code (see below). It is better to use the
alertcondition()function for more complex conditions.
Frequently (or not so frequently) Asked Questions
The following is a set of questions I asked myself when exploring alerts. Consider this an unofficial FAQ with answers derived from my testing. Given this, if anyone spots a mistake or flaw in the testing, please mention it in the comments below and I will take a look!
Why is the plot box / list of plots needed when creating an alert?
It is for creating further custom alerts inside the wizard. For example, you could trigger an alert when the upper boundary reaches a fixed level. Providing access to the plots allows end users to create their own custom alerts without touching the code. This also goes back to the note above about not needing to code alerts for simple conditions as they can be setup here without needing to add any further lines/complexity to your code.
For the purpose of this post, when creating one of our custom alerts (e.g.
Low < Lower Bound), it does not matter whether the
Upper Bound or
Lower Bound plot is selected. You can ignore this box.
Does the indicator need to be left on the chart?
But as mentioned above, it does have to be on screen when you create the alert.
Does the chart need to be left on screen?
It also does not need to be. You can go away and browse other charts. An alert will pop up when the condition is met.
Are the default indicator values used for all alerts?
No, you can change the parameters and create an alert using that custom parameter set. Tradingview will remember the settings and monitor that. You can test this by:
- Add the indicator to the chart
- Edit the parameters (to something other than the default values)
- Create the alert.
- Remove and re-add the indicator
- Edit the alert
A note about email alerts
Be wary about relying on email alerts for time-sensitive entries. Depending on your email client and service provider, you might experience a delay in receiving the mail.