iBars function return 0

 

Hi , I faced a problem I want to use iBars function and give symbol  name   parameter from an array to this function but it return 0. On the other hand ,when I use Symbol() it works without any problem . can you guys kindly tell me why?! I code in mql5 .

string symb=Symbol();
Print(symb,"  , ",iBars(symb,PERIOD_CURRENT) );
Print(Symbol(),"  , ",iBars(Symbol(),PERIOD_CURRENT) );



LOG:
        EURUSD, 0
        EURUSD, 100109
 
Gholamhossein Eslamizadeh:

Hi , I faced a problem I want to use iBars function and give symbol name parameter from an array to this function but it return 0. On the other hand ,when I use Symbol() it works without any problem . can you guys kindly tell me why?! I code in mql5 .


Both lines work out the same way.

2021.04.22 15:32:06.228 ibar (EURUSD,H1) EURUSD , 5000
2021.04.22 15:32:06.228 ibar (EURUSD,H1) EURUSD , 5000

 
Alexandr Bryzgalov:

Both lines work out the same way.

2021.04.22 15:32:06.228 ibar (EURUSD,H1) EURUSD , 5000
2021.04.22 15:32:06.228 ibar (EURUSD,H1) EURUSD , 5000

Hi,  @ Alexandr Bryzgalov , so is it possible my meta editor has a bug! Isn't it a universal opensource compiler?!

did you compile it as an indicator or script? I just figure it out that this happen only in indicator!

in script it works ok for me too!

please chek the attached file.
Files:
temp.zip  13 kb
 

I am getting the same problem today. Maybe because iBars is being called on 

int OnInit() { ... }

However, I can reproduce the error . It occurs only if I did not use the symbol for many days.

If I restart MT4, iBars works fine again for the same symbol that produced an error before. It is really a bug !

 
jocafi #: I am getting the same problem today. Maybe because iBars is being called on 

int OnInit() { ... }
Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:
  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
 
William Roeder #:
Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:
  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
Thanks William, very helpful. 
 

Thanks for the advice, William. To solve the problem, I had to implement a logic in the onTick() function to check if iBars delivered more than a minimal numbers of bars to calculate prices.

It works after some ticks.

In one of my MT4 instances I had to delete the history directory in order it worked correctly.