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