Search code examples
switch-statementpine-scriptpine-script-v5

Issue with switch statement in Pine Script version 5


I am not very experienced with coding (my first time trying the switch function) and can't figure out what is wrong with this piece of code. The error is "Syntax error at input 'US02Y'. Also, the last parenthesis of the switch statement is underlined in red to show that is where the issue is (the parenthesis just before the 1st conditional statement with "US02Y"). Each statement under the switch function starts with 1 space to allow line wrapping. Any help is much appreciated and let me know if you need more to help, here is the code:

// Get the current ticker<br>
currentTicker = syminfo.ticker

// Get the related ivol ticker<br>
string ivolString = switch ticker.standard(currentTicker)<br>
 "US02Y" => "CBOE:VXTLT"<br>
 "US10Y" => "CBOE:VXTLT"<br>
 "US30Y" => "CBOE:VXTLT"<br>
 "TLT" => "CBOE:VXTLT"<br>
 "SPX500USD" => "TVC:VIX"<br>
 "SPX" => "TVC:VIX"<br>
 "SPY" => "TVC:VIX"<br>
 "QQQ" => "NASDAQ:VOLQ"<br>
 "NDQ" => "NASDAQ:VOLQ"<br>
 "US100" => "NASDAQ:VOLQ"<br>
 "DJI" => "CBOE:VXD"<br>
 "DIA" => "CBOE:VXD"<br>
 "IWM" => "CBOE:RVX"<br>
 "RUT" => "CBOE:RVX"<br>
 "US2000" => "CBOE:RVX"<br>
 "EEM" => "CBOE:VXEEM"<br>
 "EFA" => "CBOE:VXEFA"<br>
 "EWZ" => "CBOE:VXEWZ"<br>
 "GOLD" => "CBOE:GVZ"<br>
 "GLD" => "CBOE:GVZ"<br>
 "FXI" => "CBOE:VXFXI"<br>
 "USOIL" => "CBOE:OVX"<br>
 "WTI" => "CBOE:OVX"<br>
 "FXE" => "CBOE:EVZ"<br>
 "EURUSD" => "CBOE:EVZ"<br>
 "IBM" => "CBOE:VXIBM"<br>
 "GOOGL" => "CBOE:VXGOG"<br>
 "AMZN" => "CBOE:VXAZN"<br>
 "AAPL" => "CBOE:VXAPL"<br>
 => na<br>

ivolSymbol = request.security(ivolString, "", close)<br>
ivolSymbol_availabilityCheck = str.contains(ivolString, "V") ? true : false

Solution

  • Each line in your switch block must be indented by 4 spaces or 1 tab.

    // Get the related ivol ticker
    string ivolString = switch ticker.standard(currentTicker)
        "US02Y" => "CBOE:VXTLT"
        "US10Y" => "CBOE:VXTLT"
        "US30Y" => "CBOE:VXTLT"
        "TLT" => "CBOE:VXTLT"
        "SPX500USD" => "TVC:VIX"
        "SPX" => "TVC:VIX"
        "SPY" => "TVC:VIX"
        "QQQ" => "NASDAQ:VOLQ"
        "NDQ" => "NASDAQ:VOLQ"
        "US100" => "NASDAQ:VOLQ"
        "DJI" => "CBOE:VXD"
        "DIA" => "CBOE:VXD"
        "IWM" => "CBOE:RVX"
        "RUT" => "CBOE:RVX"
        "US2000" => "CBOE:RVX"
        "EEM" => "CBOE:VXEEM"
        "EFA" => "CBOE:VXEFA"
        "EWZ" => "CBOE:VXEWZ"
        "GOLD" => "CBOE:GVZ"
        "GLD" => "CBOE:GVZ"
        "FXI" => "CBOE:VXFXI"
        "USOIL" => "CBOE:OVX"
        "WTI" => "CBOE:OVX"
        "FXE" => "CBOE:EVZ"
        "EURUSD" => "CBOE:EVZ"
        "IBM" => "CBOE:VXIBM"
        "GOOGL" => "CBOE:VXGOG"
        "AMZN" => "CBOE:VXAZN"
        "AAPL" => "CBOE:VXAPL"
        => na