I need help with some code (if statements)

DonS

New member
VIP
I have 3 different conditions. I want to plot only one based on criteria.

I have Three input factors: High, Medium, Low
Code:
input FactorHigh = 3.5;
input FactorMedium = 3.0;
input FactorLow = 2.5;

I define each
Code:
def fHigh = close > RSI(factor = FactorHigh);
def fMedium = close < RSI(factor = FactorHigh) and close > RSI(factor = FactorMedium);
def fLow = close < RSI(factor = FactorMedium) and close > RSI(factor = FactorLow);

I want to Plot:
Code:
If fHigh then plot a red square
If fMedium then plot a yellow square
If fLow then plot a green sqaure

I am having trouble understanding how to assign symbols, colors, size, etc., within if statements.

Any help is appreciated.

Don

Yes. Trying to plot on the chart.
 
Last edited by a moderator:
Solution
Can you please give me an example of how I would create such a label?

TY
tips and examples are on the page i linked you to, if you cant understand them then you will have to post the code you are working with, many members trying to assist you have repeatedly mentioned this to you already.you have failed to post the code you are working with, we can not help you any further as we dont know what code you are looking at.

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

I have 3 different conditions. I want to plot only one based on criteria.

I have Three input factors: High, Medium, Low
Code:
input FactorHigh = 3.5;
input FactorMedium = 3.0;
input FactorLow = 2.5;

I define each
Code:
def fHigh = close > RSI(factor = FactorHigh);
def fMedium = close < RSI(factor = FactorHigh) and close > RSI(factor = FactorMedium);
def fLow = close < RSI(factor = FactorMedium) and close > RSI(factor = FactorLow);

I want to Plot:
Code:
If fHigh then plot a red square
If fMedium then plot a yellow square
If fLow then plot a green sqaure

I am having trouble understanding how to assign symbols, colors, size, etc., within if statements.

Any help is appreciated.

Don
your logic is all wrong
rsi doesn't have an input called factor.
comparing price to rsi is meaningless.

write out each condition and what happens if it is true

https://toslc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/R-S/RSI
 
I have 3 different conditions. I want to plot only one based on criteria.

I have Three input factors: High, Medium, Low
Code:
input FactorHigh = 3.5;
input FactorMedium = 3.0;
input FactorLow = 2.5;

I define each
Code:
def fHigh = close > RSI(factor = FactorHigh);
def fMedium = close < RSI(factor = FactorHigh) and close > RSI(factor = FactorMedium);
def fLow = close < RSI(factor = FactorMedium) and close > RSI(factor = FactorLow);

I want to Plot:
Code:
If fHigh then plot a red square
If fMedium then plot a yellow square
If fLow then plot a green sqaure

I am having trouble understanding how to assign symbols, colors, size, etc., within if statements.

Any help is appreciated.

Don

Yes. Trying to plot on the chart.
little tip: not sure what you are trying to do, but adding a label to your script will tell you what the defined line of code's output is. in this case the IF statements line you can make a label for that particular line and it will show you what the output is. you can then do edits until you get the result you want.
 
Thank you all for your feedback.

I am not concerned with logic or format. I used RSI just to have something there. Consider them variables or use anything you want.

I am interested in the syntax to plot the information in the format described above. I am thinking that would require if then statements. Maybe something else.

I can upload the acutal code I’m working with if it would help.

Thank you,

Don
 
I have 3 different conditions. I want to plot only one based on criteria.

I have Three input factors: High, Medium, Low
Code:
input FactorHigh = 3.5;
input FactorMedium = 3.0;
input FactorLow = 2.5;

I define each
Code:
def fHigh = close > RSI(factor = FactorHigh);
def fMedium = close < RSI(factor = FactorHigh) and close > RSI(factor = FactorMedium);
def fLow = close < RSI(factor = FactorMedium) and close > RSI(factor = FactorLow);

I want to Plot:
Code:
If fHigh then plot a red square
If fMedium then plot a yellow square
If fLow then plot a green sqaure

I am having trouble understanding how to assign symbols, colors, size, etc., within if statements.

Any help is appreciated.

Don

Yes. Trying to plot on the chart.

next time, type out 'code is just an example'
or better yet, just post the real code.
your post #1 sent me on a wild goose chase.

------------------------

