1
votes

i try to put in a simplified switch-statement in my tradingview pine script:

//@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)

somehow, this could fails.

Someone an idea whats wrong with the syntax?

Thank you

2
Welcome to StackOverflow. Please could you add the error / output you are getting to help people answer your question? - James Wilson
You should use the := operator when you want to reassign values. - Baris Yakut
using the := operator i got line 8: mismatched input 'vola' expecting 'end of line without line continuation' - Heinrich Berger
@HeinrichBerger could you provide minimal reproducible code snippet? None of us knows where line 8 in your code, what vola is etc. - Michel_T.
i added the full code and removed the duplicated line - Heinrich Berger

2 Answers

0
votes

You need to initiate VOLA_INDEX before assigning a value to it.

string VOLA_INDEX = ""

0
votes

That won't work as you want, because security can't take mutable param (which is the VOLA_INDEX string). I fixed your code via ?-operator so the code shows how you can implement your idea. By the way, I've translated the code to pine v.4 and my advice here is to use v.4, because previous versions aren't supported so there could be some problems.

//@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)