Uninitialized array, was: How NW array assigned?

I am little confused on how NW variable is assigned value ??
In following code value assigned by this NW[0] = 0;
But when i trace NW[1] .... NW[n], i found some value;
I am not able to find source of these values.

Is it special variable in AFL ??


GraphXSpace = 5;
k =  Optimize("K",Param("K",1,0.25,5,0.25),0.25,5,0.25);
Per= Optimize("atr",Param("atr",4,3,20,1),3,20,1);
HaOpen = AMA( Ref( HaClose,  -1 ),  0.5 );
HaHigh = Max( H,  Max( HaClose,  HaOpen ) );
HaLow = Min( L,  Min( HaClose,  HaOpen ) );
PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "" + Name(), colorBlack, styleCandle | styleNoLabel );


rfsctor = WMA(H-L, Per);

revers = k * rfsctor;

Trend = 1;  
NW[0] = 0;  

for(i = 1; i < BarCount; i++)
 if(Trend[i-1] == 1)                
  if(j[i] < NW[i-1])                 
   Trend[i] = -1;                   
   NW[i] = j[i] + Revers[i];        
   Trend[i] = 1;
   if((j[i] - Revers[i]) > NW[i-1])
    NW[i] = j[i] - Revers[i]; 
    NW[i] = NW[i-1];
 if(Trend[i-1] == -1)               
  if(j[i] > NW[i-1])                
   Trend[i] = 1;                    
   NW[i] = j[i] - Revers[i];        
   Trend[i] = -1;
   if((j[i] + Revers[i]) < NW[i-1]) 
    NW[i] = j[i] + Revers[i]; 
    NW[i] = NW[i-1];


Plot(NW, "", IIf(Trend == 1, 27, 4), 4);
Shrt=Flip(Sell,Buy );
if( Status("action") == actionIndicator ) 
Title = EncodeColor(colorWhite)+ "NICK MA Swing System" + " - " +  Name() + " - " + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorWhite) +
 "  - " + Date() +" - "+"\n" +EncodeColor(colorYellow) +"Op-"+O+"  "+"Hi-"+H+"  "+"Lo-"+L+"  "+
"Cl-"+C+"  "+ "Vol= "+ WriteVal(V)+"\n"+ 
WriteIf (Buy , " GO LONG / Reverse Signal at "+C+"  ","")+
WriteIf (Sell , " EXIT LONG / Reverse Signal at "+C+"  ","")+"\n"+EncodeColor(colorWhite)+
WriteIf(Sell , "Total Profit/Loss for the Last Trade Rs."+(C-BuyPrice)+"","")+
WriteIf(Buy  , "Total Profit/Loss for the Last trade Rs."+(SellPrice-C)+"","")+
WriteIf(Long AND NOT Buy, "Trade : Long - Entry price Rs."+(BuyPrice),"")+
WriteIf(shrt AND NOT Sell, "Trade : Short - Entry price Rs."+(SellPrice),"")+"\n"+ 
WriteIf(Long AND NOT Buy, "Current Profit/Loss Rs."+(C-BuyPrice)+"","")+
WriteIf(shrt AND NOT Sell, "Current Profit/Loss Rs."+(SellPrice-C)+"",""));
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);                      
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);                      
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

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. 

Thank you for the clarification.

Hello Tomasz,
In the above comment, you said:
NW = 0; // this initializes variable correctly, i.e. ALL bars, not one.

  1. At this point is MW a scalar or an array? A scalar I would assume.
  2. Can I make it an array by assigning a value to one of its elements? I.e. MW[0] = 0;
  3. If the answer to #2 is yes, must I assign a value to MW[0] before I assign a value to MW[x] where x is > 0?

@rlewkov in your yesterday's thread:

I have provided you with 3 links to Tomasz's articles/replies in which you can easily find precise answers to these three questions. Did you even bother reading them? You seem not to. You didn't reply/respond in any way and now you are asking the same questions again. Please show some respect and don't make Tomasz repeat the same information over and over. I have selected those replies for you. You just need to read them.

Besides you can easily check if a variable is a scalar or an array using typeof operator:


or the debugger:



Hello Milosz,
Thanks for your reply. I did read each of the 3 posts that you sent and wasn't 100% sure of exactly how to make a variable an array in the case where I would not assign one of the reserved variables (O,H,Buy,Sell, etc) to it. After reading the posts you sent I assume it should be done as follows:

myVar = 0; // Scalar
myVar[0] = 0;  // Now array after assigning a value to a specific element

It appears from the examples that it's a best practice to first declare a variable as a scalar and then initialize an element and it seems like it's a good practice initialize element [0]. Sometimes I have to ask questions in the order that makes sense to me to make sure I understand what I'm reading.


@rlewkov I find that in many cases AmiBroker lets us get away without “declaring” variables and it can figure out if we intended it to be a string, a scalar or an array. I am not a programmer but it is my impression that this is unlike most computer languages, where the declaration is mandatory.

Also this is not to suggest that users with programming backgrounds don't follow "good practice", of course that is probably a good idea.

One last suggestion for Robert, often the forum users can best guide new users if you post some code or details about what you are trying to accomplish. Often the "AmiBroker way" of solving the problem is not intuitive to new users with other programming language familiarity. Specific coding questions without context, may not get the most complete or appropriate answers.

Welcome to the forum and good luck!


@rlewkov you should't really bother whether initially your variable is a scalar or an array, because AmiBroker takes care of that for you. If you initialize your variable like this --> myVar = 0; it is a scalar, but when there is such need, it will be automatically transformed into an array. So, yo don't have to worry about that. It is all described in Tomasz's article.

Just initialize your variable like this: --> myVar = 0; Don't force AB to convert it into an array (if not necessary) by writting to a single element of an array - like this: myVar[0] = 0; You will only make AmiBroker allocate much more memory than needed!

1 Like

You do NOT need to declare variables in AFL. It works like in Javascript. You just write

x = 5; // declaration is not necessary, assignment sets the type

Whatever you assign to variable dictates its type. And variable type can change over the course of the program. It is all done transparently and you don't have to worry about anything:

x = 5; // x is now a scalar (number)
x = "text"; // x is now a string

In AFL scalar numbers can be used anywhere where array is needed/expected. http://www.amibroker.com/guide/h_understandafl.html

If you want to initialize variable just assign a value:

x = 0; // initialize to zero

As written above after such assignment x can be used wherever scalar OR array is expected.

1 Like