Simple EA - don't understand what is wrong though...

 

Hi... am a new learner of MQL4. I just wanted an alert on the number of bars in a chart. Please refer the code below. The problem is that after execution of the special function init, when start() starts, the global variable "count" shows a value of 0. What am I doing wrong here?


int TF=1;
int count;

int init()
{
//----

int count = iBars(NULL,TF);

Alert("Bar count for symbol ",Symbol()," is ",count);
//----
return;
}


//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----

if (count != iBars(NULL,TF))
{
Alert("Bar count for symbol ",Symbol()," in Time frame ",TF, " is ",count);
count=iBars(NULL,TF);
}


//----
return;
}
//+------------------------------------------------------------------+
 

you declare count 2 times

in global before init and in init.

though the "count" in init is a local variable only avaiable in init and cannot be accessed in start

simply remove the "int" before count in init and the code should work (if i havent overseen something else) ;-)

 
meikel:

you declare count 2 times

in global before init and in init.

though the "count" in init is a local variable only avaiable in init and cannot be accessed in start

Thank you... has helped explain a lot!

 

int count = iBars(NULL,TF);


//-----

count=iBars(NUll,TF);