Page 1 of 1

How does one create a counter

Posted: Mon Jan 18, 2021 5:44 am
by bsegura
Hello:

I am having trouble trying to make a simple counter.

x = x[1] + 1;

in formula script I am using:
x : ref(x,1) + 1

I get an error 0165 Use of unassigned local variable 'X'

Does anyone have the correct method of getting a variable to increment per each loop

Re: How does one create a counter

Posted: Tue Jan 19, 2021 6:41 am
by Chris White
Hi,

The Scripting language is an array language rather than a procedural language and so it does not have constructs such as X = X + 1 (well, actually you can write something like that but it has a different meaning, see below).

You can create an array with incrementing numbers using:
Count(C, 0);

If you add this to a chart you will see a line increasing in value by 1 each bar from the first bar of data to the last bar of data.

In the scripting language if you tried:
X: C + 1;

You would be taking the array containing Close prices and adding 1 to every element.