'How can i create Alerts when my bar plot changes color?

I'm trying to set up alerts in function of the bar color changing.

What I want to do :

  1. condition 1 :When previous bar is red and current bar is green = buy alert
  2. condition 2 : When previous bar is green and current bar is red = sell alert

study("NewScript",overlay=false)

Length = input(30, minval=1)
Barcolor=input(true,title="Barcolor")

r1=iff(close[1]<open,max(open-close[1],high-low),high-low)

r2=iff(close[1]>open,max(close[1]-open,high-low),high-low)

bull=iff(close==open,iff(high-close==close-low,iff(close[1]>open,max(high-open,close-low),r1),iff(high-close>close-low,iff(close[1]<open, max(high-close[1],close-low), high-open),r1)),iff(close<open,iff(close[1]<open,max(high-close[1],close-low), max(high-open,close-low)),r1))


bear=iff(close==open,iff(high-close==close-low,iff(close[1]<open,max(open-low,high-close),r2),iff(high-close>close-low,r2,iff(close[1]>open,max(close[1]-low,high-close), open-low))),iff(close<open,r2,iff(close[1]>open,max(close[1]-low,high-close),max(open-low,high-close))))

colors=iff(sma(bull-bear,Length)>0, color.green, color.red)


plot(sma(bull-bear,Length),style=plot.style_columns,title="BBP",color=colors,linewidth=2)

plot(0,color=color.black,linewidth=1)

barcolor(Barcolor ? colors : na)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source