gpt4 book ai didi

conditional-statements - 当条件从 true/false 改变时的警报条件之后。如果任一条件为真,当前会发出警报。不是在它改变的时候

转载 作者:行者123 更新时间:2023-12-02 00:18:27 26 4
gpt4 key购买 nike

这是 tradingview 的 pine 脚本的一部分。在“//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)

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

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

最佳答案

让我们看一个使用 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)

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

enter image description here

但是,这些信号应该是 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)

这将导致:

enter image description here

关于conditional-statements - 当条件从 true/false 改变时的警报条件之后。如果任一条件为真,当前会发出警报。不是在它改变的时候,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56284820/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com