'Pinescript duplicate alerts

I have created a very basic script in pinescript.

study(title='Renko Strat w/ Alerts', shorttitle='S_EURUSD_5_[MakisMooz]', overlay=true)

rc = close

buy_entry = rc[0] > rc[2]
sell_entry = rc[0] < rc[2]

alertcondition(buy_entry, title='BUY')
alertcondition(sell_entry, title='SELL')
plot(buy_entry/10)

The problem is that I get a lot of duplicate alerts. I want to edit this script so that I only get a 'Buy' alert when the previous alert was a 'Sell' alert and visa versa. It seems like such a simple problem, but I have a hard time finding good sources to learn pinescript. So, any help would be appreciated. :)



Solution 1:[1]

One way to solve duplicate alters within the candle is by using "Once Per Bar Close" alert. But for alternative alerts (Buy - Sell) you have to code it with different logic.

I Suggest to use Version 3 (version shown above the study line) than version 1 and 2 and you can accomplish the result by using this logic:

buy_entry  = 0.0
sell_entry = 0.0

buy_entry  := rc[0] > rc[2] and sell_entry[1] == 0? 2.0 : sell_entry[1] > 0 ? 0.0 : buy_entry[1]
sell_entry := rc[0] < rc[2] and buy_entry[1] == 0 ? 2.0 : buy_entry[1] > 0  ? 0.0 : sell_entry[1]

alertcondition(crossover(buy_entry ,1) , title='BUY' )
alertcondition(crossover(sell_entry ,1), title='SELL')

Solution 2:[2]

You'll have to do it this way

if("Your buy condition here")
    strategy.entry("Buy Alert",true,1)

if("Your sell condition here")
    strategy.entry("Sell Alert",false,1)

This is a very basic form of it but it works. You were getting duplicate alerts because the conditions were fulfulling more often. But with strategy.entry(), this won't happen

When the sell is triggered, as per paper trading, the quantity sold will be double (one to cut the long position and one to create a short position)

PS :You will have to add code to create alerts and enter this not in study() but strategy()

Solution 3:[3]

The simplest solution to this problem is to use the built-in crossover and crossunder functions.

They consider the entire series of in-this-case close values, only returning true the moment they cross rather than every single time a close is lower than the close two candles ago.

//@version=5
indicator(title='Renko Strat w/ Alerts', shorttitle='S_EURUSD_5_[MakisMooz]', overlay=true)

c = close
bool buy_entry = false
bool sell_entry = false

if ta.crossover(c[1], c[3])
    buy_entry := true
    alert('BUY')

if ta.crossunder(c[1], c[3])
    sell_entry := true
    alert('SELL')

plotchar(buy_entry, title='BUY', char='B', location=location.belowbar, color=color.green, offset=-1)
plotchar(sell_entry, title='SELL', char='S', location=location.abovebar, color=color.red, offset=-1)

It's important to note why I have changed to the indices to 1 and 3 with an offset of -1 in the plotchar function. This will give the exact same signals as 0 and 2 with no offset.

The difference is that you will only see the character print on the chart when the candle actually closes rather than watch it flicker on and off the chart as the close price of the incomplete candle moves.

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Suraj Neupane
Solution 2
Solution 3 DogeCode