NexusFi: Find Your Edge


Home Menu

 





Thinkscript If Statement


Discussion in ThinkOrSwim

Updated
      Top Posters
    1. looks_one newuse with 7 posts (1 thanks)
    2. looks_two mobiusrts with 2 posts (4 thanks)
    3. looks_3 growex with 2 posts (3 thanks)
    4. looks_4 shzhning with 1 posts (1 thanks)
      Best Posters
    1. looks_one mobiusrts with 2 thanks per post
    2. looks_two growex with 1.5 thanks per post
    3. looks_3 shzhning with 1 thanks per post
    4. looks_4 newuse with 0.1 thanks per post
    1. trending_up 16,809 views
    2. thumb_up 9 thanks given
    3. group 4 followers
    1. forum 11 posts
    2. attach_file 0 attachments




 
Search this Thread

Thinkscript If Statement

  #1 (permalink)
newuse
Atlanta
 
Posts: 9 since May 2015
Thanks Given: 4
Thanks Received: 1

Dear Fellow ThinkScript Coders of Big Mike Trading:

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.

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Better Renko Gaps
The Elite Circle
ZombieSqueeze
Platforms and Indicators
REcommedations for programming help
Sierra Chart
How to apply profiles
Traders Hideout
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
34 thanks
Tao te Trade: way of the WLD
24 thanks
GFIs1 1 DAX trade per day journal
17 thanks
Vinny E-Mini & Algobox Review TRADE ROOM
13 thanks
My NQ Trading Journal
12 thanks
  #2 (permalink)
newuse
Atlanta
 
Posts: 9 since May 2015
Thanks Given: 4
Thanks Received: 1

Update:

Dear Fellow ThinkScript Coders of Big Mike Trading:

I am having a problem with this block of 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);
};

Thank you in advanced.

Reply With Quote
  #3 (permalink)
 shzhning 
Madison, NJ
 
Experience: Intermediate
Platform: CQG/TOS
Broker: Optimus/CQG
Trading: ZN/TN/ES/NQ
Posts: 134 since Jun 2010
Thanks Given: 65
Thanks Received: 112


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

Reply With Quote
Thanked by:
  #4 (permalink)
newuse
Atlanta
 
Posts: 9 since May 2015
Thanks Given: 4
Thanks Received: 1

Thank you for your answer @shzhning. I will try out your suggestion. I will update whether this solved the issue.

Reply With Quote
  #5 (permalink)
 growex 
Trubchevsk
 
Experience: Beginner
Platform: tos
Trading: stocks
Posts: 61 since May 2011
Thanks Given: 40
Thanks Received: 79


newuse View Post
Update:

Dear Fellow ThinkScript Coders of Big Mike Trading:

I am having a problem with this block of 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);
};

Thank you in advanced.


You dont need if-then-else

 
Code
                            
plot Dec Decrease;
plot Inc Increase;
Dec.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Dec.SetDefaultColor(Color.RED);
Dec.SetLineWeight(3);
Inc.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Inc.SetDefaultColor(Color.BLUE);
Inc.SetLineWeight(3); 

Follow me on Twitter Reply With Quote
Thanked by:
  #6 (permalink)
newuse
Atlanta
 
Posts: 9 since May 2015
Thanks Given: 4
Thanks Received: 1


growex View Post
You dont need if-then-else

 
Code
                            
plot Dec Decrease;

plot Inc Increase;
Dec.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Dec.SetDefaultColor(Color.RED);
Dec.SetLineWeight(3);
Inc.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Inc.SetDefaultColor(Color.BLUE);
Inc.SetLineWeight(3); 

Does Inc or Dec still paint or show on the graph if the value is 0 or no?

Reply With Quote
  #7 (permalink)
 growex 
Trubchevsk
 
Experience: Beginner
Platform: tos
Trading: stocks
Posts: 61 since May 2011
Thanks Given: 40
Thanks Received: 79


newuse View Post
Does Inc or Dec still paint or show on the graph if the value is 0 or no?

PaintingStrategy.BOOLEAN_ARROW_DOWN - it will show down arrow only if "Decrease" variable value is true or 1.

Follow me on Twitter Reply With Quote
Thanked by:
  #8 (permalink)
mobiusrts
pensacola florida
 
Posts: 7 since Jul 2014
Thanks Given: 0
Thanks Received: 8

For a conditional variable (seems like you were trying to get to this in your post)

## Simple Conditional Variables Test
# Mobius
# V01.07.2014

# Variables:
def o;
def h;
def l;
def c;
def a;
def b;

# 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);

Reply With Quote
Thanked by:
  #9 (permalink)
newuse
Atlanta
 
Posts: 9 since May 2015
Thanks Given: 4
Thanks Received: 1


mobiusrts View Post
For a conditional variable (seems like you were trying to get to this in your post)

## Simple Conditional Variables Test
# Mobius
# V01.07.2014

# Variables:
def o;
def h;
def l;
def c;
def a;
def b;

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

Thank you for your time.

Reply With Quote
Thanked by:
  #10 (permalink)
mobiusrts
pensacola florida
 
Posts: 7 since Jul 2014
Thanks Given: 0
Thanks Received: 8


I copied and pasted my original code from this forum and it plots the label fine. You likely missed some of the code in your copy and paste.

Reply With Quote
Thanked by:




Last Updated on May 18, 2015


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts