help creating a switch?

mbarcala

Active member
VIP
I trying to create a switch as a part of my indicator
https://usethinkscript.com/threads/break-price-momentum-bpm-for-thinkorswim.9453/
, some help here please?

Code:
input reverType = {default detrendOne, detrendTwo};

############### Posible Price Reversals ################
plot trendUp = if BPM > centlev and BPM crosses above BPM[1] then BPM + -0.1 else Double.NaN;
trendUp.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
trendUp.SetDefaultColor(Color.WHITE);
trendUp.SetLineWeight(1);
trendUp.HideBubble();
trendUp.HideTitle();

plot trendDn = if BPM < -centlev and BPM crosses below BPM[1] then BPM + 0.1 else Double.NaN;
trendDn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
trendDn.SetDefaultColor(Color.WHITE);
trendDn.SetLineWeight(1);
trendDn.HideBubble();
trendDn.HideTitle();

############## Possible Exit Levels #################
def extValue = if BPM >= exitLevelBeg then RoundDown((BPM - exitLevelBeg) / calDays, 0) + 1 else if BPM <= -exitLevelBeg then RoundDown((-exitLevelBeg - BPM) / calDays, 0) + 1 else 0;
plot exts = if extValue > 0 and extValue[1] != extValue then BPM else Double.NaN;
exts.SetPaintingStrategy(PaintingStrategy.POINTS);
exts.SetDefaultColor(Color.MAGENTA);
exts.SetLineWeight(3);
exts.HideBubble();
exts.HideTitle();

############## Multi Revertion Points #################
plot upBuy = if flen crosses above flen[1] and !IsNaN(close) then flen[1] else Double.NaN;
upBuy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
upBuy.SetDefaultColor(Color.MAGENTA);
upBuy.SetLineWeight(1);

plot dnSell = if flen crosses below flen[1] and !IsNaN(close) then flen[1] else Double.NaN;
dnSell.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
dnSell.SetDefaultColor(Color.MAGENTA);
dnSell.SetLineWeight(1);



switch (reverType) {
    case detrendOne:
         Posible Price Reversals should go here
         Possible Exit Levels should go here
    case detrendTwo:
         Multi Revertion Points should go here
}
 
Last edited by a moderator:
Solution
@mbarcala
My previous post was just for example.
Every 'case:' has to assign every def or plot used in the switch.
Code:
plot trendUp;
plot trendDn;
plot exts;
plot upBuy;
plot dnSell;

switch (reverType) {
case "Detrend":
    trendUp = if btUp then BPM + -0.1 else Double.NaN;
    trendDn = if btDn then BPM + 0.1 else Double.NaN;
    exts = if ext then BPM else Double.NaN;
    upBuy = Double.NaN;
    dnSell = Double.NaN;

case "MultiDetrend":
     trendUp = Double.NaN;
    trendDn = Double.NaN;
    exts = Double.NaN;   
    upBuy = if bupB then flen[1] else Double.NaN;
    dnSell = if bdnS then flen[1] else Double.NaN;
}

Svanoy

Active member
@mbarcala
You have to use switch to def a variable.
Code:
def Var1;
def Var2;
def Var3;
switch (reverType) {
case detrendOne:
         Var1 = trendUp;
         Var2 = exts;
         Var3 = UpBuy;       
case detrendTwo:
         Var1 = trendDn;
         Var2 = exts;
         Var3 = DnSell;
}
 

mbarcala

Active member
VIP
@mbarcala
You have to use switch to def a variable.
Code:
def Var1;
def Var2;
def Var3;
switch (reverType) {
case detrendOne:
         Var1 = trendUp;
         Var2 = exts;
         Var3 = UpBuy;      
case detrendTwo:
         Var1 = trendDn;
         Var2 = exts;
         Var3 = DnSell;
}
I follow your way but for some reason it keep show me all, it do not select me one group or the other one, here my code configuration on the switch
Code:
def type1;
def type2;
def type3;

switch (reverType) {
    case Detrend:
        type1 = trendUp;
        type2 = trendDn;
        type3 = exts;   

    case MultiDetrend:
        type1 = upBuy;
        type2 = dnSell;
        type3 = 0;
}
 

halcyonguy

Well-known member
VIP
Lifetime
I follow your way but for some reason it keep show me all, it do not select me one group or the other one, here my code configuration on the switch
Code:
def type1;
def type2;
def type3;

