Having trouble with switch statement formatting.

Monkey Hammer

New member
Hello All! New to the forum. Thanks in advance if anyone can help me. :) I'll do this in "Long" mode understanding the reverse is applicable for "short" mode. I'm trying to indicate the first pullback price bars after a bullish trailing stop flip. I use another line I call a Bolic which is reall a trailing stop with different, quicker, settings. So, if the trailing stop flips bull, I save that state until the price flips the Bolic bear which indicates a pullback has begun. The pullback state remains until price has flipped the Bolic bullish or the trailing stop bearish. I hope that makes sense. :) My "long" code is below. The errors are in the **'s. Please excuse my code structure. I guess you can take the dummy out of C++ but you can't take the C++ out of the dummy! :)

# Define the first pullback Flip Zones
def BW; #Buy Window I get this BW in a red box
def SW; #Sell Window
def state = {default FlipCheck, Long, Short};
switch (state)
{
case FlipCheck:
if (vshort[1] > 0 and vlong > 0) #Trailing stop bull flip.
{
state = state.Long; #Buy Window possible.
}
else
{
if (vlong[1] > 0 and vshort > 0) #Trailing stop bear flip.
then
{
state = state.Short; #Sell Window possible.
}
else
{
state = state.FlipCheck; #No trailing stop flip. Keep checking.
}
}
case Long: # we have had a trailing stop flip bull
if (bshort > 0) #if bear bolic short, then pullback has started.
{
BW = 1; #Set buy window to true
}
else #if bear bolic active
{
if (bshort[1] > 0 and blong > 0 or #bear bolic has flipped to bull bolic.
vlong[1] > 0 and vshort > 0) #bull trailstop has flipped to bear.
{
BW = 0; #Cancel buy window
state = state.FlipCheck; #Start checking again for trailng stop flip
}
else
{
BW = 1; #No change. I get this BW in a red box
state = state.Long; #No change. I get this state in a red box
}
}
case Short:
if (blong > 0) #if bull bolic short, then pullback has started.
{
SW = 1; #Set sell window to true
}
else
{
if (blong[1] > 0 and blong > 0 or
vshort[1] > 0 and vlong > 0)
{
SW = 0;
state = state.FlipCheck;
}
else
{
state = state.Short; I get this BW in a red box
SW = 1; I get this state in a red box
}
}
}

plot BuyWindow = if BW == 1 then low else Double.NaN;
plot SellWindow = if SW == 1 then high else Double.NaN;

# BuyWindowSellWindow Formatting:
BuyWindow.SetDefaultColor(Color.LIGHT_GREEN);
BuyWindow.SetStyle(Curve.POINTS);
BuyWindow.SetLineWeight(5);
SellWindow.SetDefaultColor(Color.LIGHT_RED);
SellWindow.SetStyle(Curve.POINTS);
SellWindow.SetLineWeight(5);
# End Code



I have multiple "value not assigned" errors to the "state" and the "BW"
 
Solution
i didn't notice this error before, when a ! appears in top left of chart
trying to self-assign a non-initialized rec: state

GqTGP6y.jpg


i don't think your switch function will work as it is. (it doesn't for me). no points are drawn.

with this
switch (state)

and later
case x:
state = state.Long;

i think the error is saying, you can't change a variable with this, state = state.Long ,
when the same variable, switch (state) , is being used to decide the precedent logic flow.

when i change it to have an offset, to this,
switch (state[1])
the ! error goes away, and it draws points, but on the low of every candle.

Ruby:
#  it is similar to this

# same variable on both sides of = , causes self assign error
#  this...

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

@Monkey Hammer Welcome to the usethinkscript forums... One thing I'll mention is that there are times when mid-statement comments can prove problematic so you might consider discontinuing the practice and placing them either on separate lines or after the statement closing semicolon... I have debugged several coding issues that were caused by the practice... I haven't tested the code as yet but noticed that right from the get go...

Also, it would be helpful if you would use the </> icon in the formatting toolbar to post code and use Ruby syntax highlighting...

Gonna give the code a spin... Again, welcome...
 
