2
votes

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. :)

2

2 Answers

3
votes

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')
0
votes

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()