1

i want to add alert while buy / sell signal generated. Below is my pinescript.

//@version=5
indicator(title='TEST08032023', overlay=true)

s9ema = ta.ema(close, 9)
s18ema = ta.ema(close, 18)

longCond = ta.crossover(s9ema, s18ema)
shortCond = ta.crossunder(s9ema, s18ema)

plotshape(series=longCond, title='BUY', style=shape.labelup, location=location.belowbar, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), text='BUY', size=size.small)
plotshape(series=shortCond, title='SELL', style=shape.labeldown, location=location.abovebar, color=color.new(color.red, 0), textcolor=color.new(color.white, 0), text='SELL', size=size.small)
e2e4
  • 3,428
  • 6
  • 18
  • 30

3 Answers3

0
if longCond
    alert('BUY SIGNAL', alert.freq_once_per_bar_close)
if shortCond
    alert('SHORT SIGNAL', alert.freq_once_per_bar_close)
smashapalooza
  • 932
  • 2
  • 2
  • 12
0

Another way to do it -- below your code, put:

alertcondition(condition=longCond, title="Buy Signal", message="Buy Signal")
alertcondition(condition=shortCond, title="Sell Signal", message="Sell Signal")
Stefan_S
  • 3
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 28 '23 at 15:00
0

This has two solutions on PinescriptV5, using the alert or alertcondition function.

By using alertcondition, you can choose which alert you want to enable. For example, you can enable long alerts only rather than enable all of them. if using the alert function, you will make all alert functions when activated. This can benefit you if you don't want to have multiple alerts when using a script. More info, you can check images below.

This is the updated version of your code:

  1. Using alertcondition function

    indicator(title='TEST08032023', overlay=true)
    
    s9ema = ta.ema(close, 9)
    s18ema = ta.ema(close, 18)
    
    longCond = ta.crossover(s9ema, s18ema)
    shortCond = ta.crossunder(s9ema, s18ema)
    
    alertcondition(longCond, title = "Long Alert", message = "Long")
    alertcondition(shortCond, title = "Short Alert", message = "Short")
    
    plotshape(series=longCond, title='BUY', style=shape.labelup, location=location.belowbar, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), text='BUY', size=size.small)
    plotshape(series=shortCond, title='SELL', style=shape.labeldown, location=location.abovebar, color=color.new(color.red, 0), textcolor=color.new(color.white, 0), text='SELL', size=size.small)```
    
    
  2. Using alert function

    indicator(title='TEST08032023', overlay=true)
    
    s9ema = ta.ema(close, 9)
    s18ema = ta.ema(close, 18)
    
    longCond = ta.crossover(s9ema, s18ema)
    shortCond = ta.crossunder(s9ema, s18ema)
    
    if longCond
        alert("Long")
    if shortCond
        alert("Short")
    
    plotshape(series=longCond, title='BUY', style=shape.labelup, location=location.belowbar, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), text='BUY', size=size.small)
    plotshape(series=shortCond, title='SELL', style=shape.labeldown, location=location.abovebar, color=color.new(color.red, 0), textcolor=color.new(color.white, 0), text='SELL', size=size.small)```
    
    

After adding it to the code, don't forget to activate it as image below. Hope this helps.

  1. Click "Add alert ..." on the indicator's "more" option.
  2. Select "Any alert() function" if you using alert, "Long Alert" or "Short Alert" if you using alertcondition (options may differ depending on how you write alertcondition on your code).
  3. Click "Create" button.
  4. Check Alert panel if it already active.

enter image description here

enter image description here

enter image description here

enter image description here