@rad14733 & @halconguy thanks for the replies. Offsetting the switch statement and removing mid-statement comments yielded no changes. I have reposted the code as suggested below. I moved the code to a single inidicator for testing. I added the variables from the whole code at the top for testing. I was hoping maybe a look at the code would be enough to sort out the syntax or structure errors. As the variables are currently set it should trigger a buy window and place a light green dot at the low of each bar. It is in the case Long and case Short "else" statements I get the Value never assigned errors.
Ruby:
# Define the first pullback Flip Zones
def vshort = 1;
def vlong = 1;
def bshort = 1;
def blong = 1;

def BW; #Buy Window
def SW; #Sell Window
def state = {default FlipCheck, Long, Short};
switch (state)
{
case FlipCheck:
    #Trailing stop bull flip.
    if (vshort > 0 and vlong > 0)
    {
        state = state.Long; #Buy Window possible. During test this should execute.
    }
    else
    {
        #Trailing stop bear flip.
        if (vlong[1] > 0 and vshort > 0)
        then
        {
            state = state.Short; #Sell Window possible.
        }
        else
        {
            state = state.FlipCheck; #No trailing stop flip. Keep checking.
        }
    }
case Long: # we have had a trailing stop flip bull
    #if bear bolic short, then pullback has started.
    if (bshort > 0)
    {
        BW = 1; #Set buy window to true
    }
    #if bear bolic active
    else
    {
        #bear bolic has flipped to bull bolic or bull trailstop has flipped to bear.
        if (bshort[1] > 0 and blong > 0 or
            vlong[1] > 0 and vshort > 0)
        {
            BW = 0; #Cancel buy window
            state = state.FlipCheck; #Start checking again for trailng stop flip
        }
        else
        {
            BW = 1; #No change.
            state = state.Long; #No change.
        }
    }
case Short:
    #if bull bolic short, then pullback has started.
    if (blong > 0)
    {
        SW = 1; #Set sell window to true
    }
    else
    {
        if (blong[1] > 0 and blong > 0 or
            vshort[1] > 0 and vlong > 0)
        {
            SW = 0;
            state = state.FlipCheck;
        }
        else
        {
            SW = 1;
            state = state.Short;
        }
    }
}

plot BuyWindow = if BW == 1 then low else Double.NaN;
plot SellWindow = if SW == 1 then high else Double.NaN;

# BuyWindowSellWindow Formatting:
BuyWindow.SetDefaultColor(Color.LIGHT_GREEN);
BuyWindow.SetStyle(Curve.POINTS);
BuyWindow.SetLineWeight(5);
SellWindow.SetDefaultColor(Color.LIGHT_RED);
SellWindow.SetStyle(Curve.POINTS);
SellWindow.SetLineWeight(5);
# End Code
 
Last edited:
@Monkey Hammer I'm no wizard at using the switch/case structure in Thinkscript but it appears to me that you are missing some logic in your if...then...else structures... It appears that you are missing a final else statement... Debugging of switch/case logic is not well supported within the code editor... I find convoluted if...then...else structures to work better than switch/case and therefore tend to avoid switch/case in Thinkscript... The same goes for a few other languages I've used over the years... In your code I think you are failing out the bottom of your if...then...else structures and the editors parser can't toss the proper errors... I think that is why you are getting the Value never assigned errors...
 
i copied the code from post 4, and modified it.
when using switch , all the variables defined outside of it, need to be used in each case branch. you are missing a few.
i added some variables. i put this line above each line i added # add this ------------

this got rid of syntax errors. i don't know if it works as intended. i didn't check the if/then logic as rad14733 suggested

Ruby:
# switchtrail_00

#https://usethinkscript.com/threads/having-trouble-with-switch-statement-formatting.7015/#post-67870

# halcyonguy
# added several lines
#    add this ------------


# Define the first pullback Flip Zones
def vshort = 1;
def vlong = 1;
def bshort = 1;
def blong = 1;

#Buy Window  #Sell Window
def BW;
def SW;
def state = {default FlipCheck, Long, Short};
#input state = {default FlipCheck, Long, Short};

