Pine script tradingview脚本中的if语句出现问题

Pine script tradingview脚本中的if语句出现问题,pine-script,Pine Script,我尝试在tradingview脚本中加入一个简化的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

我尝试在tradingview脚本中加入一个简化的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)
无论如何,这可能会失败

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


谢谢

您需要先启动VOLA_索引,然后再为其赋值


字符串VOLA_INDEX=“”

在为其赋值之前,需要启动VOLA_索引


字符串VOLA_INDEX=“”

无法按您想要的方式工作,因为
安全性
无法获取可变参数(即
VOLA_索引
字符串)。我通过
-运算符修复了您的代码,因此代码显示了如何实现您的想法。顺便说一下,我已经将代码翻译成了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)

这不会像您希望的那样工作,因为
security
不能接受可变参数(即
VOLA_索引
字符串)。我通过
-运算符修复了您的代码,因此代码显示了如何实现您的想法。顺便说一下,我已经将代码翻译成了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)

欢迎来到StackOverflow。请添加您得到的错误/输出以帮助人们回答您的问题,好吗?当您想重新分配值时,您应该使用
:=
运算符。使用:=运算符我得到了第8行:输入不匹配“vola”期望“行尾无行延续”@HeinrichBerger您能提供最小的可重复代码段吗?我们都不知道第8行的代码在哪里,vola是什么等等。我添加了完整的代码并删除了重复的行欢迎来到StackOverflow。请添加您得到的错误/输出以帮助人们回答您的问题,好吗?当您想重新分配值时,您应该使用
:=
运算符。使用:=运算符我得到了第8行:输入不匹配“vola”期望“行尾无行延续”@HeinrichBerger您能提供最小的可重复代码段吗?我们都不知道第8行的代码在哪里,vola是什么等等。我添加了完整的代码并删除了重复的代码。我已经尝试并更新了上面的代码。为此,我得到了返回:输入“VOLA_INDEX”时出现语法错误。我已经尝试并更新了上面的代码。为此,我得到了返回:输入'VOLA_INDEX'Hello时出现语法错误。非常感谢,我明白你的意思了。我会用你的na添加缺失的部分,你是对的,但我已经将代码剪切到最相关的部分。非常感谢!!!你好非常感谢,我明白你的意思了。我会用你的na添加缺失的部分,你是对的,但我已经将代码剪切到最相关的部分。非常感谢!!!