Problem Description
Solution
Here is a robust and reusable MQL4 code snippet that restricts trading activity to a specific time window (08:00 to 17:00 Server Time).
MQL4 Time Restriction Snippet
This code includes user inputs to adjust the hours easily and a boolean check function to be placed inside your OnTick() event handler.
//+------------------------------------------------------------------+
//| Inputs |
//+------------------------------------------------------------------+
input int StartHour = 8; // Trading start hour (0-23)
input int EndHour = 17; // Trading end hour (0-23) - Trades stop at 17:00
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// 1. Check Time Filter before performing any logic
if(!IsTradingTime())
{
// Optional: Add logic here to close positions if outside hours
// or simply return to wait for the next tick.
return;
}
// 2. Place your strategy logic here
// Example:
// if(Signal == BUY) OrderSend(...)
}
//+------------------------------------------------------------------+
//| Helper Function: Check if current time is within allowed window |
//+------------------------------------------------------------------+
bool IsTradingTime()
{
// Get the current hour of the trade server
int currentHour = TimeHour(TimeCurrent());
// Logic for standard day trading (Start < End)
// Example: 8 to 17 means trading is allowed from 08:00:00 to 16:59:59
if(StartHour < EndHour)
{
if(currentHour >= StartHour && currentHour < EndHour)
return true;
}
// Logic for overnight trading (Start > End)
// Example: 22 to 8 means trading is allowed from 22:00 to 07:59
else if(StartHour > EndHour)
{
if(currentHour >= StartHour || currentHour < EndHour)
return true;
}
// Logic if Start == End (Trading allowed for the specific hour only)
else
{
if(currentHour == StartHour)
return true;
}
return false;
}
Implementation Details
TimeCurrent(): This function retrieves the Server Time, not your local computer time. This is crucial for backtesting and live trading consistency.TimeHour(): Extracts the hour (0–23) from the server timestamp.- Inclusive vs. Exclusive:
- The logic
currentHour < EndHourtreats the End Hour as the hard stop. - If
EndHouris set to 17 (5:00 PM), trading is allowed during the 16th hour (4:00 PM to 4:59 PM). As soon as the clock hits 17:00, the function returnsfalse.
- The logic
- Overnight Handling: The
else if(StartHour > EndHour)block ensures the code works correctly even if you decide to trade a session that crosses midnight (e.g., 22:00 to 02:00).
Q&A: MQL4 Time Management
Q: How do I close all open trades automatically at 5:00 PM?
A: You need to add a separate check. If IsTradingTime() returns false and OrdersTotal() > 0, you should loop through open orders and close them using OrderClose().
Q: Does this code work in the Strategy Tester?
A: Yes. TimeCurrent() simulates historical time correctly during backtesting, so the time filter will be respected exactly as it would in live trading.
Q: How do I handle minutes (e.g., start at 8:30 AM)?
A: You would need to expand the logic to check TimeMinute(TimeCurrent()). For example:
int currentMinute = TimeMinute(TimeCurrent());
if(currentHour == StartHour && currentMinute < StartMinute) return false;
Q: Why use Server Time instead of Local Time?
A: Forex markets operate on the broker's server time (often GMT+2 or GMT+3). Using TimeLocal() (your computer's time) can cause desynchronization, especially if you run the EA on a VPS in a different timezone.