typical TOS, won't load on a weekend... so just typing this out without testing


to plot a shape to have 1 of 3 colors, put an if-then within a .AssignValueColor( )

Code:
def na = double.nan;
plot z= if (fHigh or fMedium or fLow) then high*1.001 else na;
z.SetPaintingStrategy(PaintingStrategy.SQUARES);
z.AssignValueColor( If fHigh then color.red else if fMedium then color.yellow else if fLow then color.green  else color.black);

https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/AssignValueColor
 
Okay. Please forgive me for causing confusion and wasting your time.

Here is the code I have so far:

Code:
#dls_ExtendedKeltnerChannels

# Three input factors: High, Medium, Low
input KCFactorHigh = 3.5;
input KCFactorMedium = 3.0;
input KCFactorLow = 2.5;

def na = double.nan;

# Define variables
def highLong = close > KeltnerChannels(factor = KCFactorHigh).UpperBand;
def mediumLong = close > KeltnerChannels(factor = KCFactorMedium).UpperBand;
def lowLong = close > KeltnerChannels(factor = KCFactorLow).UpperBand;

def highShort = close < KeltnerChannels(factor = KCFactorHigh).LowerBand;
def mediumShort = close < KeltnerChannels(factor = KCFactorMedium).LowerBand;
def lowShort = close < KeltnerChannels(factor = KCFactorLow).LowerBand;

# Plot
Plot longExtendedKC = if highLong then ?? else if highMedium then ?? else if lowLong then ?? else na;

Plot shortExtendedKC = if highShort then ?? else if mediumShort then ?? else if lowShort then ?? else na;

# [B]* SetChartType Shapes and colors of plots *[/B]

# High
extLongHigh.setPaintingStrategy(PaintingStrategy.Boolean_Wedge_Up);
extLongHigh.setDefaultColor(Color.MAGENTA);
extLongHigh.setLineWeight(5);
#-----
extShortHigh.setPaintingStrategy(PaintingStrategy.Boolean_Wedge_Down);
extShortHigh.setDefaultColor(Color.CYAN);
extShortHigh.setLineWeight(5);

#Medium
extLongMedium.setPaintingStrategy(PaintingStrategy.Boolean_Wedge_Up);
extLongMedium.setDefaultColor(Color.YELLOW);
extLongMedium.setLineWeight(5);
#-----
extShortMedium.setPaintingStrategy(PaintingStrategy.Boolean_Wedge_Down);
extShortMedium.setDefaultColor(Color.YELLOW);
extShortMedium.setLineWeight(5);

#Low
extLongLow.setPaintingStrategy(PaintingStrategy.Boolean_Wedge_Up);
extLongLow.setDefaultColor(Color.ORANGE);
extLongLow.setLineWeight(5);
#-----
extShortLow.setPaintingStrategy(PaintingStrategy.Boolean_Wedge_Down);
extShortLow.setDefaultColor(Color.ORANGE);
extShortLow.setLineWeight(5);

### End of code for dls_ExtendedKeltnerChannels ###
 
Last edited by a moderator:
Okay. Please forgive me for causing confusion and wasting your time.

Here is the code I have so far:

#dls_ExtendedKeltnerChannels

# Three input factors: High, Medium, Low
input KCFactorHigh = 3.5;
input KCFactorMedium = 3.0;
input KCFactorLow = 2.5;

def na = double.nan;

# Define variables
def highLong = close > KeltnerChannels(factor = KCFactorHigh).UpperBand;
def mediumLong = close > KeltnerChannels(factor = KCFactorMedium).UpperBand;
def lowLong = close > KeltnerChannels(factor = KCFactorLow).UpperBand;

def highShort = close < KeltnerChannels(factor = KCFactorHigh).LowerBand;
def mediumShort = close < KeltnerChannels(factor = KCFactorMedium).LowerBand;
def lowShort = close < KeltnerChannels(factor = KCFactorLow).LowerBand;

# Plot
Plot longExtendedKC = if highLong then ?? else if highMedium then ?? else if lowLong then ?? else na;

Plot shortExtendedKC = if highShort then ?? else if mediumShort then ?? else if lowShort then ?? else na;

# * SetChartType Shapes and colors of plots *

