How do I declare a variable in an AFL as an array?
If I make the following declaration "i = 0;" then i is a scalar. Certain variables such as Buy, Sell are by definition arrays.
I want to iterate through the bars using for (i=0; i < BarCount; ++i) , do a calculation for each bar, and store the value for each bar for use in later processing ... therefore an array.
Thanks
Milosz
August 13, 2018, 10:15pm
2
Read here:
Since the subject of data types surfaced in this thread: Send orders to IB on next bar (Intraday)
Here are some comments regarding data types in AFL.
While some users who familiarized a bit with array handling in AFL may think that everything is an array in AFL, that is not the case.
The data type depends on value assigned to variable. If you assign scalar (number) the variable type will be scalar (floating point number, 4 bytes long).
So if you assign a number to variable like this:
var1 =…
https://www.amibroker.com/guide/h_understandafl.html
and:
Your code is incorrect. You should not write to single element of uninitialized array.
NW[0] = 0; // wrong, if array is never initialized before
If you do this, you will get an array with first element (index 0) initialized but all the other will hold just random "garbage" (uninitialized RAM content). Of course it does not matter much if you simply ignore/ not use uninitialized content.
Correct usage is to write
NW = 0; // this initializes variable correctly, i.e. ALL bars, not one.
// Oh My GOD !@#*&@#
if (typeof(MyVariable) == "undefined") {myVariable = 0;}
Don’t do that!
The variables should be always initialized in the formula file where they are used.
Initialization writes initial value.
Just do this:
myVariable = 0;
at the top of your code. UNCONDITIONALLY.
The other file may write to already initialized variable again, but the concept of checking for “undefined” is completely SICK.
Using variable without writing initial value to it is equivalent to saying “oh …
2 Likes