How to get the latest chart price in Expert Advisor

Hi everybody,

I am developing my first EA and maybe some of you could help me.

What I want to do is quite simple but I dont know how to write itin mql5.

I just want to get the latest price as quickly as possible (here every 16ms for example). This my current code :slight_smile:

input int shift=0; 
  void OnInit () {EventSetMillisecondTimer (16);} 
  void OnTimer () 
     datetime time  = iTime(Symbol(),PERIOD_M1,shift);
     double   open  = iOpen(Symbol(),PERIOD_M1,shift);
     double   high  = iHigh(Symbol(),PERIOD_M1,shift); 
     double   low   = iLow(Symbol(),PERIOD_M1,shift); 
     double   close   = iClose(Symbol(),PERIOD_M1,shift); 

     Print( //"Time: "  ,TimeToString(time,TIME_DATE|TIME_SECONDS),"\n", 
          "Heure bougie : "  ,time,"\n",
          "Open: "  ,open,"\n", 
          "High: "  ,high,"\n", 
          "Close: "  ,close,"\n",
          "Low: "   ,low);

But this code is always collecting data from EURUSD with H1 period, even if this chart is not displayed before running the code.

So how to change it to get data from Google (#GOOG) for example or another share on a minute basis (M1).

Thank you very much for your help !

Hello you have to change the


you can use


Depending on the position where the instrument is in market place.

Not true, it reads the data from the chart that your EA is dropped on. (current chart), and from M1 bars.
read about Symbol(), TimeSeries, and Copy…() functions.

to read other symbols, (other instruments), change the first parameter.
like this : iTime(“ANOTHER_NAME”, PERIOD_M1, …);

by the way, you probably don’t need to retrieve these data with such high frequency refresh rate. (every 16ms ???!!!)
many coders prefer new bar formation, as a analyze frequency. (e.g. each M1 bar).
and some coders prefer intra bar data analyze. (like each tick)
but I think your rate of data-receiving is not necessary. but you know better.

Thanks for helping !

It works, if I change the position I have another values from different instruments. But how to know which one I am dealing with ? and what is the 1 after position ?

And why it’s always the EURUSD chart displayed when I am running the code, how to get the appropriate chart displayed ?

Thanks !

yes OnTick is (always) better that OnTimer
these are functions that get called when something happens.
Ontick : when new tick is arrived
OnTimer : based on the time interval you set
OnTrade : when something related to trades happens

OnTImer introduces may problems usually.

OnTick seems good, it avoids making actions “for nothing” or missing variations as I did with time intervals.

But I can’t understand what is exactly behind a tick. I thought it was only when variations occur but there are also ticks when the market is closed, so how it’s possible ? Thanks for all.

there’s no OnTick event calling when market is closed.
if a price change happens, a tick is received and OnTick is executed.