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;
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: