Prevent alert triggering on consecutive signal? (Pine Script)

Viewed 22

I setup these two alerts:

alertcondition(bar_close > bar_open, 'BUY/LONG', 'BUY_ALERT')
alertcondition(bar_close < bar_open, 'SELL/SHORT', 'SELL_ALERT')

They work as a trigger on confirmed closed bars, but they trigger on each consecutive bar that's the same color. I wanted to change the code so they don't trigger if the previous color was the same as the current.

I found this answer: Alert Condition/Consecutive Signal pinescript

I copied what was shown in that answer and applied it to my script, so now my alerts look like this:

alertcondition(bar_close > bar_open and not bar_close > bar_open[1], 'BUY/LONG', 'BUY_ALERT')
alertcondition(bar_close < bar_open and not bar_close < bar_open[1], 'SELL/SHORT', 'SELL_ALERT')

The behavior is the same, with the alert still triggering on each consecutive bar that's the same color as the last.

I wondered if perhaps it's because it works on a confirming closed candle, so I changed those [1] to [2] but that doesn't work, the alert still triggers on each consecutive color:

bar_close > bar_open and not bar_close > bar_open[2]
bar_close < bar_open and not bar_close < bar_open[2]

I then tried adding those [1] to the end of each like this, but this also keeps the same behavior:

bar_close > bar_open and not bar_close[1] > bar_open[1]
bar_close < bar_open and not bar_close[1] < bar_open[1]

That only leaves one other thing to try, but again the behavior is the same with [2] there:

bar_close > bar_open and not bar_close[2] > bar_open[2]
bar_close < bar_open and not bar_close[2] < bar_open[2]

After trying all of the above I just couldn't get it to work.

Thanks in advance to anyone that might be able to shed light on this.

0 Answers
Related