basic ThinkScript syntax for IF statements

Razzoo

New member
Plus
if showperc {
def percValRounded = Round(PercVal, 2);
def labelId;

if !IsNaN(PercVal) {
labelId = CreateLabel(AsPercent(percValRounded), TextAlignment.ABOVE_BAR);
} else {
labelId = Double.NaN;
}

if !IsNaN(labelId) {
SetColor(labelId, Color.WHITE);
} else {
SetLabelTextColor(labelId, if PercVal >= 0 then Color.LIME else Color.ORANGE);}
};


# Buy and sell signals
def goodlong = long and buyprice <= sellprice;
def goodshort = short and sellprice > buyprice;
def txtlight_b = Color.LIME; # Buy signal set to always green.
def txtlight_s = if goodshort then Color.RED else Color.YELLOW;

This is part of the code I'm having problems with. If I omit the semicolon at the end of the braces, then the next line (def goodlong = long and buyprice <= sellprice;) shows red and the first if and final brace are red. If I put a semicolon after the brace, like it shows above, I no longer get the error in the "def goodlong" line but still get the red on the if and final brace. Any help would be appreciated.
 
if showperc {
def percValRounded = Round(PercVal, 2);
def labelId;

if !IsNaN(PercVal) {
labelId = CreateLabel(AsPercent(percValRounded), TextAlignment.ABOVE_BAR);
} else {
labelId = Double.NaN;
}

if !IsNaN(labelId) {
SetColor(labelId, Color.WHITE);
} else {
SetLabelTextColor(labelId, if PercVal >= 0 then Color.LIME else Color.ORANGE);}
};


# Buy and sell signals
def goodlong = long and buyprice <= sellprice;
def goodshort = short and sellprice > buyprice;
def txtlight_b = Color.LIME; # Buy signal set to always green.
def txtlight_s = if goodshort then Color.RED else Color.YELLOW;

This is part of the code I'm having problems with. If I omit the semicolon at the end of the braces, then the next line (def goodlong = long and buyprice <= sellprice;) shows red and the first if and final brace are red. If I put a semicolon after the brace, like it shows above, I no longer get the error in the "def goodlong" line but still get the red on the if and final brace. Any help would be appreciated.

just looking at code on my cell..

thinkscript doesn't highlight red every error at the same time. that gives a false impression that just a few errors exist, when there are many.

i'm going to guess this is a chapgpt code, isn't it?
there are several function names that don't exist in thinkscript, so fixing isn't possible.



general things,
need a def before if then.
need an else with if then.
can't have def within a if then.
can't set plots or colors within a if then.

if showperc {
def percValRounded = Round(PercVal, 2);

should be

def percValRounded;
if showperc then {
percValRounded = Round(PercVal, 2);
} else {
percValRounded = 0;
}
 

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

if showperc {
def percValRounded = Round(PercVal, 2);
def labelId;

if !IsNaN(PercVal) {
labelId = CreateLabel(AsPercent(percValRounded), TextAlignment.ABOVE_BAR);
} else {
labelId = Double.NaN;
}

if !IsNaN(labelId) {
SetColor(labelId, Color.WHITE);
} else {
SetLabelTextColor(labelId, if PercVal >= 0 then Color.LIME else Color.ORANGE);}
};


# Buy and sell signals
def goodlong = long and buyprice <= sellprice;
def goodshort = short and sellprice > buyprice;
def txtlight_b = Color.LIME; # Buy signal set to always green.
def txtlight_s = if goodshort then Color.RED else Color.YELLOW;

This is part of the code I'm having problems with. If I omit the semicolon at the end of the braces, then the next line (def goodlong = long and buyprice <= sellprice;) shows red and the first if and final brace are red. If I put a semicolon after the brace, like it shows above, I no longer get the error in the "def goodlong" line but still get the red on the if and final brace. Any help would be appreciated.
As @Joshua told you in your last post, the scripts that you are submitting are not thinkscript.

Besides the errors cited by @halcyonguy above, here are a few more:
The ToS platform does not allow for creating labels within {} script functions.
The ToS platform does not allow for creating label color within {} script functions.
The ToS platform does not allow for defining colors in def statements.

You did not provide any information as to what you are trying to do.


So this is a random example of real thinkscript based on some keywords in your post.
D17dQs3.png

Ruby:
# Trade Price and % gain / loss
def plBuy = GetAveragePrice();
def plStraightLine = highestall(plBuy);

plot x=plStraightLine;
x.SetDefaultColor(Color.blue);
x.SetLineWeight(3);

Addlabel(plBuy, "Buy Price: " +plBuy  +"  |Sell Price: " +close  +" |pct gain: "  +close/plbuy,
if plBuy < close then color.green else color.red);
 
Last edited:
When learning thinkscript, it is best to search the forum for a script that has the basic building blocks for what you are attempting.

At least then you have the basic ThinkScript syntax to start with and all you have to do is change the math and logic.

Luckily, ToS's mathematical and logic syntax is uniform to all other languages.
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
299 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