Pine 脚本变量作用域

Pine Script variable scope

我正在使用 inside bars 编写一个简单的策略。我遇到的问题是,如果入场蜡烛后任何柱的高点超过内柱高点,我想取消交易。

下面的代码片段应该能更好地解释它

var short_Stop_Loss_Level = 0
if (high[0] > short_Stop_Loss_Level)
    label.new(bar_index, high, style=label.style_none, text="C=" + tostring(short_Stop_Loss_Level), yloc=yloc.abovebar)
    strategy.cancel_all()


Short_Condition = t and Inside_Bar
if Short_Condition
    short_Stop_Loss_Level = high[0] + 0.03

    label.new(bar_index, low, style=label.style_none,
          text="s=" + tostring(short_Stop_Loss_Level), yloc=yloc.belowbar)

    strategy.cancel_all()
    // strategy.close_all()
    strategy.entry("Enter", strategy.short, stop=Short_Stop_Buy_Level, qty=100)

enter image description here

从图片中您可以看到停止值没有在 if 范围之外维护,即使我已经声明了一个全局停止变量。我对此很陌生,也许我犯了一个我无法发现的简单错误

pine-script中,variable declaration是用=运算符完成的。就像你喜欢 var short_Stop_Loss_Level = 0.

但是,variable assignment 是通过 := 运算符完成的。因此,每当您想为 已定义的 变量赋予新值时,您应该使用 := 运算符。

if Short_Condition
    short_Stop_Loss_Level := high[0] + 0.03

您应该仔细检查您的代码并确保在需要时使用 := 运算符。