# High
extLongHigh.setPaintingStrategy(PaintingStrategy.Boolean_Wedge_Up);
extLongHigh.setDefaultColor(Color.MAGENTA);
extLongHigh.setLineWeight(5);
#-----
extShortHigh.setPaintingStrategy(PaintingStrategy.Boolean_Wedge_Down);
extShortHigh.setDefaultColor(Color.CYAN);
extShortHigh.setLineWeight(5);

#Medium
extLongMedium.setPaintingStrategy(PaintingStrategy.Boolean_Wedge_Up);
extLongMedium.setDefaultColor(Color.YELLOW);
extLongMedium.setLineWeight(5);
#-----
extShortMedium.setPaintingStrategy(PaintingStrategy.Boolean_Wedge_Down);
extShortMedium.setDefaultColor(Color.YELLOW);
extShortMedium.setLineWeight(5);

#Low
extLongLow.setPaintingStrategy(PaintingStrategy.Boolean_Wedge_Up);
extLongLow.setDefaultColor(Color.ORANGE);
extLongLow.setLineWeight(5);
#-----
extShortLow.setPaintingStrategy(PaintingStrategy.Boolean_Wedge_Down);
extShortLow.setDefaultColor(Color.ORANGE);
extShortLow.setLineWeight(5);

### End of code for dls_ExtendedKeltnerChannels ###

lol. and what exactly are you trying to do with this code? it has little to no reference to your original post.

def fHigh =
def fMedium =
def fLow =


ALL three of those def's in bold above are missing from what you just posted above.

1) you started off with asking how if conditions work as if you were trying to learn.
2) then you specifically ask for how something is done with no code reference.
3) now you are posting a code that has no relevance to your original question.

trust me whatever you are holding back from sharing has most likely already been thought of and most likely is not going to be the magical indicator/study of the decade. but if you don't want to share your code cause you feel its proprietary then we cant help.

@halcyonguy is correct, this is becoming a guessing wild goose chase game with roads that keep changing signs.
 
Last edited:
Hopefully a picture really is worth a thousand words.

Please see attached.

Here are the lower ones.
 

Attachments

  • ExtKC1.png
    ExtKC1.png
    62.3 KB · Views: 86
  • 2024-06-30_13-02-04.png
    2024-06-30_13-02-04.png
    9.2 KB · Views: 83
Hopefully a picture really is worth a thousand words.

Please see attached.

Here are the lower ones.

You are correct, a picture is worth a thousand words.
However, tiny snippets of a chart provide little illumination.
When submitting images, include the whole chart.
AND THEN annotate the confluence that you are describing.

Also, a shared chart link will allow the contributors to see what you are seeing and provide more effective assistance.
https://usethinkscript.com/threads/how-to-share-a-chart-in-thinkorswim.14221/
 
Hopefully a picture really is worth a thousand words.

Please see attached.

look at my post 7 see see how different colors can be applied to a shape

now, you have made 5 posts, and we still don't know your rules/conditions for determining what you want.
you say higher, but what is higher, and what colors? how many colors? 3, 5, 20? what happens when the max quantity is reached, keep plotting the last color? don't plot anything ? what if only 2 bars rise then price falls? after how many bars to you give up and reset?
i am asking you a lot of questions to help you start thinking about the possibilities and how to handle them.

programming is trying to create a set of rules, to handle any combination of inputs and giving valid outputs.
 
Hopefully a picture really is worth a thousand words.

Please see attached.

Here are the lower ones.

in your picture now you said

"i want to have these plot different colors"

since you have failed to provide enough information (and/or the code with what you are working with), here is the information you will need to change colors:
Code:
https://usethinkscript.com/resources/thinkscript-colors-list-of-standard-colors-for-thinkorswim-indicators.7/
 
little tip: not sure what you are trying to do, but adding a label to your script will tell you what the defined line of code's output is. in this case the IF statements line you can make a label for that particular line and it will show you what the output is. you can then do edits until you get the result you want.
Can you please give me an example of how I would create such a label?

TY
 
Can you please give me an example of how I would create such a label?

TY
tips and examples are on the page i linked you to, if you cant understand them then you will have to post the code you are working with, many members trying to assist you have repeatedly mentioned this to you already.you have failed to post the code you are working with, we can not help you any further as we dont know what code you are looking at.
 
Solution

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
291 Online
Create Post

Similar threads

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