MQL4 timeseries access

MQL4 bar data functions and shift-safe access

Use iOpen, iHigh, iLow, iClose, iTime, and iVolume with one symbol, timeframe, and bar shift. Shift 0 is the current bar. Use iBarShift to map a time to a bar, and check history availability because data-access functions can return zero when local history is empty.

Open the Coding Agent, then choose MQL4 from the language menu.

BarDataLookup.mq4.mq4
#property strict
#property script_show_inputs

input datetime TargetTime = D'2025.01.02 12:00';

void OnStart()
  {
   int shift = iBarShift(NULL, PERIOD_H1, TargetTime, true);
   if(shift < 0)
     {
      Print("No exact H1 bar for ", TimeToString(TargetTime));
      return;
     }

   datetime bar_time = iTime(NULL, PERIOD_H1, shift);
   double open_price = iOpen(NULL, PERIOD_H1, shift);
   double close_price = iClose(NULL, PERIOD_H1, shift);
   long tick_volume = iVolume(NULL, PERIOD_H1, shift);

   if(bar_time == 0)
     {
      Print("Bar history is unavailable.");
      return;
     }

   PrintFormat("%s open=%s close=%s ticks=%d",
               TimeToString(bar_time),
               DoubleToString(open_price, Digits),
               DoubleToString(close_price, Digits),
               tick_volume);
  }
  • Shift 0 is still forming
  • Keep symbol and timeframe aligned
  • iBarShift maps time to a bar
  • Zero can mean missing history

A bar is identified by three coordinates

Symbol, timeframe, and shift together identify the requested bar. NULL selects the current chart symbol, while PERIOD_CURRENT or zero selects the current chart timeframe. An explicit timeframe makes cross-timeframe logic easier to review.

Shift 0 changes as ticks arrive. Use shift 1 when a rule must wait for a completed bar, and document any intentional intrabar behavior.

FunctionReturned valueMissing-history signal
iTimeBar opening time0
iOpen / iHigh / iLow / iClosePrice0
iVolumeTick volume0
iBarsAvailable bar count0

iBarShift can require an exact bar

With exact set to true, iBarShift returns minus one when the requested time falls in a history gap. With exact false, it returns the nearest bar according to the documented lookup behavior.

The returned shift is meaningful only for the same symbol and timeframe used in the lookup. Use it immediately or remap after a history refresh if the application stores time as its stable identifier.

  • Use exact true when nearest-bar substitution would corrupt the rule
  • Check iBars before requesting a long lookback
  • Read all OHLC values from the same bar coordinates
  • Use time, not a cached shift, as the durable identity across history changes

Volume in MT4 is commonly tick volume

The predefined Volume series and iVolume expose tick volume for a bar in common MT4 feeds. It counts price updates and is not automatically centralized exchange trade volume.

Indicators such as VWAP must state which volume field they use. A calculation can be syntactically correct while its data interpretation does not match the intended market definition.

Review the complete example

The script requests an exact hourly bar for a chosen time, rejects a missing shift, and reads all values from the same symbol, timeframe, and shift.

The example reads history only. Confirm the broker feed, timezone, bar completeness, and volume definition before using the values in a signal.

BarDataLookup.mq4.mq4
#property strict
#property script_show_inputs

input datetime TargetTime = D'2025.01.02 12:00';

void OnStart()
  {
   int shift = iBarShift(NULL, PERIOD_H1, TargetTime, true);
   if(shift < 0)
     {
      Print("No exact H1 bar for ", TimeToString(TargetTime));
      return;
     }

   datetime bar_time = iTime(NULL, PERIOD_H1, shift);
   double open_price = iOpen(NULL, PERIOD_H1, shift);
   double close_price = iClose(NULL, PERIOD_H1, shift);
   long tick_volume = iVolume(NULL, PERIOD_H1, shift);

   if(bar_time == 0)
     {
      Print("Bar history is unavailable.");
      return;
     }

   PrintFormat("%s open=%s close=%s ticks=%d",
               TimeToString(bar_time),
               DoubleToString(open_price, Digits),
               DoubleToString(close_price, Digits),
               tick_volume);
  }
Where Pineify fits

Move from the rule to editable MQL4 source

Pineify can generate or revise one self-contained .mq4 file with MQL4-specific reference context. Its independent static checker returns MetaEditor diagnostics so you can repair source issues before testing in your own MT4 environment.

Open MQL4 Coding Agent

Static diagnostics do not prove runtime behavior, backtest results, broker compatibility, or live-trading safety.

Pineify MQL4 Coding Agent with MQL4 selected and an editable MetaTrader 4 code artifact

A practical workflow

  1. 1

    Fix the bar coordinates

    Choose one symbol, timeframe, and current or closed-bar policy.

  2. 2

    Check available history

    Use iBars and return-value checks before reading the series.

  3. 3

    Map time when needed

    Use iBarShift with an explicit exact or nearest-bar policy.

  4. 4

    Read a consistent record

    Use the same coordinates for time, OHLC, and volume.

Frequently asked questions

What is bar shift 0 in MQL4?

Shift 0 is the current bar, whose price and volume can keep changing until it closes.

How do I get the previous bar close?

Call iClose for shift 1 on the required symbol and timeframe.

What does iBarShift do?

It returns the shift of the bar corresponding to a specified time for one symbol and timeframe.

Why does iClose return zero?

A zero can indicate that local history for the requested series is not loaded, so check availability and errors rather than assuming it is a market price.

Is iVolume real exchange volume?

In common MT4 forex feeds it is tick volume, not centralized exchange trade volume.

Last verified against the linked official references on . Compile and test the final source in the MetaTrader 4 build, broker, symbol, and account environment you plan to use.

Continue with one reviewable MQL4 file

Define the requirement, inspect the generated source, repair diagnostics, and test the behavior in your own MT4 environment.

Open Coding Agent