【问题标题】:TradingView v1 to v4 iff statements Pine ScriptTradingView v1 到 v4 iff 语句 Pine 脚本
【发布时间】:2021-03-02 17:32:59
【问题描述】:

我正在尝试将 TradingView 的股票 ATR 停止脚本(内置于 Pine Script v1 中)集成到我自己的 v4 代码中,这导致错误“输入语法错误'行尾没有行继续'的 iff以下陈述。

有人可以帮我将这些行翻译成 v4 兼容的代码吗? ????????????????????????

注意:正如帖子标题所暗示的,此代码有效,但仅在 Pine Script v1 中,我正在尝试将其调整为 v4,我认为它需要 if else 语句而不是 iff 语句。

//@version=1
study(title="ATR Stop", overlay = true)

nATRPeriod = input(5)
nATRMultip = input(2.5)

xATR = atr(nATRPeriod)
nLoss = nATRMultip * xATR

xATRTrailingStop = iff(close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), close - nLoss),
                    iff(close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), close + nLoss), 
                        iff(close > nz(xATRTrailingStop[1], 0), close - nLoss, close + nLoss)))

pos =   iff(close[1] < nz(xATRTrailingStop[1], 0) and close > nz(xATRTrailingStop[1], 0), 1,
            iff(close[1] > nz(xATRTrailingStop[1], 0) and close < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0))) 

color = pos == -1 ? red: pos == 1 ? green : blue 

plot(xATRTrailingStop, color=color, title="ATR Trailing Stop")

【问题讨论】:

  • Pine 代码以 4 个空格为增量缩进。如果代码在同一个缩进“级别”,它被认为是一个单独的命令。如果缩进一个空格,它被认为是上一行的一部分。你应该在最后一行缩进你的iff,再加一个空格,就像你在第二行缩进iff一样。
  • 谢谢我试了一下,但我仍然在上面代码的第一行收到“输入语法错误'没有行继续'的行尾”:(
  • 请更新您的问题以包含完整的脚本,而不仅仅是此代码 sn-p。这将有助于调查问题。
  • 已更新以包含完整的脚本????????

标签: pine-script


【解决方案1】:

下面的 v4 代码。缩进很重要。

//@version=4
study(title="ATR Stop", overlay = true)

nATRPeriod = input(5)
nATRMultip = input(2.5)

var float   xATRTrailingStop    = na
var int     pos                 = na

xATR                = atr(nATRPeriod)
nLoss               = nATRMultip * xATR

xATRTrailingStop    := iff(close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0), 
                             max(nz(xATRTrailingStop[1]), close - nLoss), 
                             iff(close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0), 
                                 min(nz(xATRTrailingStop[1]), close + nLoss), 
                                 iff(close > nz(xATRTrailingStop[1], 0), 
                                     close - nLoss, 
                                     close + nLoss)))

pos                 := iff(close[1] < nz(xATRTrailingStop[1], 0) and close > nz(xATRTrailingStop[1], 0), 
                         1, 
                         iff(close[1] > nz(xATRTrailingStop[1], 0) and close < nz(xATRTrailingStop[1], 0), 
                          -1, 
                          nz(pos[1], 0))) 

atrColor            = pos == -1 ? color.red : pos == 1 ? color.green : color.blue 

plot(xATRTrailingStop, "ATR Trailing Stop", atrColor)

【讨论】:

  • 像魅力一样工作 非常感谢 永远不会单独解决这个问题! ????
【解决方案2】:

试试这个,我认为你的问题是你放在最后的逗号。 IFF 可以有 2 个可能的结果,您将它们放在第一行本身。那为什么要加逗号呢?

也要使用适当的缩进

xATRTrailingStop = iff(close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), close - nLoss)
                   iff(close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), close + nLoss) 
                   iff(close > nz(xATRTrailingStop[1], 0), close - nLoss, close + nLoss)))
    
    pos =   iff(close[1] < nz(xATRTrailingStop[1], 0) and close > nz(xATRTrailingStop[1], 0), 1,
            iff(close[1] > nz(xATRTrailingStop[1], 0) and close < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0)))

PS:如果这不起作用,请尝试再次编写代码,如果可能使用if else,您将能够正确看到嵌套。别忘了缩进

【讨论】:

  • 以上代码是TradingView自己的代码。它有效,但仅在版本 1 Pine Script 中。我想将其更新为 v4 兼容性。我相信这可能需要 if else 但我不知道该怎么做。你愿意帮忙吗?谢谢??
猜你喜欢
  • 2020-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-10
  • 1970-01-01
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
相关资源
最近更新 更多