switch (reverType) {
    case Detrend:
        type1 = trendUp;
        type2 = trendDn;
        type3 = exts;  

    case MultiDetrend:
        type1 = upBuy;
        type2 = dnSell;
        type3 = 0;
}

If you added the post#2 switch code to the end of your code in post#1, the only way the switch will change is if you go to edit studies and change the input variable, reverType.

what do you want to see happen?

if you want the program to change the value of reverType, what formula or conditions would do that? what conditions would determine which of these to use?
detrendOne, detrendTwo

maybe an if-then would work better?

here is an example of code that changes the controlling variable within a switch case function.
notice the switch line has an offset of 1 on state.
If you are going to be changing the switch variable, within a case line, then you have to use the previous value to control the switch case.

def state = {default init, long, short};
switch (state[1]) {
case init:
state = state.long;
.....

https://usethinkscript.com/threads/parabolic-sar-psar-question.515/#post-9830
 

mbarcala

Active member
VIP
If you added the post#2 switch code to the end of your code in post#1, the only way the switch will change is if you go to edit studies and change the input variable, reverType.

what do you want to see happen?

if you want the program to change the value of reverType, what formula or conditions would do that? what conditions would determine which of these to use?
detrendOne, detrendTwo

maybe an if-then would work better?

here is an example of code that changes the controlling variable within a switch case function.
notice the switch line has an offset of 1 on state.
If you are going to be changing the switch variable, within a case line, then you have to use the previous value to control the switch case.

def state = {default init, long, short};
switch (state[1]) {
case init:
state = state.long;
.....

https://usethinkscript.com/threads/parabolic-sar-psar-question.515/#post-9830
here is what I what to happen, I follow the same sample as the stochasticFull indicator switch
Code:
input reverType = {default "Detrend", "MultiDetrend"};

def btUp = BPM > centlev and BPM crosses above BPM[1];
def btDn = BPM < -centlev and BPM crosses below BPM[1];
def ext = extValue > 0 and extValue[1] != extValue;
def bupB = flen crosses above flen[1] and !IsNaN(close);
def bdnS = flen crosses below flen[1] and !IsNaN(close);

plot trendUp;
plot trendDn;
plot exts;
plot upBuy;
plot dnSell;

switch (reverType) {
case "Detrend":
    trendUp = if btUp then BPM + -0.1 else Double.NaN;
    trendDn = if btDn then BPM + 0.1 else Double.NaN;
    exts = if ext then BPM else Double.NaN;
case "MultiDetrend":
    upBuy = if bupB then flen[1] else Double.NaN;
    dnSell = if bdnS then flen[1] else Double.NaN;
}

trendUp.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
trendUp.SetDefaultColor(Color.WHITE);
trendUp.SetLineWeight(1);
trendUp.HideBubble();
trendUp.HideTitle();
#--------------
trendDn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
trendDn.SetDefaultColor(Color.WHITE);
trendDn.SetLineWeight(1);
trendDn.HideBubble();
trendDn.HideTitle();

exts.SetPaintingStrategy(PaintingStrategy.POINTS);
exts.SetDefaultColor(Color.MAGENTA);
exts.SetLineWeight(3);
exts.HideBubble();
exts.HideTitle();

upBuy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
upBuy.SetDefaultColor(Color.MAGENTA);
upBuy.SetLineWeight(1);
upBuy.SetHiding(reverType == reverType."MultiDetrend");

dnSell.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
dnSell.SetDefaultColor(Color.MAGENTA);
dnSell.SetLineWeight(1);
dnSell.SetHiding(reverType == reverType."MultiDetrend");

the error I getting now is Value never assigned to trendUp. it repeat the same error for the 5 plot. by the way because that error I still don't know if it change at all! lol
 

Svanoy

Active member
@mbarcala
My previous post was just for example.
Every 'case:' has to assign every def or plot used in the switch.
Code:
plot trendUp;
plot trendDn;
plot exts;
plot upBuy;
plot dnSell;

switch (reverType) {
case "Detrend":
    trendUp = if btUp then BPM + -0.1 else Double.NaN;
    trendDn = if btDn then BPM + 0.1 else Double.NaN;
    exts = if ext then BPM else Double.NaN;
    upBuy = Double.NaN;
    dnSell = Double.NaN;

case "MultiDetrend":
     trendUp = Double.NaN;
    trendDn = Double.NaN;
    exts = Double.NaN;   
    upBuy = if bupB then flen[1] else Double.NaN;
    dnSell = if bdnS then flen[1] else Double.NaN;
}
 
Solution

Similar threads

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.
Top