🚀 AI One-Click Generation of mql4 Strategy Code

Mql4 Tutorial: How to Accurately Find a Bar's Index Using a Specific Timestamp with iBarShift()

mql4 | Published on: 2025/12/9
The following content is generated by EasyQuant .

Problem Description

How do I find the index of a bar that corresponds to a specific date and time in the past?

Solution

To find the index (shift) of a bar corresponding to a specific date and time in MQL4, you should use the built-in function iBarShift.

This function searches the chart's history for the bar that covers the specified time.

Syntax

int iBarShift(
   string symbol,           // Symbol (e.g., "EURUSD" or NULL for current)
   int timeframe,           // Timeframe (e.g., PERIOD_H1 or 0 for current)
   datetime time,           // The specific date and time to find
   bool exact = false       // Mode: false = nearest bar, true = exact match only
);

Implementation Example

Here is a complete script demonstrating how to find the bar index for a specific time.

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   // 1. Define the target time you are looking for
   // Example: October 25, 2023, at 12:00 server time
   datetime targetTime = D'2023.10.25 12:00';
   
   // 2. Call iBarShift
   // We use Symbol() for the current chart and Period() for the current timeframe.
   // We set 'exact' to false to find the nearest bar if the exact second doesn't exist.
   int shift = iBarShift(Symbol(), Period(), targetTime, false);
   
   // 3. Check the result
   if(shift != -1)
   {
      // Get the actual open time of the found bar to verify
      datetime foundTime = iTime(Symbol(), Period(), shift);
      
      Print("Target Time: ", TimeToString(targetTime));
      Print("Found Bar Index (Shift): ", shift);
      Print("Actual Bar Open Time: ", TimeToString(foundTime));
      
      // Example: Get the Close price of that specific bar
      double closePrice = iClose(Symbol(), Period(), shift);
      Print("Close Price at that time: ", closePrice);
   }
   else
   {
      Print("Bar not found (History might not be loaded or time is in the future).");
   }
}

Key Parameters Explained

  1. exact = false (Default/Recommended):

    • If the exact time (e.g., 12:00:00) does not exist (perhaps due to a gap in quotes or a weekend), the function returns the index of the nearest bar that started before or at that time.
    • This is usually the safest option for general trading logic.
  2. exact = true:

    • The function will return -1 if a bar with that exact open time does not exist.
    • Use this if your strategy relies on the specific existence of a candle (e.g., checking if a specific H1 candle exists).

Important Considerations

  • History Availability: iBarShift only works on data currently loaded in the terminal. If you request a date from 10 years ago and that data isn't downloaded, it may return -1 or the oldest available bar index.
  • Future Time: If you pass a time in the future, iBarShift (with exact=false) will usually return 0 (the current bar), as it is the closest available bar to that future time.

Q&A

Q: What happens if iBarShift returns -1?
A: This indicates the function failed to find the bar. This usually happens if you set exact=true and the specific time gap exists, or if the requested time is outside the available history range.

Q: Can I use iBarShift on a different symbol than the one the EA is running on?
A: Yes. Simply replace Symbol() with the specific string name, for example: iBarShift("GBPUSD", PERIOD_H1, targetTime).

Q: How do I get the Close price once I have the index?
A: Once you have the index (let's call it shift), you can use standard access functions like iClose(Symbol(), Period(), shift), iHigh(...), or simply the arrays Close[shift], High[shift] if working on the current chart.