无法发出遵循 strategy.entry 个命令的警报

Trouble making alerts that follow strategy.entry orders

我在交易视图中有一个有效的策略,我想添加警报。

我只需要 strategy.entry(长)的 'buy' 警报和 strategy.entry(短)的 'sell' 警报。

条件不能相同。多头的条件之一是有 2 个连续的绿色条。因此,一旦长警报触发,并且下一根柱线也为绿色,长警报将在第三根柱线上再次触发。每当有 2 个连续的绿色条时,它就会触发。我需要的是它触发一次(做多)然后必须等待卖出警报才能再次触发。

这是问题的示例代码,感谢您的帮助。

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)

在做多之前,你只需要弄清楚你是否已经做多。因此,您可以为此使用变量并在触发买入或卖出信号之前检查其值。

幸运的是,当我开始使用 pinescript 时,我使用的是完全相同的脚本 :) 它是 v3 但它确实有效。

//@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)