Array error

 

Hi everyone!

I'm trying to figure out how MQL4 arrays work and I stumbled upon a behavior I don't understand.

I want an array to hold some undetermined number of elements so I figure out that I should use SetIndexBuffer to let the platform handle it's size.

While I can see the elements printed in OnInit function, when I print them again in OnCalculate I see there are all zeroes.

What am I missing?

Thanks in advance!

#property indicator_chart_window
#property indicator_buffers 2

double nodes[];

int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0, nodes);
   SetIndexEmptyValue(0,0);

   nodes[0] = 200;
   nodes[1] = 195;
   nodes[2] = 190;
   nodes[3] = 185;
   nodes[4] = 180;

   print_array(nodes);

   return(INIT_SUCCEEDED);
  }

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   int limit;
   if(!prev_calculated)  
      limit=rates_total;   
   else
      limit=rates_total-prev_calculated; // limit==1 only at the 1st tick of a new bar

   if(limit==1)
     {
      Print("LIMIT==", limit);
      print_array(nodes); 
     }
   return(rates_total);
  }

void print_array(const double &ar[])  
  {
   for(int i=0; i<
10 ; ++i)
         Print(i, " ",ar[i]);
  }
 
double nodes[];

Your array has no size. You must resize it before updating elements.

Perhaps you should read the manual. ArrayResize
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

 

Thanks William,

I was guided by this:

"The function (ArrayResize) can be applied only to dynamic arrays. It should be noted that you cannot change the size of dynamic arrays assigned as indicator buffers by the SetIndexBuffer() function. For indicator buffers, all operations of resizing are performed by the runtime subsystem of the terminal."

But I'll get back to reading about SetIndexBuffer...

Dynamic Array Object - Data Types - Language Basics - MQL4 Reference
Dynamic Array Object - Data Types - Language Basics - MQL4 Reference
  • docs.mql4.com
Dynamic Array Object - Data Types - Language Basics - MQL4 Reference