StopFirst-Core/Include/StopFirst/Time.mqh
RUSLAN SLOBODIANIUK 6ba79702fd Make the header comments self-contained
They referred to project documents that are not part of this repository.
Comments only; PriceToPips is now documented as not rounding, which is what
it does.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-16 21:48:01 +03:00

112 lines
5 KiB
MQL5

//+------------------------------------------------------------------+
//| Time.mqh|
//| Copyright 2026, bandersoft |
//| https://www.mql5.com/en/users/fats |
//+------------------------------------------------------------------+
//| |
//| Timeframe and weekday names, trading-hour windows. |
//| |
//+------------------------------------------------------------------+
#ifndef STOPFIRST_TIME_MQH
#define STOPFIRST_TIME_MQH
#include "Defs.mqh"
#include "Sessions.mqh" // CurrentGmtHour
//+------------------------------------------------------------------+
//| Convert timeframe to string |
//| |
//| Returns: the short label - "M1", "M15", "H4", "MN1" and so on - |
//| or "Unknown" for a value outside the 21 standard |
//| timeframes. |
//+------------------------------------------------------------------+
string TimeframeToString(ENUM_TIMEFRAMES timeframe)
{
switch(timeframe)
{
case PERIOD_M1: return "M1";
case PERIOD_M2: return "M2";
case PERIOD_M3: return "M3";
case PERIOD_M4: return "M4";
case PERIOD_M5: return "M5";
case PERIOD_M6: return "M6";
case PERIOD_M10: return "M10";
case PERIOD_M12: return "M12";
case PERIOD_M15: return "M15";
case PERIOD_M20: return "M20";
case PERIOD_M30: return "M30";
case PERIOD_H1: return "H1";
case PERIOD_H2: return "H2";
case PERIOD_H3: return "H3";
case PERIOD_H4: return "H4";
case PERIOD_H6: return "H6";
case PERIOD_H8: return "H8";
case PERIOD_H12: return "H12";
case PERIOD_D1: return "D1";
case PERIOD_W1: return "W1";
case PERIOD_MN1: return "MN1";
default: return "Unknown";
}
}
//+------------------------------------------------------------------+
//| Get day of week as string |
//| |
//| Returns: "Sunday" through "Saturday" for 0..6, "Unknown" for any |
//| other value. |
//+------------------------------------------------------------------+
string DayOfWeekToString(int day)
{
//--- a switch needs no state and no first-call branch
switch(day)
{
case SUNDAY: return "Sunday";
case MONDAY: return "Monday";
case TUESDAY: return "Tuesday";
case WEDNESDAY: return "Wednesday";
case THURSDAY: return "Thursday";
case FRIDAY: return "Friday";
case SATURDAY: return "Saturday";
}
return "Unknown";
}
//+------------------------------------------------------------------+
//| Check if within trading hours |
//| |
//| Parameters: |
//| startHour, endHour - 0..23, end exclusive, wrapping allowed |
//| gmtOffset - hours to shift the GMT window by |
//| |
//| The window is in GMT, via Sessions.mqh, not raw broker server |
//| time: on a GMT+3 broker IsWithinTradingHours(8, 17) means |
//| 08:00-17:00 GMT, which is 11:00-20:00 on the broker clock. Do not |
//| pass the broker's own offset as gmtOffset to compensate. |
//| |
//| In the Strategy Tester the offset cannot be measured, so call |
//| SetGmtOffset() in OnInit or this silently assumes GMT+0. See |
//| Sessions.mqh. |
//| |
//| Returns: true if the current GMT hour, shifted by gmtOffset, |
//| falls in [startHour, endHour). Windows wrapping past |
//| midnight, such as 22 to 6, are handled. false for |
//| out-of-range hours. |
//+------------------------------------------------------------------+
bool IsWithinTradingHours(int startHour, int endHour, int gmtOffset = 0)
{
if(startHour < 0 || startHour > 23 || endHour < 0 || endHour > 23)
return false;
//--- the base is the broker clock converted to GMT,
//--- not the broker clock itself
int currentHour = ((CurrentGmtHour() + gmtOffset) % 24 + 24) % 24;
if(startHour <= endHour)
return (currentHour >= startHour && currentHour < endHour);
else
return (currentHour >= startHour || currentHour < endHour);
}
#endif // STOPFIRST_TIME_MQH
//+------------------------------------------------------------------+