【问题标题】:Issue with if statement in tradingview pine scripttradingview pine 脚本中的 if 语句问题
【发布时间】:2020-03-26 20:20:30
【问题描述】:

我尝试在我的 tradingview pine 脚本中加入一个简化的 switch 语句:

//@version=3
study("my_test",shorttitle="bands",overlay=true)

string VOLA_INDEX = ""

if (ticker == "USOIL")
   VOLA_INDEX := "OVX"
if (ticker == "GOLD")
   VOLA_INDEX := "GVZ"
if (ticker == "GER30")
   VOLA_INDEX := "DV1X"   


src = security(ticker,"D",close[1])
vola = security(VOLA_INDEX,"D",close[1])

bands1 = src * vola/100 * sqrt(0.00273972602) 
bands3 = src * vola/100 * sqrt(0.00821917808) 

upper1 = src + bands1
lower1 = src - bands1

plot( src, title="mean", color=black, style=linebr, linewidth=2, transp=100, trackprice = true,offset=-9999)
plot( upper1, title="upper", color=blue, style=linebr, linewidth=2, transp=40, trackprice = true,offset=-9999)
plot( lower1, title="lower", color=blue, style=linebr, linewidth=2, transp=40, trackprice = true,offset=-9999)

不知何故,这可能会失败。

有人知道语法有什么问题吗?

谢谢

【问题讨论】:

  • 欢迎来到 StackOverflow。请您添加错误/输出来帮助人们回答您的问题吗?
  • 当您想要重新分配值时,您应该使用:= 运算符。
  • 使用 := 运算符我得到第 8 行:不匹配的输入 'vola' 期望 '行尾没有续行'
  • @HeinrichBerger 你能提供最少的可重现代码sn-p吗?我们都不知道line 8 在您的代码中的位置,vola 是什么等等。
  • 我添加了完整的代码并删除了重复的行

标签: pine-script


【解决方案1】:

您需要在为其赋值之前启动 VOLA_INDEX。

字符串 VOLA_INDEX = ""

【讨论】:

  • 我已经尝试并更新了上面的代码。为此,我得到了回报:输入“VOLA_INDEX”处的语法错误
【解决方案2】:

这不会如您所愿,因为security 不能采用可变参数(即VOLA_INDEX 字符串)。我通过?-operator 修复了您的代码,因此代码显示了您如何实现您的想法。顺便说一句,我已将代码翻译成 pine v.4,我的建议是使用 v.4,因为不支持以前的版本,所以可能会出现一些问题。

//@version=4
study("my_test",shorttitle="bands",overlay=true)


vola = syminfo.ticker == "USOIL" ? security("OVX", "D", close[1]) :
  syminfo.ticker == "GOLD" ? security("GVZ", "D", close[1]) :
  syminfo.ticker == "GER30" ? security("DV1X", "D", close[1]) :
  na // I'm not sure about this. What should be here if none of the symbols matches

src = security(syminfo.ticker, "D", close[1])


bands1 = src * vola/100 * sqrt(0.00273972602) 
bands3 = src * vola/100 * sqrt(0.00821917808) 

upper1 = src + bands1
lower1 = src - bands1

plot(src, title="mean", color=color.black, style=plot.style_linebr, linewidth=2, transp=100, trackprice = true,offset=-9999)
plot(upper1, title="upper", color=color.blue, style=plot.style_linebr, linewidth=2, transp=40, trackprice = true,offset=-9999)
plot(lower1, title="lower", color=color.blue, style=plot.style_linebr, linewidth=2, transp=40, trackprice = true,offset=-9999)

【讨论】:

  • 你好。非常感谢,我明白你的意思。我将在您的 na 中添加您正确的缺失部分,但我已将代码剪切到最相关的部分。非常感谢!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-15
  • 1970-01-01
  • 2020-02-10
  • 1970-01-01
相关资源
最近更新 更多