Hands-On Guide: Adding Time Filters and Session Controls to Your MQL5 Expert Advisor
Market volatility changes drastically depending on the active trading session. Running an Expert Advisor during low-liquidity night sessions or right before major economic news releases can expose your account to erratic spreads and slippage. Adding a time filter to your MQL5 EA ensures your bot only executes trades during optimal market hours.
In this hands-on guide, we will implement session control logic using MQL5 time structures.
---------------------------------------------------
1. Why Time Filters Matter in Algorithmic Trading
Different trading sessions (such as London, New York, or Asian sessions) exhibit distinct price action characteristics. By restricting your EA to specific hours:
• You avoid low-liquidity periods where spreads widen significantly.
• You prevent your bot from taking entries during messy, range-bound overnight hours.
• You can program automatic Friday afternoon exits to avoid weekend gap risks.
---------------------------------------------------
2. Implementing Hour and Minute Filters in MQL5
Open your Expert Advisor in MetaEditor (F4) and add input parameters and a time-check function:
// --- Time Filter Inputs
input bool EnableTimeFilter = true; // Use Session Time Filter?
input int StartHour = 8; // Trading Start Hour (Server Time)
input int EndHour = 17; // Trading End Hour (Server Time)
bool CheckTimeSession()
{
if(!EnableTimeFilter) return true; // Bypass filter if disabled
MqlDateTime dt;
TimeToStruct(TimeCurrent(), dt); // Get current broker server time
// Check if the current hour falls within the allowed window
if(dt.hour >= StartHour && dt.hour < EndHour)
{
return true; // Inside allowed session
}
return false; // Outside allowed session
}
---------------------------------------------------
3. Integrating the Filter into OnTick()
To ensure your EA checks the time before evaluating any technical indicators or opening trades, place the function right at the top of your `OnTick()` loop:
void OnTick()
{
// Verify session time window before executing any logic
if(!CheckTimeSession())
{
return; // Exit tick function if outside trading hours
}
// --- Your existing Moving Average, Crossover, and Trade Logic goes here ---
}
---------------------------------------------------
4. Compiling & Testing Your Time-Filtered EA
1. Press F7 in MetaEditor to compile your code (ensure 0 errors).
2. Go to the Strategy Tester in MT5 (Ctrl + R).
3. Under the Inputs tab, adjust your `StartHour` and `EndHour` parameters and observe how trade executions align strictly with your specified hours during backtesting.
---------------------------------------------------
Final Thoughts
Adding session controls and time filters gives you granular control over when your automated strategies interact with the market. It is a simple yet powerful way to refine your algorithmic performance.
Stay tuned for our next guide as we march closer to our content targets!
Comments
Post a Comment