How does one use a counter variable in ThinkScript?


Category:
0
0

My example is
def VAOPerigee= if (AWESOMEOSCILLATOR().AO[0] > AWESOMEOSCILLATOR().AO[1])  && (AWESOMEOSCILLATOR().AO[1]<=AWESOMEOSCILLATOR().AO[2]) && (AWESOMEOSCILLATOR().AO[3] >AWESOMEOSCILLATOR().AO[2]) && AWESOMEOSCILLATOR().AO[3]<0 then yes else no;
( i.e when AO or any other oscillator valleys and comes up)

if VAOPerigee then rec VCtr=VCtr[1]+1;
if VCtr==3 then LongSignal=yes && VCtr=0;
( How does one get around the counter  accumulation variable  and the necessary reset used in most C-Based languages?)

Can you do this in Thinkscript?  How? write the last 2 lines for me anyone out there.

Marked as spam
Posted by (Questions: 13, Answers: 18)
Asked on January 11, 2018 10:34 pm
7086 views
0
Private answer

We have several examples of this scattered throughout the site. Some are found in our free video tutorials and others are found right here in the Q&A forum. However they are difficult to find if that is the exact term you are searching for.

So here is how to create a counter, based on the true/false variable from your code VAOPerigee.

rec counter = if VAOPerigee then 1 else if counter[1] == 1 or counter[1] < 3 then counter[1] + 1 else 0;

The biggest concept to grasp when moving from any other programing language to a Trading platform is to realize the platform runs a loop. It loops through every bar of the chart, every time a price is updated or the chart is refreshed. For each iteration of that loop (of which you have no control) the platform will read and execute each line of your code.

So that single line of code uses recursion. At each candle on the chart, it checks if VAOPerigee is true. If true, it resets the value of counter to 1. If false, it first checks if the previous candle value of counter is less than 3 (but NOT equal zero). If so, it increments the value of counter by one. Otherwise it sets counter to zero.

Now we create your long signal

def LongSignal = counter == 3;

And we’re done.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on January 12, 2018 8:56 am
0
Private answer

So the reason we do not   code it

rec counter=if VAOPergee then 1 else if counter [1]< 3 then counter[1]+1 else 0;

or

rec counter=if   VAOPerigee then counter[1] +1;

is because counter array is not initialized to 0 like in a normal language, correct? and it can not be initialized since you can not separate the first iteration like other languages.

You are saying counter[1] or even counter[0] could be any number, even a non-integer number or at  least a negative integer or a positive # greater than 3 .   That is why we can not do it like this which is the common way I believe done in NinjaScript or MTL4  or etc . We must set these 2 elements of the the array named counter[1]   & counter[0],  and then we can accumulate.

This code does it marvelously.

if you could do this in Thinkscript, which you can not do.

declare first iteration;(does not exist)

def counter=0;def counter[1]=0;   or def  array counter[] =0;

counter=counter+1; or rec counter=if VAOPerigee then counter[1] +1;  normal code for accumulating would work.

 

 

 

 

 

 

 

Marked as spam
Posted by (Questions: 13, Answers: 18)
Answered on January 15, 2018 12:51 pm
0

There is a way to give a variable an initial value. I only use it when necessary and I wanted to keep this example tight and compact. The way we set an initial value of a variable is through the use of a function called CompoundValue(), and you can read all about that here: http://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Others/CompoundValue.html

But before you go trying to incorporate that into the solution I provided, it is important you understand that on the first candle of the chart, the variable is assigned an initial value of zero. This happens as a result of the If-then-else statement.

rec counter = if VAOPerigee then 1 else if counter[1] == 1 or counter[1] < 3 then counter[1] + 1 else 0; # copied from my solution to your question.

The beauty of this, is that you have everything you need in a single line of code. Sort of like Python, very efficient. Although it may take more effort to read until you get used to it. So let me restructure that single line into a nested if-then-else statement so you can see this in a more C-like structure. (I'll post that as a new answer to this topic rather than here in the comments section of your answer)

Trust me, very soon this will all start to fall into place. I understand what it's like trying to write in a scripting language when you have a background of Java or C++. But Thinkorswim is just a scripting language. So consider the restrictions one might experience trying to right in JavaScript, as opposed to Java SE. NinjaTrader uses full C++, while Thinkorswim uses a scripting language.

( at January 15, 2018 1:32 pm)
0

Another note. I think I need to add more emphasis to this concept.
From my original answer:
“The biggest concept to grasp when moving from any other programing language to a Trading platform is to realize the platform runs a loop. It loops through every bar of the chart, every time a price is updated or the chart is refreshed. For each iteration of that loop (of which you have no control) the platform will read and execute each line of your code.”

Which means the EVERY variable in the script is an array. We don’t create array’s in Thinkscript. Everything is already an array. And we reference values in those arrays the same way we reference values from the chart data (O/H/L/C and Volume).

Ok? So every ‘def’ and every ‘rec’ is an array, **period. If you want constants within a script, we use an ‘input’ or we create an **enumerated variable. Read more about that here: http://toslc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/def.html

( at January 15, 2018 1:59 pm)
0
Private answer

This is another way to write it. This method uses nested if-then-else statements to accomplish the exact same thing as:

rec counter = if VAOPerigee then 1 else if counter[1] == 1 or counter[1] < 3 then counter[1] + 1 else 0;

It may be easier to read for those who are used to a C-based programing language.

rec counter;
if (VAOPerigee == yes) {
# we have a signal, set counter to 1
counter = 1;
} else {
if (counter[1] == 1 or counter[1] < 3) {
# previous value of counter indicates a signal has occurred
# increment counter by one
counter = counter[1] + 1;
} else {
# no signal, and counter[1] is not between 1 and 3
# this is the initialized value
counter = 0;
}
}

I’ve also included a screenshot because when I save this answer it may not retain the indentations, making it difficult to read the nested if statement above.

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on January 15, 2018 1:50 pm