Increasing the value of a variable

suts01709

New member
I am new to ThinkScript and have looked at various forums and sample scripts for if / then / else and "counters" but am missing something.

I just wanted to try something simple to test for and plot values of certain test variables to get my feet ***.

Questions:
1. How do you increase / decrease the value of a variable when certain criteria is met?
2. Is the "else" really required whether you have a condition to test or not?

Here is my simple / sample code:

def Test1_Value = 0;
def Diff_SlowK = SlowK - SlowK[1];

if (SlowK > SlowD) then { Test1_Value = Test1_Value + 1; } # The if/then/else works but it gives an error that Test1_Value already assigned
else { Test1_Value = Test1_Value - 1; }; # "Already assigned: Test1_Value at 20:27" for example

if (Diff_SlowK > 0) {Test1_Value = Test1_Value + 1;}; # This if statement does not work, gives the following error:
# Syntax error: An 'else' block expected at 23:1
# Syntax error: Semicolon expected at 23:1

if (Diff_SlowK > 0) then { Test1_Value = Test1_Value + 1; } # Requires the else command whether or not I use the "then"
else {}; # which seems odd that "else" is required but maybe I am missing something

plot Test1_Value;
.
.
.
plot Test2_Value;
plot Test3_Value;
plot Values = Test1_Value + Test2_Value + Test3_Value;
 
Last edited:
Solution
Def TestValue = if SlowK > SlowD then TestValue[1] + 1 else TestValue[1] - 1;

or

Def TestValue;
if SlowK > SlowD {
TestValue = TestValue[1] + 1
} else {
TestValue = TestValue[1] - 1
}

In the later segments, you would do Else TestValue = TestValue[1] which just carries on the prior value as storage basically.

Simple comparisons naturally resolve Boolean. ie A = X > Y; which requires no Else.

You do not need to, for example, do; A = If X > Y then yes else no;
Def TestValue = if SlowK > SlowD then TestValue[1] + 1 else TestValue[1] - 1;

or

Def TestValue;
if SlowK > SlowD {
TestValue = TestValue[1] + 1
} else {
TestValue = TestValue[1] - 1
}

In the later segments, you would do Else TestValue = TestValue[1] which just carries on the prior value as storage basically.

Simple comparisons naturally resolve Boolean. ie A = X > Y; which requires no Else.

You do not need to, for example, do; A = If X > Y then yes else no;
 
Solution

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

Thank you Joshua. The quick feedback is much appreciated.

To keep adding criteria and increasing / decreasing the Test Value, how are the following lines formatted?

This line worked and I like the way you consolidated / condensed it:
def Test1_Value = if SlowK > SlowD then Test1_Value[1] + 1 else Test1_Value[1] - 1;

As expected, I cannot define a variable again:
def Test1_Value = if Diff_SlowK > 0 then Test1_Value[1] + 1 else Test1_Value[1] - 1;

And these did not work:
if Diff_SlowK > 0 then Test1_Value[1] + 1 else Test1_Value[1] - 1;
if (Diff_SlowK > 0) then {Test1_Value[1] + 1;} else {Test1_Value[1] - 1;};
if Diff_SlowK > 0 then {Test1_Value[1] + 1;}
else {Test1_Value[1] - 1;};
if Diff_SlowK > 0 then Test1_Value[1] + 1
else Test1_Value[1] - 1;
if (Diff_SlowK > 0) then {Test1_Value[1] + 1;}
else {Test1_Value[1] - 1;};
if (Diff_SlowK > 0) then Test1_Value[1] + 1
else Test1_Value[1] - 1;
 
Thank you Joshua. The quick feedback is much appreciated.

To keep adding criteria and increasing / decreasing the Test Value, how are the following lines formatted?

This line worked and I like the way you consolidated / condensed it:
def Test1_Value = if SlowK > SlowD then Test1_Value[1] + 1 else Test1_Value[1] - 1;

As expected, I cannot define a variable again:
def Test1_Value = if Diff_SlowK > 0 then Test1_Value[1] + 1 else Test1_Value[1] - 1;

And these did not work:
if Diff_SlowK > 0 then Test1_Value[1] + 1 else Test1_Value[1] - 1;
if (Diff_SlowK > 0) then {Test1_Value[1] + 1;} else {Test1_Value[1] - 1;};
if Diff_SlowK > 0 then {Test1_Value[1] + 1;}
else {Test1_Value[1] - 1;};
if Diff_SlowK > 0 then Test1_Value[1] + 1
else Test1_Value[1] - 1;
if (Diff_SlowK > 0) then {Test1_Value[1] + 1;}
else {Test1_Value[1] - 1;};
if (Diff_SlowK > 0) then Test1_Value[1] + 1
else Test1_Value[1] - 1;

correct, you can not have 2 code lines that define the same variable name.
this will cause an error.
def var1 = 1 + close;
def var1 = 5 + close;

if a variable value Is to be determined by multiple conditions, then you could use if then or switch case. But it has to be all in one formula, or what i'll call a formula block. each instance of 'def var' , has to be unique, it can only appear once in the program.
you would define variables for all the possible conditions.
then after those code lines, use those variables in an if then to determine the value of Test1_Value.


here is one way. i'm keeping it simple, by using just numbers for possible values. but any variable/formula could be used.
Code:
def var1 = if cond1 then 1 else if cond2 then 2 else 3;

i tend to put each 'else' at the start of a new line, to make it easier to read. i didn't above because sometimes a carriage return is incorrectly interpretted by the program, when copying/pasting text, into the script editor, causing some code to be highlighted in red.

another way. define the variable first, then check conditions and assign a value.
Code:
def var1;
if cond1 then {
var1 = 1;
} else if con2 then {
var1 = 2;
} else {
var1 = 3;
}

another way, define multiple variables from 1 condition.
Code:
def var1;
def var2;
if cond1 then {
var1 = 1;
var2 = 66;
} else if con2 then {
var1 = 2;
var2 = 5;
} else {
var1 = 3;
var2 = 9;
}


ver1 of your code
Code:
def SlowK = ... Add your formula for this
def SlowD = .. Add your formula for this
def Diff_SlowK = ... Add your formula for this

def Test1_Value;
if SlowK > SlowD then {
Test1_Value = Test1_Value[1] + 1;
} else if SlowK < SlowD then {
Test1_Value = Test1_Value[1] - 1;
} else if Diff_SlowK > 0 then {
Test1_Value = Test1_Value[1] + 1;
} else if Diff_SlowK < 0 then {
Test1_Value = Test1_Value[1] - 1;
} else {
Test1_Value = Test1_Value[1];
}

ver2 of your code
Code:
def SlowK = ... Add your formula for this
def SlowD = .. Add your formula for this
def Diff_SlowK = ... Add your formula for this

def Test1_Value;
if (SlowK > SlowD or Diff_SlowK > 0) then {
Test1_Value = Test1_Value[1] + 1;
} else if (SlowK < SlowD or Diff_SlowK < 0) then {
Test1_Value = Test1_Value[1] - 1;
} else {
# if none of the conditions were triggered, then assign the value from previous bar
Test1_Value = Test1_Value[1];
}

some reference links
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/If

https://usethinkscript.com/resources/thinkscript-conditions-if-then-else-statement.12/

https://usethinkscript.com/threads/nested-if-then-syntax-in-thinkscript.2349/page-2#post-46472


https://jshingler.github.io/TOS-and....html#if-expressions-and-statements-explained

https://jshingler.github.io/TOS-and-Thinkscript-Snippet-Collection/TOS & Thinkscript Collection.html
hal_ifthen
 
def Test1_Value; if SlowK > SlowD then { Test1_Value = Test1_Value[1] + 1; } else if SlowK < SlowD then { Test1_Value = Test1_Value[1] - 1; } else if Diff_SlowK > 0 then { Test1_Value = Test1_Value[1] + 1; } else if Diff_SlowK < 0 then { Test1_Value = Test1_Value[1] - 1; } else { Test1_Value = Test1_Value[1]; }
Thank you halconguy. I appreciate the quick feedback as well!

I am on my way using a conditional test value for each step instead of incrementing by test. Took me a couple of hours to think through the changes to my script but it looks like it is now working as expected.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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