Tony
Tony

Reputation: 11

Prevent duplicate alerts in TradingView in the same direction

new to pinescript and have been trying all day to figure this out - I have a script that is combining 2 indicators and creating an alert when both indicator criterias are met - which is working great, exactly how I want in terms of alert triggers - However, im facing an issue trying to prevent consecutive alerts from firing in the same direction - ie if previous alert was short, next alert can only be long, cannot have 2 longs or shorts back to back. tried all sorts with cahtgpt help and no joy yet - Using version 5

Many thanks in advance for any help solving this

buy = cross_up and ta.crossover(sig, 0)

sell = cross_down and ta.crossunder(sig, 0)

alertcondition(buy, 'Long Entry', 'Close MANAUSDT Buy MANAUSDT Q=390%')

alertcondition(sell, 'Short Entry', 'Close MANAUSDT Sell MANAUSDT Q=390%')

//Add Entry Markers

plotshape(buy, title='Buy' , location=location.belowbar , color=#05F90F, size=size.small , style=shape.triangleup , text='Long', textcolor = #05F90F, offset = 1)

plotshape(sell, title='Sell', location=location.abovebar , color=#F90719 , size=size.small , style=shape.triangledown , text='Short', textcolor = #F90719, offset = 1)

create various alert listeners to set previous alert state, to prevent next state repeating current state, no joy, still a noob at this :(

Upvotes: 1

Views: 567

Answers (1)

Math Rave
Math Rave

Reputation: 11

You can create a while loop that checks if the condition of the alert is already satisfied. I did the same and it works.

Upvotes: 1

Related Questions