EA Setup
Alerts
Message Structure
Expert Advisor
Debugging
Others
Low-Code Alert
In this basic alert module, you will learn how to add arrows on your entries and add alerts with fixed messages. In the intermediate alert module, you will learn how to send in dynamic alert messages.
1. Basic
In this example, the script is on PineScript Version 5 — evident from:
//@version=5
1.1 Determine your Script Type
On the first few lines of your script, you should see the word “strategy” or “indicator”.
//@version=5
strategy('Supertrend Strategy - PineConnector', overlay=true)
//@version=5
indicator('AlphaTrend - PineConnector', overlay=true)
1.2 Strategy
In this example, we will use the following SuperTrend strategy script.
//@version=5
strategy('Supertrend Strategy', overlay=true)
[supertrend, direction] = ta.supertrend(3, 10)
bodyMiddle = plot((open + close) / 2, display=display.none)
upTrend = plot(direction < 0 ? supertrend : na, 'Up Trend', color=color.new(color.green, 0), style=plot.style_linebr)
downTrend = plot(direction < 0 ? na : supertrend, 'Down Trend', color=color.new(color.red, 0), style=plot.style_linebr)
fill(bodyMiddle, upTrend, color.new(color.green, 90), fillgaps=false)
fill(bodyMiddle, downTrend, color.new(color.red, 90), fillgaps=false)
if ta.change(direction) < 0
strategy.entry('My Long Entry Id', strategy.long)
if ta.change(direction) > 0
strategy.entry('My Short Entry Id', strategy.short)
1.2.1 Add Visual Confirmation
We are trying to identify the exact entry conditions and adding a visual confirmation. Add the plotshape code at the bottom of your script:
plotshape(LongEntryCondition, style=shape.labelup, location=location.belowbar, color=color.new(#046ff9, 0), size=size.large, text='PineConnector \n Buy', textcolor=color.new(color.white, 0)) //plotting up arrow when buy/long conditions met
plotshape(ShortEntryCondition, style=shape.labeldown, location=location.abovebar, color=color.new(#046ff9, 0), size=size.large, text='PineConnector \n Sell', textcolor=color.new(color.white, 0)) //plotting down arrow when sell/short conditions met
strategy.entry.
Example
The underlined and bolded text are the Actual Entry Conditions.
if ta.change(direction) < 0
strategy.entry('My Long Entry Id', strategy.long)
if ta.change(direction) > 0
strategy.entry('My Short Entry Id', strategy.short)The updated code is now as follows:
//@version=5
strategy('Supertrend Strategy', overlay=true)
[supertrend, direction] = ta.supertrend(3, 10)
bodyMiddle = plot((open + close) / 2, display=display.none)
upTrend = plot(direction < 0 ? supertrend : na, 'Up Trend', color=color.new(color.green, 0), style=plot.style_linebr)
downTrend = plot(direction < 0 ? na : supertrend, 'Down Trend', color=color.new(color.red, 0), style=plot.style_linebr)
fill(bodyMiddle, upTrend, color.new(color.green, 90), fillgaps=false)
fill(bodyMiddle, downTrend, color.new(color.red, 90), fillgaps=false)
if ta.change(direction) < 0
strategy.entry('My Long Entry Id', strategy.long)
if ta.change(direction) > 0
strategy.entry('My Short Entry Id', strategy.short)
plotshape(ta.change(direction) < 0, style=shape.labelup, location=location.belowbar, color=color.new(#046ff9, 0), size=size.large, text='PineConnector \n Buy', textcolor=color.new(color.white, 0)) //plotting up arrow when buy/long conditions met
plotshape(ta.change(direction) > 0, style=shape.labeldown, location=location.abovebar, color=color.new(#046ff9, 0), size=size.large, text='PineConnector \n Sell', textcolor=color.new(color.white, 0)) //plotting down arrow when sell/short conditions met
Save the code and click “Add to chart”.
You should see the PineConnector Blue arrows, indicating the Buy and Sell instances.
1.2.2 Add the Alert Function
Once you have successfully plotted the arrows where they appear 1 candle prior to the strategy’s entries, add the alert codes. The alert codes are to appear near the “strategy.entry” codes, usually found at the bottom few rows.
Since the first “strategy.entry” is “strategy.long”, we inject the following code:
alert('LicenseID,buy,EURUSD,risk=1', alert.freq_once_per_bar_close)
Since the second “strategy.entry” is “strategy.short”, we inject the following code:
alert('LicenseID,sell,EURUSD,risk=1', alert.freq_once_per_bar_close)
After adding the arrows and alert function, you should have the following:
//@version=5
strategy('Supertrend Strategy', overlay=true)
[supertrend, direction] = ta.supertrend(3, 10)
bodyMiddle = plot((open + close) / 2, display=display.none)
upTrend = plot(direction < 0 ? supertrend : na, 'Up Trend', color=color.new(color.green, 0), style=plot.style_linebr)
downTrend = plot(direction < 0 ? na : supertrend, 'Down Trend', color=color.new(color.red, 0), style=plot.style_linebr)
fill(bodyMiddle, upTrend, color.new(color.green, 90), fillgaps=false)
fill(bodyMiddle, downTrend, color.new(color.red, 90), fillgaps=false)
if ta.change(direction) < 0
strategy.entry('My Long Entry Id', strategy.long)
alert('60123456789,buy,EURUSD,risk=1', alert.freq_once_per_bar_close)
if ta.change(direction) > 0
strategy.entry('My Short Entry Id', strategy.short)
alert('60123456789,sell,EURUSD,risk=1', alert.freq_once_per_bar_close)
plotshape(ta.change(direction) < 0, style=shape.labelup, location=location.belowbar, color=color.new(#046ff9, 0), size=size.large, text='PineConnector \n Buy', textcolor=color.new(color.white, 0)) //plotting up arrow when buy/long conditions met
plotshape(ta.change(direction) > 0, style=shape.labeldown, location=location.abovebar, color=color.new(#046ff9, 0), size=size.large, text='PineConnector \n Sell', textcolor=color.new(color.white, 0)) //plotting down arrow when sell/short conditions met
After ensuring you’ve updated “LicenseID”, “LongEntryCondition” and “ShortEntryCondition”, save the modified script.
1.2.3 Create the Alert
Adding the alert code in your strategy does not create alerts. You will have to create alerts on the symbol and timeframe of your choice.
1.2.3.1 Create an Alert
Press Alt+A (Windows) or Option+A (Mac), to create an alert.
1.2.3.2 Configure Alert
Configure your alert as per the following:
- Condition: Supertrend Strategy, alert() function calls only
- Webhook URL: https://pineconnector.net/webhook/
Select “Create” once you have configured the alert.
1.2.3.3 Alert Firing
Once your strategy’s entry conditions are met, you should see a blue arrow plotted on the chart and an alert should fire.
You should see a new EURUSD position on your MetaTrader terminal.
1.3 Indicator
In this example, we will use the following EMA indicator script.
//@version=5
indicator("EMA", overlay=true)
ema20 = ta.ema(close,20)
ema50 = ta.ema(close,50)
plot(ema20, color=color.new(color.blue, 5))
plot(ema50, color=color.new(color.red, 5))
long = ta.crossover(ema20, ema50) and close > ema20
short = ta.crossunder(ema20, ema50) and close < ema20
1.3.1 Add Visual Confirmation
We are trying to identify the exact entry conditions and adding a visual confirmation.
Add the plotshape() code at the bottom of your script:
plotshape(LongEntryCondition, style=shape.labelup, location=location.belowbar, color=color.new(#046ff9, 0), size=size.large, text='PineConnector \n Buy', textcolor=color.new(color.white, 0)) //plotting up arrow when buy/long conditions met
plotshape(ShortEntryCondition, style=shape.labeldown, location=location.abovebar, color=color.new(#046ff9, 0), size=size.large, text='PineConnector \n Sell', textcolor=color.new(color.white, 0)) //plotting down arrow when sell/short conditions met
The updated code is now as follows:
//@version=5
indicator("EMA", overlay=true)
ema20 = ta.ema(close,20)
ema50 = ta.ema(close,50)
plot(ema20, color=color.new(color.blue, 5))
plot(ema50, color=color.new(color.red, 5))
long = ta.crossover(ema20, ema50) and close > ema20
short = ta.crossunder(ema20, ema50) and close < ema20
plotshape(long, style=shape.labelup, location=location.belowbar, color=color.new(#046ff9, 0), size=size.large, text='PineConnector \n Buy', textcolor=color.new(color.white, 0)) //plotting up arrow when buy/long conditions met
plotshape(short, style=shape.labeldown, location=location.abovebar, color=color.new(#046ff9, 0), size=size.large, text='PineConnector \n Sell', textcolor=color.new(color.white, 0)) //plotting down arrow when sell/short conditions met
Save the code, and click “Add to chart”.
You should see the PineConnector Blue arrows, indicating the Buy and Sell instances.
1.3.2 Add the Alert Function
Once you have successfully plotted the arrows where the entries should be, add the alert codes:
if LongEntryCondition
alert('LicenseID,buy,EURUSD,risk=1', alert.freq_once_per_bar_close)
if ShortEntryCondition
alert('LicenseID,buy,EURUSD,risk=1', alert.freq_once_per_bar_close)
After adding the arrows and alert function, you should have the following:
//@version=5
indicator("EMA", overlay=true)
ema20 = ta.ema(close,20)
ema50 = ta.ema(close,50)
plot(ema20, color=color.new(color.blue, 5))
plot(ema50, color=color.new(color.red, 5))
long = ta.crossover(ema20, ema50) and close > ema20
short = ta.crossunder(ema20, ema50) and close < ema20
plotshape(long, style=shape.labelup, location=location.belowbar, color=color.new(#046ff9, 0), size=size.large, text='PineConnector \n Buy', textcolor=color.new(color.white, 0)) //plotting up arrow when buy/long conditions met
plotshape(short, style=shape.labeldown, location=location.abovebar, color=color.new(#046ff9, 0), size=size.large, text='PineConnector \n Sell', textcolor=color.new(color.white, 0)) //plotting down arrow when sell/short conditions met
if long
alert('60123456789,buy,EURUSD,risk=1', alert.freq_once_per_bar_close)
if short
alert('60123456789,buy,EURUSD,risk=1', alert.freq_once_per_bar_close)
After ensuring you’ve updated “LicenseID”, “LongEntryCondition” and “ShortEntryCondition”, save the modified script.
1.3.3 Create the Alert
Adding the alert code in your indicator does not create alerts. You will have to create alerts on the symbol and timeframe of your choice.
1.3.3.1 Create an Alert
Press Alt+A (Windows) or Option+A (Mac), to create an alert.
1.3.3.2 Configure Alert
Configure your alert as per the following, and create click “Create”.
- Condition: EMA, Any alert() function calls only
- Webhook URL: https://pineconnector.net/webhook/
1.3.3.3 Alert Firing
Once your indicator’s entry conditions are met, you should see a blue arrow plotted on the chart and an alert should fire concurrently. There will be an entry in the Alerts Log with the timestamp.
You should see a new EURUSD position on your MetaTrader terminal.
2. Intermediate
In the basic low-code module, you learnt where to place the plotshape() function to plot the arrows and the basic alert() codes to create the alert function.
In this intermediate alert module, the focus will be to transform the static alert messages to be comprehensive and dynamic.
alert.freq_once_per_bar_close
to minimise repainting. Repainting is when the script behaviour differs when running the script in real-time compared to historical prices.
Read TradingView’s comprehensive address regarding repainting here (strong coffee recommended).2.1 Basic
In the basic module, we create static alerts using:
alert('LicenseID,buy,EURUSD,risk=1', alert.freq_once_per_bar_close)
The above code generates an alert message with a static symbol and risk value.
2.2 Multiple Parameters
alert('LicenseID,buy,EURUSD,risk=1,sl=10,tp=20', alert.freq_once_per_bar_close)
The alert message is still static, but with sl= and tp= parameters.
2.3 Dynamic Symbol
alert('LicenseID,buy,' +syminfo.ticker+ ',sl=10,tp=20,risk=1', alert.freq_once_per_bar_close)
Now, the symbol will change based on the ticker you create the alert on.
In the example above, we created the alert on the US100 chart. Should an alert trigger, the alert message will print:
symbol = syminfo.ticker
if syminfo.ticker == "US100"
symbol := "NAS100"
if LongEntryCondition
alert('LicenseID,buy,'+symbol+',sl=10,tp=20,risk=1', alert.freq_once_per_bar_close)
On the first line, we have a string named “symbol” set as TradingView’s ticker by default.
At the first “if”, “symbol” changes to NAS100 if the ticker is US100.
At the second “if”, we simply use “symbol” instead of “syminfo.ticker” for the alert function.
2.4 Dynamic Values
LongSL = low[1]
LongTP = ta.ema(close,50)
RiskValue = 1
if LongEntryCondition
alert('LicenseID,buy,' +syminfo.ticker+ ',sl=' +str.tostring(LongSL)+ ',tp=' +str.tostring(LongTP)+ ',risk='+str.tostring(RiskValue), alert.freq_once_per_bar_close)
Instead of hardcoding your sl=, tp= and risk=, you may use variables which may be dynamic such as low[1].
LongSL - Candle Data
The low[1] represents the low price of the the most recent candle while low[2] represent the low price 2 candles ago.
LongTP - Formula
LongTP is also dynamic which stores the latest Exponential Moving Average value of the 50 most recent close prices, changing at the close of each candle.
RiskValue - Static Value
You may also store static values, as seen for the “RiskValue” parameter where we stored a fixed value of 1.
Implementation
The alert() function only accept strings (texts) while “LongSL”, “LongTP” and “RiskValue” are floats (numbers). We will use the “str.tostring()” function to convert floats to string — to be compliant to the alert() function’s requirement.
2.5 Multiple License IDs
If you have multiple License IDs, we can send create alerts to all very easily.
if LongEntryCondition
alert('LicenseID1,buy,' +syminfo.ticker+ ',risk=1,sl=20', alert.freq_once_per_bar_close)
alert('LicenseID2,buy,' +syminfo.ticker+ ',risk=1,sl=30', alert.freq_once_per_bar_close)
alert('LicenseID3,buy,' +syminfo.ticker+ ',risk=1,sl=40', alert.freq_once_per_bar_close)
alert('LicenseID4,buy,' +syminfo.ticker+ ',risk=1,sl=40', alert.freq_once_per_bar_close)
alert('LicenseID5,buy,' +syminfo.ticker+ ',risk=1,sl=40', alert.freq_once_per_bar_close)
To implement, simply duplicate the alert code and change the LicenseID accordingly.
Even with 1 alert created, when entry conditions are met, 5 alerts will trigger at the same time.
2.6 Putting it all Together
//@version=5
indicator("EMA", overlay=true)
ema20 = ta.ema(close,20)
ema50 = ta.ema(close,50)
plot(ema20, color=color.new(color.blue, 5))
plot(ema50, color=color.new(color.red, 5))
long = ta.crossover(ema20, ema50) and close > ema20
short = ta.crossunder(ema20, ema50) and close < ema20
//plotting arrows to print entries on the chart
plotshape(long, style=shape.labelup, location=location.belowbar, color=color.new(#046ff9, 0), size=size.large, text='PineConnector \n Buy', textcolor=color.new(color.white, 0)) //plotting up arrow when buy/long conditions met
plotshape(short, style=shape.labeldown, location=location.abovebar, color=color.new(#046ff9, 0), size=size.large, text='PineConnector \n Sell', textcolor=color.new(color.white, 0)) //plotting down arrow when sell/short conditions met
//manipulating symbol to NAS100 if ticker is US100
symbol = syminfo.ticker
if syminfo.ticker == "US100"
symbol := "NAS100"
//variables to store dynamic values
LongSL = low[1]
LongTP = ta.ema(close,50)
RiskValue = 1
//trigger 3 alerts to the various License IDs with dynamic syntax
if long
alert('LicenseID1,buy,' +symbol+ ',sl=' +str.tostring(LongSL)+ ',tp=' +str.tostring(LongTP)+ ',risk='+str.tostring(RiskValue), alert.freq_once_per_bar_close)
alert('LicenseID2,buy,' +symbol+ ',sl=' +str.tostring(LongSL)+ ',tp=' +str.tostring(LongTP)+ ',risk='+str.tostring(RiskValue), alert.freq_once_per_bar_close)
alert('LicenseID3,buy,' +symbol+ ',sl=' +str.tostring(LongSL)+ ',tp=' +str.tostring(LongTP)+ ',risk='+str.tostring(RiskValue), alert.freq_once_per_bar_close)
← Previous
Next →
On this page
- Low-Code Alert
- 1. Basic
- 1.1 Determine your Script Type
- 1.2 Strategy
- 1.2.1 Add Visual Confirmation
- 1.2.2 Add the Alert Function
- 1.2.3 Create the Alert
- 1.2.3.1 Create an Alert
- 1.2.3.2 Configure Alert
- 1.2.3.3 Alert Firing
- 1.3 Indicator
- 1.3.1 Add Visual Confirmation
- 1.3.2 Add the Alert Function
- 1.3.3 Create the Alert
- 1.3.3.1 Create an Alert
- 1.3.3.2 Configure Alert
- 1.3.3.3 Alert Firing
- 2. Intermediate
- 2.1 Basic
- 2.2 Multiple Parameters
- 2.3 Dynamic Symbol
- 2.4 Dynamic Values
- 2.5 Multiple License IDs
- 2.6 Putting it all Together