1
votes

I have a working strategy in trading view and I would like to add alerts to.

I simply need a 'buy' alert on strategy.entry(long) and a 'sell' alert on strategy.entry(short).

The conditions can't be the same. One of the conditions for long is that there are 2 consecutive green bars. So once the long alert triggers, and the next bar is also green, the long alert will trigger again on the third bar. It triggers every time there are 2 consecutive green bars. What I need is so that it triggers once (go long) and then has to wait for a sell alert before it is allowed to trigger again.

Here is example code on the problem, thanks for any help.

study("Consecutive Up/Down Strategy", overlay=true)

consecutiveBarsUp = input(1)
consecutiveBarsDown = input(1)

price = close

ups = 0.0
ups := price > price[1] ? nz(ups[1]) + 1 : 0

dns = 0.0
dns := price < price[1] ? nz(dns[1]) + 1 : 0
// Strategy Execution, this WORKS because once you go long, you can't go long again until you have gone short. 

// if (ups >= consecutiveBarsUp)
//     strategy.entry("ConsUpLE", strategy.long, comment="ConsUpLE")

// if (dns >= consecutiveBarsDown)
//     strategy.entry("ConsDnSE", strategy.short, comment="ConsUpLE")

// Alert conditions, this doesn't work because it triggers the alert multiple times in a buy/sell cycle. After it goes long for the first time, the condition can be met before it can go short. Ex. 2 Consecuative green bars triggers long alert, but then the third consecuative green bar also triggers the long alert, before it closes the position.

alertcondition(ups >= consecutiveBarsUp, title='long', message='long')
alertcondition(dns >= consecutiveBarsDown, title='short', message='short')

//plot(strategy.equity, title="equity", color=color.red, linewidth=2, style=plot.style_areabr)
1

1 Answers

2
votes

You just need to figure out if you are already long, before going long. SO, you can use a variable for that and check its value before you trigger a buy or sell signal.

Luckily for you, I worked on the very same script when I started working with pinescript :) It's v3 but it does the trick.

//@version=3
study("Consecutive Up/Down with Alerts", overlay=true)

consecutiveBarsUp = input(3)
consecutiveBarsDown = input(3)

price = close
isLong = false
isLong := nz(isLong[1], false)

ups = 0.0
ups := price > price[1] ? nz(ups[1]) + 1 : 0

dns = 0.0
dns := price < price[1] ? nz(dns[1]) + 1 : 0

buySignal = (ups >= consecutiveBarsUp) and not nz(isLong[1])
sellSignal = (dns >= consecutiveBarsDown) and nz(isLong[1])

if (buySignal)
    isLong := true

if (sellSignal)
    isLong := false

alertcondition(condition=buySignal, title="BUY", message="BUY Signal.")
alertcondition(condition=sellSignal, title="SELL", message="SELL Signal.")

plotshape(buySignal, style=shape.triangleup, color=green, transp=40, text="BUY", editable=false, location=location.belowbar, size=size.small)
plotshape(sellSignal, style=shape.triangledown, color=red, transp=40, text="SELL", editable=false, location=location.abovebar, size=size.small)