Charts Tradingview脚本警报未按预期工作

Charts Tradingview脚本警报未按预期工作,charts,pine-script,trading,technical-indicator,Charts,Pine Script,Trading,Technical Indicator,我正在使用下面的脚本,但没有收到预期的警报。我知道这是一个重新绘制的脚本。它与背面测试配合得很好。 长警报设置为每栏关闭一次,短警报设置为每栏关闭一次 意外的行为是 1有几次,我收到短警报,虽然没有相应的长警报。尽管我在脚本中已注意到,短警报将仅在有长警报时发送。 每个条有2个连续的短警报。我知道在实时栏中,短条件可能多次为真。但由于我已将警报设置为每小节一次,因此警报应仅在短条件第一次变为真时出现 如果我做错了什么,你能告诉我吗 提前谢谢 //@version=4 study("My Scri

我正在使用下面的脚本,但没有收到预期的警报。我知道这是一个重新绘制的脚本。它与背面测试配合得很好。 长警报设置为每栏关闭一次,短警报设置为每栏关闭一次

意外的行为是 1有几次,我收到短警报,虽然没有相应的长警报。尽管我在脚本中已注意到,短警报将仅在有长警报时发送。 每个条有2个连续的短警报。我知道在实时栏中,短条件可能多次为真。但由于我已将警报设置为每小节一次,因此警报应仅在短条件第一次变为真时出现

如果我做错了什么,你能告诉我吗

提前谢谢

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

ST = input(true, title = "Activate Strategy Tester")
T_SY = input(2020, title = "Strategy Start Year")
T_SM = input(5, title = "Start Month")
T_SD = input(1, title = "Strategy Start Day")
T_EY = input(2025, title = "Strategy End Year")
T_EM = input(1, title = "End Month")
T_ED = input(1, title = "Strategy End Day")
T_S = timestamp(T_SY, T_SM, T_SD,00,00)
T_E = timestamp(T_EY, T_EM, T_ED,00,00)
T= ST and time >= T_S and time <= T_E 

firstrun = true
bought = false
longcondition = false
shortcondition = false

//Just to track first run

firstrun := firstrun[1]


if (firstrun == false)
    bought := bought[1]

//once condition is met, send a buy alert and make "bought" equal to true  //to enable selling

if (close <= 8600 and bought==false and T)
    bought := true
    longcondition :=true

alertcondition(longcondition,  "Long",  "Long")  

plotshape(longcondition,  title = "Buy",  text = 'Buy',  style = shape.labelup,   location = location.abovebar, color= color.green, textcolor = color.white, transp = 0, size = size.tiny)


if (longcondition)
    longcondition :=false

//once condition is met, sent a sell alert.

if (bought and close>=9000 and T)  
    shortcondition := true
    bought := false

alertcondition(shortcondition,  "short",  "short") 
plotshape(shortcondition,  title = "Sell",  text = 'Sell',  style = shape.labelup,   location = location.belowbar, color= color.red, textcolor = color.white, transp = 0, size = size.tiny)


if (shortcondition)
    shortcondition :=false

plotchar(bought, "bought", "", location = location.top)   

firstrun := false

您购买的变量现在会自动将其值保存到一个条到一个条,我已经删除了脚本中变量的后期重新初始化。在这里似乎工作得很好:

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

ST = input(true, title = "Activate Strategy Tester")
T_SY = input(2000, title = "Strategy Start Year")
T_SM = input(5, title = "Start Month")
T_SD = input(1, title = "Strategy Start Day")
T_EY = input(2025, title = "Strategy End Year")
T_EM = input(1, title = "End Month")
T_ED = input(1, title = "Strategy End Day")
T_S = timestamp(T_SY, T_SM, T_SD,00,00)
T_E = timestamp(T_EY, T_EM, T_ED,00,00)
T= ST and time >= T_S and time <= T_E 

var bought = false
longcondition = false
shortcondition = false

//once condition is met, send a buy alert and make "bought" equal to true  //to enable selling
if (close <= 8600 and bought==false and T)
    bought := true
    longcondition :=true

//once condition is met, sent a sell alert.
if (bought and close>=9000 and T)  
    shortcondition := true
    bought := false

alertcondition(longcondition,  "Long",  "Long")  
alertcondition(shortcondition,  "short",  "short") 
plotshape(longcondition,  title = "Buy",  text = 'Buy',  style = shape.labelup,   location = location.abovebar, color= color.green, textcolor = color.white, transp = 0, size = size.tiny)
plotshape(shortcondition,  title = "Sell",  text = 'Sell',  style = shape.labelup,   location = location.belowbar, color= color.red, textcolor = color.white, transp = 0, size = size.tiny)
// For debugging.
plotchar(bought, "bought", "•", location = location.top)

非常感谢。让我检查一下