当条件从真/假变为警报条件后。当前在任一条件为真时发出警报。不是当它改变

Tre*_*ond 2 alert conditional-statements pine-script

这是用于交易视图的松树脚本的一部分。在“//Condition”之后的脚本中,我希望仅在条件从多头变为空头或由空头变为多头时生成警报。不是每根蜡烛都像现在这样结束,因为一个条件始终为真。这已更改为一项研究。

threshold = input(title="Threshold", type=float, defval=0.0014, step=0.0001)

buying  = l3_0 > threshold ? true : l3_0 < -threshold ? false : buying[1]
///// T edit
selling = l3_0 > -threshold ? true : l3_0 < threshold ? false : 
selling[1] //// T edit END

hline(0, title="base line")
bgcolor(l3_0 > 0.0014 ? green : l3_0 < -0.0014 ? red : gray, transp=20)
bgcolor(buying ? green : red, transp=20)
plot(l3_0, color=silver, style=area, transp=75)
plot(l3_0, color=aqua, title="prediction")

/////     Stragegy     
/////////////////////////////////////////////////////
//longCondition = buying
//if (longCondition)
    //strategy.entry("Long", strategy.long)

//shortCondition = buying != true
//if (shortCondition)
    //strategy.entry("Short", strategy.short)
Run Code Online (Sandbox Code Playgroud)

///// 警报//////////////////////////////////////////// ///////////警报条件(条件,标题,消息)

//Condition
long  = l3_0 > 0.0014
short = l3_0 < -0.0014


alertcondition(long, title = "ANN Long", message= "ANN Long")
alertcondition(short, title = "ANN Short", message= "ANN Short")
Run Code Online (Sandbox Code Playgroud)

Bar*_*kut 5

让我们看一个使用 MACD 的小例子。我们想要去delta>= 0,只要delta<0。此外,我们希望保持在我们的位置,除非触发相反的信号(输入一次并等待相反的信号)。

您的代码如下所示:

//@version=3
study("My Script", overlay=true)

// Get the inputs
MACDLengthMACD = input(title="MACD Length", defval=9, minval=1, maxval=100)
fastLengthMACD = input(title="MACD Fast Length", defval=12, minval=1, maxval=100)
slowlengthMACD = input(title="MACD Slow Length", defval=26, minval=1, maxval=100)

// Standard MACD calculations
MACD = ema(close, fastLengthMACD) - ema(close, slowlengthMACD)
aMACD = ema(MACD, MACDLengthMACD)
deltaMACD = MACD - aMACD

buySignal = (deltaMACD >= 0)
sellSignal= (deltaMACD < 0)

plotshape(series=buySignal, text="BUY", style=shape.triangleup, location=location.belowbar, color=green, size=size.small)
plotshape(series=sellSignal, text="SELL", style=shape.triangledown, location=location.abovebar, color=red, size=size.small)
Run Code Online (Sandbox Code Playgroud)

在这种情况下,你会得到多个买入或卖出信号,因为buySignalsellSignal将是true只要他们的条件true

在此处输入图片说明

然而,这些信号应该只true针对一根柱线,以便仅触发一个买入或卖出信号。为了实现这个目标,你可以使用另一个变量(isLongisShort在下面的代码)和使用历史参考操作[],以确定是否是以前长或短。

然后,仅在您尚未做多时触发买入信号,仅在尚未做空时触发卖出信号。这样你只会得到一个买入或卖出信号。

//@version=3
study("My Script", overlay=true)

// Get the inputs
MACDLengthMACD = input(title="MACD Length", defval=9, minval=1, maxval=100)
fastLengthMACD = input(title="MACD Fast Length", defval=12, minval=1, maxval=100)
slowlengthMACD = input(title="MACD Slow Length", defval=26, minval=1, maxval=100)

// Standard MACD calculations
MACD = ema(close, fastLengthMACD) - ema(close, slowlengthMACD)
aMACD = ema(MACD, MACDLengthMACD)
deltaMACD = MACD - aMACD

// Deternine if we are currently LONG
isLong = false
isLong := nz(isLong[1], false)

// Determine if we are currently SHORT
isShort = false
isShort := nz(isShort[1], false)

// Buy only if the buy signal is triggered and we are not already long
buySignal = not isLong and (deltaMACD >= 0)

// Sell only if the sell signal is triggered and we are not already short
sellSignal= not isShort and (deltaMACD < 0)

if (buySignal)
    isLong := true
    isShort := false

if (sellSignal)
    isLong := false
    isShort := true

plotshape(series=buySignal, text="BUY", style=shape.triangleup, location=location.belowbar, color=green, size=size.small)
plotshape(series=sellSignal, text="SELL", style=shape.triangledown, location=location.abovebar, color=red, size=size.small)
Run Code Online (Sandbox Code Playgroud)

这将导致:

在此处输入图片说明