switch (state)
{
case FlipCheck:
 # add this ------------
    BW = 0;
 # add this ------------
    sw = 0;

    #Trailing stop bull flip.
    if (vshort > 0 and vlong > 0)
    {
        state = state.Long; #Buy Window possible. During test this should execute.
    }
    else
    {
        #Trailing stop bear flip.
        if (vlong[1] > 0 and vshort > 0)
        then
        {
            state = state.Short; #Sell Window possible.
        }
        else
        {
            state = state.FlipCheck; #No trailing stop flip. Keep checking.
        }
    }
case Long: # we have had a trailing stop flip bull
 # add this------------
    SW = 0;

    #if bear bolic short, then pullback has started.
    if (bshort > 0)
    {
        BW = 1; #Set buy window to true
# add this ------------------
        state = state.Long;

    }
    #if bear bolic active
    else
    {
        #bear bolic has flipped to bull bolic or bull trailstop has flipped to bear.
        if (bshort[1] > 0 and blong > 0 or
            vlong[1] > 0 and vshort > 0)
        {
            BW = 0; #Cancel buy window
            state = state.FlipCheck; #Start checking again for trailng stop flip
        }
        else
        {
            BW = 1; #No change.
            state = state.Long; #No change.
        }
    }
case Short:
 # add this --------------
    BW = 0;

    #if bull bolic short, then pullback has started.
    if (blong > 0)
    {
        SW = 1; #Set sell window to true
# add this ----------
        state = state.Short;

    }
    else
    {
        if (blong[1] > 0 and blong > 0 or
            vshort[1] > 0 and vlong > 0)
        {
            SW = 0;
            state = state.FlipCheck;
        }
        else
        {
            SW = 1;
            state = state.Short;
        }
    }
}

plot BuyWindow = if BW == 1 then low else Double.NaN;
plot SellWindow = if SW == 1 then high else Double.NaN;

# BuyWindowSellWindow Formatting:
BuyWindow.SetDefaultColor(Color.LIGHT_GREEN);
BuyWindow.SetStyle(Curve.POINTS);
BuyWindow.SetLineWeight(5);
SellWindow.SetDefaultColor(Color.LIGHT_RED);
SellWindow.SetStyle(Curve.POINTS);
SellWindow.SetLineWeight(5);
# End Code
 
Thanks you guys! I did not know of the requirement on the variables. @halconguy your code tweaks got rid of the syntax errors. With the variables set as I posted above the logic did paint a light green dot under all the bars. That is not the final logic but I can now test with the syntax correct and my new understanding of the switch rule.

@rad14733 I prefer standard if/else myself. I chose switch/case because I need the code to remember when there is the possibilty of a bull or bear pullback but only on the FIRST pullback after a trail stop flip. I think it was Gann (?) who said "the safest entry is the firrst pullback after some event" or something like that. It could take many bars to get the pullback and a pullback could last many bars. Hence the need to know what "state" we're in. In ACSIL C++ with Sierra I used persistent variables. Are you guys aware of anything like that in ThinkScript? I could not find anything like a global or persistent variable.

I'll post a screen pic when I have it working.

Danke Shon! 🍻
 
i didn't notice this error before, when a ! appears in top left of chart
trying to self-assign a non-initialized rec: state

GqTGP6y.jpg


i don't think your switch function will work as it is. (it doesn't for me). no points are drawn.

with this
switch (state)

and later
case x:
state = state.Long;

i think the error is saying, you can't change a variable with this, state = state.Long ,
when the same variable, switch (state) , is being used to decide the precedent logic flow.

when i change it to have an offset, to this,
switch (state[1])
the ! error goes away, and it draws points, but on the low of every candle.

Ruby:
#  it is similar to this

# same variable on both sides of = , causes self assign error
#  this causes a !  error
def bn = barnumber();
def x = if bn > 100 then x+1 else x;


# add an offset, no error
def bn = barnumber();
def x = if bn > 10 then x[1]+1 else x[1];
 
Last edited:
Solution

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
499 Online
Create Post

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top