team dany
team dany

Reputation: 1

How to add alerts in pine script

I was trying to add alerts when the label comes in the script. Tried to do it, but was not able to make it out. I am not pro in coding but tried to make some change seeing other code. I added the alert condition for an other code but this was not working. Can the alerts be added to this.? Thank you.

//@version=4

study("BDW",overlay=true,max_lines_count=500,max_bars_back=500)
h = input(8.,'Bandwidth')
src = input(close,'Source')
//----
n = bar_index
var ln = array.new_line(0) 
if barstate.isfirst
    for i = 0 to 499
        array.push(ln,line.new(na,na,na,na))
//----
float y2 = na
float y1 = na
float y1_d = na
//----
line l = na
label lb = na
if barstate.islast
    for i = 0 to min(499,n-1)
        sum = 0.
        sumw = 0.
        for j = 0 to min(499,n-1)
            w = exp(-(pow(i-j,2)/(h*h*2)))
            sum += src[j]*w
            sumw += w
        y2 := sum/sumw
        d = y2 - y1

        l := array.get(ln,i)
        line.set_xy1(l,n-i+1,y1)
        line.set_xy2(l,n-i,y2)
        line.set_color(l,y2 > y1 ? #ff1100 : #39ff14)
        line.set_width(l,2)
        
        if d > 0 and y1_d < 0
            label.new(n-i+1,src[i],'▲',color=#00000000,style=label.style_circle,textcolor=#39ff14,textalign=text.align_center) 
        if d < 0 and y1_d > 0
            label.new(n-i+1,src[i],'▼',color=#00000000,style=label.style_circle,textcolor=#ff1100,textalign=text.align_center) 

        y1 := y2
        y1_d := d
        

crossOverAlert =if  d > 0 and y1_d < 0
alertcondition(condition=crossOverAlert,title="BUY", message="BUY")

crossUnderAlert = if d < 0 and y1_d > 0
alertcondition(condition=crossUnderAlert,title="SELL", message="SELL")

Upvotes: 0

Views: 1087

Answers (1)

Starr Lucky
Starr Lucky

Reputation: 1961

Use alert() instead of alertcondition() and put it right after when the label comes in the script.

  if d > 0 and y1_d < 0
            label.new(n-i+1,src[i],'▲',color=#00000000,style=label.style_circle,textcolor=#39ff14,textalign=text.align_center) 
            alert("BUY")
        if d < 0 and y1_d > 0
            label.new(n-i+1,src[i],'▼',color=#00000000,style=label.style_circle,textcolor=#ff1100,textalign=text.align_center) 
            alert("SELL")
    

Upvotes: 0

Related Questions