Pine Script – Lesson 5: How To Create Alerts
How to Create TradingView Alerts
This Pine Script lesson will cover how to add TradingView alerts to your scripts.
I will be using the Pine Script we created in Lesson 4 titled ‘How to Make the RSI Indicator Generate Trading Signals‘ to demonstrate this example.
In that lesson I showed you how to create visual signals on the chart when the RSI goes overbought or oversold. Now we will add push-notification alerts to the script.
Types of TradingView Alerts
TradingView has several push-notification types.
Notify on App only works if you first download the (free) TradingView app (Android or iOS). If you have the app installed and a trading alert is triggered then you will receive a push-notification straight to your mobile device.
Show Popup will display a window popup inside your TradingView chart window when an alert is triggered. The popup will display on every chart you have open.
Send Email sends you an email whenever an alert is triggered. You can specify the message that is sent so that you know which indicator triggered the email alert.
Play Sound plays an audio alert whenever a signal is triggered. You can select from 8 different sounds and you can choose how long the sound plays for. This is a fantastic tool for day-traders and I use it every day.
Send Email-to-SMS will send you a text message to your phone. In order to use this feature you must have a phone data carrier that allows email-to-SMS functionality (eg. MobileNumber@online.telstra.com.au).
How To Add Alert Functionality To Scripts
Now let’s do some quick coding.
Adding alert functionality to your scripts is incredibly easy. You can do it in a single line of code.
To add alert functionality to our RSI Signal indicator, simply add this line of code to the end of the script:
alertcondition(isRsiOB or isRsiOS, title="RSI Alert!", message="RSI signal for XXX")
How To Set TradingView Alerts
Now you can easily set alerts from the TradingView alerts menu, like so:
You can set alerts a multitude of ways. The easiest way is to just click on the Alert menu and then click the Plus button. Then you can choose what to set an alert for and the conditions that trigger it.
The alert options are fairly straight-forward to understand, but I’ll break them down below.
This section sets the alert condition. You must click on the first box and select the script that you want to generate your trading signal.
Because our RSI script generates trading alerts whether the RSI is overbought or oversold (thanks to the line ‘isRsiOB or isRsiOS’) you do not need to touch the second box.
The third box below the first two is what generates our trading signal in this case, which is why the alert will not respond to the Overbought or Oversold setting.
Normally if you do not have a script selected then this lower box will let you choose a multitude of regular alert options. You can use it to detect breakouts (ie. price crossing a horizontal line or even a diagonal trend-line) or if price moves a certain % intraday.
These four boxes let you choose when the alert is triggered on a candle-basis.
If you set it to Only Once then the alert will trigger a single time and it will trigger during the current candle without waiting for it to close.
If you set it to Once Per Bar then it will trigger once per bar but will repeat on any new bars.
If you set it to Once Per Bar Close it will only trigger when the candle closes. This is the most common setting that you will use for trading signals.
Once Per Minute will trigger the alert on any current candle multiple times, but only once per every minute.
This section lets you set the expiration time of the alert.
How long you can have the alert active depends on your membership plan. On a free account you are limited to only 1 alert per account and the maximum alert time is 2 months.
On a Pro account you can set multiple alerts and on the premium account you can use the Open-ended setting which will leave the alert active forever until you manually disable it.
You can set any date within your maximum time period and any time of day. Expiry times are useful for tracking day-trading setups with a limited time window, but most of the time you will want to set the alert for the maximum time possible.
//@version=4 study(title="Lesson 5", shorttitle="RSI Signal", overlay=true) // Get user input rsiSource = input(title="RSI Source", type=input.source, defval=close) rsiLength = input(title="RSI Length", type=input.integer, defval=14) rsiOverbought = input(title="RSI Overbought Level", type=input.integer, defval=80) rsiOversold = input(title="RSI Oversold Level", type=input.integer, defval=20) // Get RSI value rsiValue = rsi(rsiSource, rsiLength) isRsiOB = rsiValue >= rsiOverbought isRsiOS = rsiValue <= rsiOversold // Plot signals to chart plotshape(isRsiOB, title="Overbought", location=location.abovebar, color=color.red, transp=0, style=shape.triangledown, text="OB") plotshape(isRsiOS, title="Oversold", location=location.belowbar, color=color.green, transp=0, style=shape.triangleup, text="OS") // Send out an alert if this candle meets our conditions alertcondition(isRsiOB or isRsiOS, title="RSI Alert!", message="RSI signal for XXX")