Generate code in 3 minutes
Enter your idea and AI generates runnable code.
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.
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
);
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).");
}
}
exact = false (Default/Recommended):
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.exact = true:
-1 if a bar with that exact open time does not exist.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.iBarShift (with exact=false) will usually return 0 (the current bar), as it is the closest available bar to that future time.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.