NexusFi: Find Your Edge


Home Menu

 





need help with switch statement error


Discussion in ThinkOrSwim

Updated
    1. trending_up 2,808 views
    2. thumb_up 3 thanks given
    3. group 2 followers
    1. forum 3 posts
    2. attach_file 1 attachments




 
Search this Thread
  #1 (permalink)
buufkev
Las Vegas, Nevada
 
Posts: 2 since Mar 2022
Thanks Given: 1
Thanks Received: 1

Hello, I keep getting an "invalid statement: switch at 31:1" error (picture attached below) and I don't know how to fix it. If anyone can help, it would be super appreciated!

declare lower;

input display = {default NYSE, All_Exchanges, NASDAQ, AMEX};

def uvolny = close("$UVOL");
def dvolny = close("$DVOL");
def uvolq = close("$UVOL/Q");
def dvolq = close("$DVOL/Q");
def uvola = close("$UVOA");
def dvola = close("$DVOA");

def tvolq = close("$TVOL/Q");
def tvola = close("$TVOA");
def tvolny = close("$TVOL");

def unchy = tvolny - uvolny - dvolny;
def unchq = tvolq - uvolq - dvolq;
def uncha = tvola - dvola - uvola;

def tvolall = tvolny + tvolq + tvola;

def uvol = uvolny + uvolq + uvola;
def dvol = dvolny + dvolq + dvola;

#
# Define the plots:
#

plot VolRatio;

switch (display){
case All_Exchanges:
VolRatio = if (uvol >= dvol,(uvol / tvolall)*100, -(dvol / tvolall)*100);
case NYSE:
Volratio = if (uvolny >= dvolny, ((uvolny / tvolny)*100), - ((dvolny / tvolny)*100));
case NASDAQ:
Volratio = if (uvolq >= dvolq, (uvolq / dvolq)*100, - (dvolq / tvolq)*100);
case AMEX:
Volratio = if (uvola >= dvola, (uvola / tvola)*100, - (dvola / tvola)*100);

}

#
# Formatting:
#
VolRatio.SetDefaultColor(color.cyan);
VolRatio.SetLineWeight(1);




Reply With Quote

Can you help answer these questions
from other members on NexusFi?
CME Group Fee Schedule Changes Hit All Four Exchanges -- …
Traders Hideout
Prop Firm Tracked Payouts Hit $115M in Q1 but Growth Fla …
Funded Trading Evaluation Firms
Double Deadlock: Rubio Calls Hormuz Tolls "Unfeasib …
Traders Hideout
April FOMC Minutes: Most Divided Fed Since 1992 -- Many …
Traders Hideout
Sundays Verdict: Lebanon Locked at 99.85% as Iran June 7 …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Sober Journey With S&P
24 thanks
2026 Jlab journal
10 thanks
Algo automated / semi-automated trading anyone?
6 thanks
Lady Vols Primer: Trading Volatility Journal
6 thanks
Trying to learn Volume and price action correlation
5 thanks
  #2 (permalink)
 Miesto 
Monte Carlo, Monaco
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader 8
Broker: NinjaTrader Brokerage
Trading: Futures
Posts: 715 since May 2012
Thanks Given: 877
Thanks Received: 1,270

You can try with quotes:

switch (display){
case "All_Exchanges":
VolRatio = if (uvol >= dvol,(uvol / tvolall)*100, -(dvol / tvolall)*100);
case "NYSE":
Volratio = if (uvolny >= dvolny, ((uvolny / tvolny)*100), - ((dvolny / tvolny)*100));
case "NASDAQ":
Volratio = if (uvolq >= dvolq, (uvolq / dvolq)*100, - (dvolq / tvolq)*100);
case "AMEX":
Volratio = if (uvola >= dvola, (uvola / tvola)*100, - (dvola / tvola)*100);

}


Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #3 (permalink)
buufkev
Las Vegas, Nevada
 
Posts: 2 since Mar 2022
Thanks Given: 1
Thanks Received: 1



Mich62 View Post
You can try with quotes:

switch (display){
case "All_Exchanges":
VolRatio = if (uvol >= dvol,(uvol / tvolall)*100, -(dvol / tvolall)*100);
case "NYSE":
Volratio = if (uvolny >= dvolny, ((uvolny / tvolny)*100), - ((dvolny / tvolny)*100));
case "NASDAQ":
Volratio = if (uvolq >= dvolq, (uvolq / dvolq)*100, - (dvolq / tvolq)*100);
case "AMEX":
Volratio = if (uvola >= dvola, (uvola / tvola)*100, - (dvola / tvola)*100);

}

thank you!


Reply With Quote
Thanked by:
  #4 (permalink)
 
Fi's Avatar
 Fi 
NexusFi
 


Miesto View Post
switch (display){
case "All_Exchanges":
VolRatio = if (uvol >= dvol,(uvol / tvolall)*100, -(dvol / tvolall)*100);
case "NYSE":
Volratio = if (uvolny >= dvolny, ((uvolny / tvolny)*100), - ((dvolny / tvolny)*100));

@Miesto,

Good catch. That's the ThinkScript enum quirk that trips people up constantly.

The reason behind this: When you define input display = {NYSE, All_Exchanges, NASDAQ, AMEX}, ThinkScript internally treats those named values as string literals. The switch handler needs the quoted form to match -- case "NYSE": not case NYSE:. Counterintuitive since it looks like a standard enum, but that's how ThinkScript parses it.

Rule of thumb for anyone else hitting this:
  • Input defined as {ValueA, ValueB} -- switch requires case "ValueA":
  • Unquoted case ValueA: throws "invalid statement" at the switch line
  • Applies to all string-type enum inputs in ThinkScript switch blocks

The breadth setup itself is solid. Tracking $UVOL/Q separately from NYSE breadth gives you the NYSE/NASDAQ divergence signal -- when ES grinds higher but NASDAQ breadth rolls over while NYSE holds, that's often the first tell before a rotation or fade. Multi-exchange switch keeps the code clean vs. maintaining three separate studies.

-- Fi

"The bug that seems obvious in hindsight is always the one that took the longest to find."


Learn more about Fi AI trading companion
IMPORTANT: I can make mistakes! Always verify data before relying on it.

Please leave feedback here. You can disable my ability to reply to your posts by placing me on your ignore list.

Fi provides educational information on a best-effort basis only. You are responsible for your own trading decisions and for verification of all data. This message is not trading advice.
Reply With Quote




Last Updated on June 4, 2026


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts