lenso_here
lenso_here

Reputation: 110

Alerts on Color changes

I'm trying to just get alert when color change of the line. This code alerts at each bar which I don't want.

study("Tillson T3", overlay=true)
length1 = input(8, "T3 Length")
a1 = input(0.7, "Volume Factor")

e1=ema((high + low + 2*close)/4, length1)
e2=ema(e1,length1)
e3=ema(e2,length1)
e4=ema(e3,length1)
e5=ema(e4,length1)
e6=ema(e5,length1)
c1=-a1*a1*a1
c2=3*a1*a1+3*a1*a1*a1
c3=-6*a1*a1-3*a1-3*a1*a1*a1
c4=1+3*a1+a1*a1*a1+3*a1*a1
T3=c1*e6+c2*e5+c3*e4+c4*e3

col1= T3>T3[1]
col3= T3<T3[1]
color = col1 ? green : col3 ? red : yellow
plot(T3, color=color, linewidth=3, title="T3")

alertcondition(col1, title='Alert on Green Bar', message='Green Bar!')
alertcondition(col3, title='Alert on Red Bar', message='Red Bar!')

Upvotes: 0

Views: 1924

Answers (2)

Hikmet Sezen
Hikmet Sezen

Reputation: 1

buy= T3>T3[1] and T3[1]<T3[2] 
sell= T3<T3[1] and T3[1]>T3[2]  
color = T3>T3[1] ? green : T3<T3[1] ? red : yellow
plot(T3, color=color, linewidth=3, title="T3")
alertcondition(buy, title='Alert on Green Bar', message='Green Bar!')
alertcondition(sell, title='Alert on Red Bar', message='Red Bar!')

Upvotes: 0

vitruvius
vitruvius

Reputation: 21121

Well, as long as condition argument of the alertcondition() is true, you will get an alert.

If you plot col1 and col3, you will see why you are getting multiple alerts. It is because one of them stays true for multiple bars. What you need is a pulse.

enter image description here

To create a pulse, you need to think about your implementation. Your implementation guarantees that col1 and col3 can never be true at the same time. So, you can compare col3[1] and col1. So that, if col3[1] and col1 is true, it means that one bar ago col3 was true, but for the current bar only col1 is true, which indicates a change from col3 to col1.

Have a look at the following code and chart:

//@version=3
study(title="Color", overlay=false)
T3 = close
col1= T3>T3[1]
col3= T3<T3[1]
isNewCol1 = nz(col3[1]) and col1
isNewCol3 = nz(col1[1]) and col3

plot(series=isNewCol1 ? 1 : 0, title="isNewCol1", color=orange, linewidth=4)
plot(series=isNewCol3 ? 1 : 0, title="isNewCol3", color=blue, linewidth=4)

enter image description here

Edit:

You just need to use those variables in alertcondition().

study("Tillson T3", overlay=true)
length1 = input(8, "T3 Length")
a1 = input(0.7, "Volume Factor")

e1=ema((high + low + 2*close)/4, length1)
e2=ema(e1,length1)
e3=ema(e2,length1)
e4=ema(e3,length1)
e5=ema(e4,length1)
e6=ema(e5,length1)
c1=-a1*a1*a1
c2=3*a1*a1+3*a1*a1*a1
c3=-6*a1*a1-3*a1-3*a1*a1*a1
c4=1+3*a1+a1*a1*a1+3*a1*a1
T3=c1*e6+c2*e5+c3*e4+c4*e3

col1= T3>T3[1]
col3= T3<T3[1]
isNewCol1 = nz(col3[1]) and col1
isNewCol3 = nz(col1[1]) and col3
colorP = col1 ? green : col3 ? red : yellow
plot(T3, color=colorP, linewidth=3, title="T3")

plotshape(series=isNewCol1, title="col1", style=shape.triangleup, location=location.belowbar, color=green, text="Green", size=size.normal)
plotshape(series=isNewCol3, title="col3", style=shape.triangledown, location=location.abovebar, color=red, text="Red", size=size.normal)
alertcondition(condition=isNewCol1, title="isNewCol1", message="green")
alertcondition(condition=isNewCol3, title="isNewCol3", message="red")

enter image description here

Upvotes: 1

Related Questions