Pine Script – Lesson 5: How To Create Alerts

Pine Script Basics Course Pine Script Mastery Course

How to Create TradingView Alerts

NOTE: This lesson is out-dated and for Version 4 syntax. To see my latest FREE Pine Script V5 lessons complete with source code, check out my YouTube Pine Script Tutorials page.

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:

How To Set TradingView Alerts

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 straightforward to understand, but I’ll break them down below.

TradingView Alert Options


Condition

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.


Options

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.


Expiration Time

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.


More Information

If you want more information on how to set complex TradingView alerts then you should check out my TradingView Alert Guide by clicking here!

Advanced Course

If you want to take your Pine Script coding to the next level, then I think you’ll be interested in my Pine Script Mastery Course.

If you liked this free content then I promise that you’ll love my premium content where I am able to go into much greater detail and help answer students’ questions!



Source Code

//@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")

OVERVIEW


Free Premium Charts!

5 1 vote
Article Rating
Subscribe
Notify of
guest
6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Dar J
Dar J
3 years ago

I received an error building on previous lesson. I got it to work when I have the variables as rsiOB and rsiOS not isRsiOB is isRsiOS.

Last edited 3 years ago by Dar J
Jan
Jan
2 years ago

This code is not working.

John
John
Reply to  mjslabosz
2 years ago

For what it’s worth I also just copied the code into Pine Editor, added it and it’s working fine for me.

aml
Reply to  mjslabosz
1 year ago

Hi ,
Is the alert appears as triangle only , because I selected sound from setting , but no response , is not?
Regards

Karthik
Karthik
1 year ago

How to create alert condition based on previous candle. Example, if current 5 min candle is engulfing the previous 5 min candle, then system should alert BULLISH ENGULFING