Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
I am a beginning thinkscript programmer and I am learning the syntax of thinkscript pretty fast. However, I am having trouble with the if statements. I understand you can have one statement inside of an if block but is it possible to have multiple statements in an if block?
Not: if (condition) then <this> else <that>;
but: if (condition) then { <this>; <that>;};
Here is example code:
def alertMe =
if (Decrease) then {
Decrease.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Decrease.SetDefaultColor(Color.RED);
Decrease.SetLineWeight(3);
} else if (Increase) then {
Increase.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Increase.SetDefaultColor(Color.BLUE);
Increase.SetLineWeight(3);
};
Alert(alertMe, "ICB", Alert.Bar, Sound.bell);
Alert(!alertMe, "No changes", Alert.Bar, Sound.bell);
Thank you in advanced.
Can you help answer these questions from other members on NexusFi?
i'm not a programmer, but i think you use if/else if when you have 3 conditions, and if/else when you have 2 conditions. looks to me you have 2 conditions, and should try if/else, not if/else if
# Calculations
o = (open + close[1]) / 2;
h = Max(high, close[1]);
l = Min(low, close[1]);
c = (o + h + l + close) / 4;
if c >= o
then {
a = 1;
b = 0;
} else if c < o
then {
a = 0;
b = 1;
} else {
a = Double.NaN;
b = Double.NaN;
}
AddLabel(1, "Close higher = " + a + " Close lower " + b, if a == 1 then color.green else color.red);
# Calculations
o = (open + close[1]) / 2;
h = Max(high, close[1]);
l = Min(low, close[1]);
c = (o + h + l + close) / 4;
if c >= o
then {
a = 1;
b = 0;
} else if c < o
then {
a = 0;
b = 1;
} else {
a = Double.NaN;
b = Double.NaN;
}
AddLabel(1, "Close higher = " + a + " Close lower " + b, if a == 1 then color.green else color.red);
Thank you for your suggestion and the simple example @mobiusrts. It rejected this structure of the if statement for some reason but I will compare this example with my previous code to gain a better understanding of the if structure.
I just wish the TDAmeritrade Sink or Swim library contained an example that had more than one statement inside the if block. That threw me off along with the Human Readable Syntax following after that example.