250 lines
8.8 KiB
Plaintext
250 lines
8.8 KiB
Plaintext
//+------------------------------------------------------------------+
|
|
//| TimeFilter.mqh |
|
|
//| Universal Buffer Reader EA v2.0 |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "Copyright 2025"
|
|
#property link ""
|
|
#property version "1.00"
|
|
#property strict
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| CTimeFilter - Validates day of week and trading session filters |
|
|
//+------------------------------------------------------------------+
|
|
class CTimeFilter
|
|
{
|
|
private:
|
|
bool m_enabled;
|
|
bool m_days_enabled[7]; // 0=Sunday, 1=Monday, ..., 6=Saturday
|
|
bool m_asian_enabled;
|
|
bool m_europe_enabled;
|
|
bool m_america_enabled;
|
|
|
|
// Logging
|
|
bool m_enable_debug;
|
|
|
|
public:
|
|
//+------------------------------------------------------------------+
|
|
//| Constructor |
|
|
//+------------------------------------------------------------------+
|
|
CTimeFilter()
|
|
{
|
|
m_enabled = false;
|
|
m_enable_debug = false;
|
|
|
|
// Default: Mon-Fri enabled
|
|
for(int i = 0; i < 7; i++)
|
|
{
|
|
m_days_enabled[i] = (i >= 1 && i <= 5);
|
|
}
|
|
|
|
m_asian_enabled = true;
|
|
m_europe_enabled = true;
|
|
m_america_enabled = true;
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Destructor |
|
|
//+------------------------------------------------------------------+
|
|
~CTimeFilter()
|
|
{
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Set parameters |
|
|
//+------------------------------------------------------------------+
|
|
void SetParameters(
|
|
bool enabled,
|
|
bool mon, bool tue, bool wed, bool thu, bool fri, bool sat, bool sun,
|
|
bool asian, bool europe, bool america,
|
|
bool enable_debug = false
|
|
)
|
|
{
|
|
m_enabled = enabled;
|
|
m_days_enabled[1] = mon; // Monday
|
|
m_days_enabled[2] = tue; // Tuesday
|
|
m_days_enabled[3] = wed; // Wednesday
|
|
m_days_enabled[4] = thu; // Thursday
|
|
m_days_enabled[5] = fri; // Friday
|
|
m_days_enabled[6] = sat; // Saturday
|
|
m_days_enabled[0] = sun; // Sunday
|
|
|
|
m_asian_enabled = asian;
|
|
m_europe_enabled = europe;
|
|
m_america_enabled = america;
|
|
m_enable_debug = enable_debug;
|
|
|
|
if(m_enable_debug)
|
|
{
|
|
Print("[TimeFilter] Parameters set - Enabled: ", m_enabled);
|
|
Print("[TimeFilter] Days: Mon=", mon, " Tue=", tue, " Wed=", wed,
|
|
" Thu=", thu, " Fri=", fri, " Sat=", sat, " Sun=", sun);
|
|
Print("[TimeFilter] Sessions: Asian=", asian, " Europe=", europe, " America=", america);
|
|
}
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Set debug mode |
|
|
//+------------------------------------------------------------------+
|
|
void SetDebugMode(bool enable_debug)
|
|
{
|
|
m_enable_debug = enable_debug;
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Check if current time is allowed for trading |
|
|
//+------------------------------------------------------------------+
|
|
bool IsTimeAllowed()
|
|
{
|
|
if(!m_enabled)
|
|
{
|
|
if(m_enable_debug)
|
|
{
|
|
Print("[TimeFilter] Time filtering disabled - Trading allowed");
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool day_allowed = IsDayOfWeekAllowed();
|
|
bool session_allowed = IsSessionTimeAllowed();
|
|
|
|
bool result = (day_allowed && session_allowed);
|
|
|
|
if(m_enable_debug)
|
|
{
|
|
Print("[TimeFilter] Time check - Day allowed: ", day_allowed,
|
|
", Session allowed: ", session_allowed,
|
|
", Result: ", (result ? "ALLOWED" : "BLOCKED"));
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Get current day of week name |
|
|
//+------------------------------------------------------------------+
|
|
string GetCurrentDayOfWeek()
|
|
{
|
|
MqlDateTime dt;
|
|
TimeToStruct(TimeGMT(), dt);
|
|
|
|
string day_names[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
|
|
return day_names[dt.day_of_week];
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Get current session name |
|
|
//+------------------------------------------------------------------+
|
|
string GetCurrentSession()
|
|
{
|
|
MqlDateTime dt;
|
|
TimeToStruct(TimeGMT(), dt);
|
|
int hour = dt.hour;
|
|
|
|
if(IsAsianSession(hour)) return "Asian";
|
|
if(IsEuropeSession(hour)) return "Europe";
|
|
if(IsAmericaSession(hour)) return "America";
|
|
|
|
return "Off-hours";
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Get current GMT time as string |
|
|
//+------------------------------------------------------------------+
|
|
string GetCurrentGMTTime()
|
|
{
|
|
MqlDateTime dt;
|
|
TimeToStruct(TimeGMT(), dt);
|
|
|
|
return StringFormat("%02d:%02d:%02d GMT", dt.hour, dt.min, dt.sec);
|
|
}
|
|
|
|
private:
|
|
//+------------------------------------------------------------------+
|
|
//| Check if current day of week is allowed |
|
|
//+------------------------------------------------------------------+
|
|
bool IsDayOfWeekAllowed()
|
|
{
|
|
MqlDateTime dt;
|
|
TimeToStruct(TimeGMT(), dt);
|
|
|
|
string day_names[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
|
|
bool allowed = m_days_enabled[dt.day_of_week];
|
|
|
|
if(m_enable_debug)
|
|
{
|
|
Print("[TimeFilter] Day of week: ", day_names[dt.day_of_week],
|
|
" (", dt.day_of_week, ") - ", (allowed ? "ALLOWED" : "BLOCKED"));
|
|
}
|
|
|
|
return allowed;
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Check if current session is allowed |
|
|
//+------------------------------------------------------------------+
|
|
bool IsSessionTimeAllowed()
|
|
{
|
|
MqlDateTime dt;
|
|
TimeToStruct(TimeGMT(), dt);
|
|
int hour = dt.hour;
|
|
|
|
bool in_asian = IsAsianSession(hour);
|
|
bool in_europe = IsEuropeSession(hour);
|
|
bool in_america = IsAmericaSession(hour);
|
|
|
|
bool allowed = false;
|
|
string active_session = "None";
|
|
|
|
if(in_asian && m_asian_enabled)
|
|
{
|
|
allowed = true;
|
|
active_session = "Asian";
|
|
}
|
|
else if(in_europe && m_europe_enabled)
|
|
{
|
|
allowed = true;
|
|
active_session = "Europe";
|
|
}
|
|
else if(in_america && m_america_enabled)
|
|
{
|
|
allowed = true;
|
|
active_session = "America";
|
|
}
|
|
|
|
if(m_enable_debug)
|
|
{
|
|
Print("[TimeFilter] Current time: ", dt.hour, ":00 GMT");
|
|
Print("[TimeFilter] In Asian session (00-08): ", in_asian, " - Enabled: ", m_asian_enabled);
|
|
Print("[TimeFilter] In Europe session (07-16): ", in_europe, " - Enabled: ", m_europe_enabled);
|
|
Print("[TimeFilter] In America session (13-22): ", in_america, " - Enabled: ", m_america_enabled);
|
|
Print("[TimeFilter] Active session: ", active_session, " - ", (allowed ? "ALLOWED" : "BLOCKED"));
|
|
}
|
|
|
|
return allowed;
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Check if Asian session (00:00 - 08:00 GMT) |
|
|
//+------------------------------------------------------------------+
|
|
bool IsAsianSession(int hour)
|
|
{
|
|
return (hour >= 0 && hour < 8);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Check if Europe session (07:00 - 16:00 GMT) |
|
|
//+------------------------------------------------------------------+
|
|
bool IsEuropeSession(int hour)
|
|
{
|
|
return (hour >= 7 && hour < 16);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Check if America session (13:00 - 22:00 GMT) |
|
|
//+------------------------------------------------------------------+
|
|
bool IsAmericaSession(int hour)
|
|
{
|
|
return (hour >= 13 && hour < 22);
|
|
}
|
|
};
|
|
//+------------------------------------------------------------------+ |