Watch how to download trading robots for free
Find us on Telegram!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Views:
8886
Rating:
(15)
Published:
Updated:
TimeUtils.mqh (122.83 KB) view
basic.mq5 (1.18 KB) view
advanced.mq5 (23.34 KB) view
perf.mq5 (3.28 KB) view
validate.mq5 (4.69 KB) view
MQL5 Freelance Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

Why Use TimeUtils?

MQL5 provides basic date and time functions such as TimeToStruct() and StructToTime() . While sufficient for simple tasks, many common calendar operations require repetitive, verbose, and error-prone code. TimeUtils combines high performance with a rich set of calendar utilities, allowing you to write cleaner, more expressive MQL5 code while avoiding common date and time pitfalls.

Performance and Core Engine

  • Optimized replacements for TimeToStruct() and StructToTime() .
  • As of v2.00+, TimeUtils features advanced March-based conversion routines derived from the Neri–Schneider calendar algorithm.
  • TimeToStructFast() / StructToTimeFast(): Up to 10x–20x faster than native TimeToStruct and StructToTime.
  • Instant extraction of date/time components ( Year() , Day() , Hour() , DayOfWeek() ) without forcing you to declare or fill MqlDateTime structure.
  • Significant speed up for large backtests, optimization passes, and high-frequency processing.

Advanced Functionality

  • Add or subtract seconds, minutes, hours, days, business days, weeks, months, quarters, and years.
  • Find the start or end of a minute, day, week, month, quarter, or year.
  • Finding the next or previous specific weekday
  • Calculate calendar and elapsed differences between dates.
  • Work with business days, weekends, week numbers, quarters, leap years, and more.

Flexible Formatting

  • Format dates using customizable format tokens (like "DDD, YYYY.MM.DD hh:mm:ss"), 
  • Generate human-readable calendar differences such as: 2 years 3 months 5 days

Simple and Consistent API

  • TimeUtils exposes more than 150 intuitive helper functions that eliminate repetitive date/time code, making Expert Advisors easier to write, read, and maintain.

For example, instead of

MqlDateTime st;
TimeToStruct(TimeCurrent(), st);

if(st.day_of_week == 5 &&
   st.hour >= 18)

you simply write

if(IsFriday(TimeCurrent())
    && Hour(TimeCurrent()) >= 18)

To trade only during the London session (08:00-17:00).

// London session (08:00 -> 17:00 UTC)
if(IsInMarketSession(SESSION_LONDON))
  {
   // Trading logic...
  }

To stop trading around news releases.

// Skip trading ±60 min around high-impact news
if(IsNearNews(TimeCurrent(),
              60,
              60,
              CALENDAR_IMPORTANCE_HIGH))
  {
   // Stop trading...
  }

The code is also hosted on Algo Forge here: https://forge.mql5.io/amrali/TimeUtils

CalendarDifference() is cross validated against Python's dateutil.relativedelta over more than half a million calendar intervals, including leap years, month ends, year boundaries, quarter boundaries, and randomized test cases, with zero mismatches.


Will TimeUtils bloat your EA or slow it down?

No. TimeUtils is a header-only ( .mqh ) library, and the MQL5 compiler automatically removes every function you don't use. Only the functions your EA actually calls are included in the final .ex5 , so there is no unnecessary increase in executable size.

Since the library is compiled directly into your EA, there are no external DLLs, no additional memory usage, and no runtime overhead.

In return, your source code becomes much shorter, cleaner, and easier to maintain by replacing repetitive date/time logic with simple function calls.

Documentation & Core Capabilities:

TimeUtils is a comprehensive date and time utility library for MQL5, providing over 150 self-explanatory functions organized into logical categories. It simplifies common calendar operations, eliminates boilerplate datetime code, and correctly handles leap years, varying month lengths, business days, and other calendar edge cases.

The function names should be self-explanatory; also, you can find a short description for a specific function in the TimeUtils.mqh file.

The library uses a very coherent vocabulary:

  • DateFrom → construction
  • Add → relative movement
  • Difference → elapsed duration
  • StartOf / EndOf → boundary
  • Previous / Next → Adjuster
  • With → field replacement
  • Is → predicate

1. Core Date/Time

Fast access to individual calendar fields together with lightweight helpers for constructing and converting date/time values without manually populating an MqlDateTime structure.

//====================================================================
// Calendar Fields
//====================================================================
Year(datetime)                            Month(datetime)
Day(datetime)                             Hour(datetime)
Minute(datetime)                          Second(datetime)

DayOfWeek(datetime)                       DateOnly(time)
DayOfYear(datetime)                       TimeOnly(time)

2. Construction & Conversion

Create datetime values from calendar components or strings, and efficiently convert between datetime and MqlDateTime structures.

//====================================================================
// Construction & Conversion
//====================================================================
DateFrom(2026, 7, 20)
DateFromString("2026.07.20 14:30")

TimeToStructFast(datetime, MqlDateTime)
StructToTimeFast(MqlDateTime)

3. Calendar Indices & Positions

Retrieve absolute calendar indices or determine the position of a timestamp within a year, quarter, month, week or day.

//====================================================================
// Absolute Indices
//====================================================================
YearIndex(datetime)                       MonthIndex(datetime)
QuarterIndex(datetime)                    WeekIndex(datetime)
DayIndex(datetime)                        HourIndex(datetime)
MinuteIndex(datetime)

//====================================================================
// Position Within Calendar
//====================================================================
Quarter(datetime)

WeekOfYear(datetime)                      WeekOfMonth(datetime)

HourOfYear(datetime)                      MinuteOfYear(datetime)
HourOfQuarter(datetime)                   MinuteOfQuarter(datetime)
HourOfMonth(datetime)                     MinuteOfMonth(datetime)
HourOfWeek(datetime)                      MinuteOfWeek(datetime)
MinuteOfDay(datetime)

SecsElapsedOfYear(datetime)               SecsElapsedOfQuarter(datetime)
SecsElapsedOfMonth(datetime)              SecsElapsedOfWeek(datetime)
SecsElapsedOfDay(datetime)                SecsElapsedOfHour(datetime)
SecsElapsedOfMinute(datetime)             SecsElapsedOfEpoch(datetime)

SecondsSinceMidnight(datetime)

4. Navigation / Manipulation

Navigate through the calendar, snap timestamps to period boundaries, replace individual date/time fields, round to common intervals, and perform calendar-aware arithmetic.

//====================================================================
// Period Boundaries
//====================================================================
StartOfMinute(datetime)                   EndOfMinute(datetime)
StartOfHour(datetime)                     EndOfHour(datetime)
StartOfDay(datetime)                      EndOfDay(datetime)
StartOfWeek(datetime)                     EndOfWeek(datetime)
StartOfMonth(datetime)                    EndOfMonth(datetime)
StartOfQuarter(datetime)                  EndOfQuarter(datetime)
StartOfYear(datetime)                     EndOfYear(datetime)

StartOfToday()                            EndOfToday()
StartOfTomorrow()                         EndOfTomorrow()
StartOfYesterday()                        EndOfYesterday()

//====================================================================
// Calendar Navigation
//====================================================================
LastDayOfWeek(datetime)                   FirstWeekdayOfMonth(datetime, MONDAY)
LastDayOfMonth(datetime)                  LastWeekdayOfMonth(datetime, FRIDAY)
LastDayOfQuarter(datetime)                NthWeekdayOfYearMonth(2026, 11, Nth, FRIDAY)
LastDayOfYear(datetime)

NextDayOfWeek(datetime, MONDAY)           PreviousDayOfWeek(datetime, MONDAY)
NextOrSame(datetime, SUNDAY)              PreviousOrSame(datetime, SUNDAY)
NextSunday(datetime)                      PreviousSunday(datetime)
NextMonday(datetime)                      PreviousMonday(datetime)
NextTuesday(datetime)                     PreviousTuesday(datetime)
NextWednesday(datetime)                   PreviousWednesday(datetime)
NextThursday(datetime)                    PreviousThursday(datetime)
NextFriday(datetime)                      PreviousFriday(datetime)
NextSaturday(datetime)                    PreviousSaturday(datetime)

//====================================================================
// Rounding & Alignment
//====================================================================
RoundToMinute(datetime)                   CeilToMinute(datetime)
RoundToHour(datetime)                     CeilToHour(datetime)
RoundToDay(datetime)                      CeilToDay(datetime)
RoundToWeek(datetime)                     CeilToWeek(datetime)

//====================================================================
// Field Replacement
//====================================================================
WithSecond(datetime, 30)                  WithMonth(datetime, 12)
WithMinute(datetime, 45)                  WithYear(datetime, 2030)
WithHour(datetime, 14)                    WithTime(datetime, 14, 30, 0)
WithDay(datetime, 15)                     WithDate(datetime, 2026, 7, 20)
WithDayOfWeek(datetime, FRIDAY)

//====================================================================
// Arithmetic
//====================================================================
AddSeconds(datetime, 30)                  SubSeconds(datetime, 30)
AddMinutes(datetime, 15)                  SubMinutes(datetime, 15)
AddHours(datetime, 8)                     SubHours(datetime, 8)
AddDays(datetime, 5)                      SubDays(datetime, 5)
AddBusinessDays(datetime, 5)              SubBusinessDays(datetime, 5)
AddWeeks(datetime, 2)                     SubWeeks(datetime, 2)
AddMonths(datetime, 3)                    SubMonths(datetime, 3)
AddQuarters(datetime, 2)                  SubQuarters(datetime, 2)
AddYears(datetime, 10)                    SubYears(datetime, 10)

AddPeriod(datetime, MqlDateTime period)
SubPeriod(datetime, MqlDateTime period)

5. Difference & Duration

Calculate exact elapsed time or calendar-aware differences between two timestamps, including business days, calendar months and human-readable calendar periods.

//====================================================================
// Exact Time Durations
//====================================================================
DifferenceInSeconds(from, to)             DifferenceInBusinessDays(from, to)
DifferenceInMinutes(from, to)             DifferenceInWeekendDays(from, to)
DifferenceInHours(from, to)
DifferenceInDays(from, to)
DifferenceInWeeks(from, to)
DifferenceInMonths(from, to)
DifferenceInQuarters(from, to)
DifferenceInYears(from, to)

//====================================================================
// Calendar Differences
//====================================================================
DifferenceInCalendarDays(from, to)        DifferenceInCalendarWeeks(from, to)
DifferenceInCalendarMonths(from, to)      DifferenceInCalendarQuarters(from, to)
DifferenceInCalendarYears(from, to)

CalendarDifference(from, to, MqlDateTime period)
CalendarDifferenceToString(from, to)

6. Validation & Comparison

Convenience functions for validating dates, comparing timestamps, and checking whether a date belongs to the current calendar period.

//====================================================================
// Day Checks
//====================================================================
IsWeekday(datetime)                       IsWeekend(datetime)

IsSunday(datetime)                        IsThursday(datetime)
IsMonday(datetime)                        IsFriday(datetime)
IsTuesday(datetime)                       IsSaturday(datetime)
IsWednesday(datetime)

IsFirstDayOfMonth(datetime)               IsToday(datetime)
IsLastDayOfMonth(datetime)                IsTomorrow(datetime)
                                          IsYesterday(datetime)

//====================================================================
// Current Period
//====================================================================
IsCurrentMinute(datetime)                 IsCurrentMonth(datetime)
IsCurrentHour(datetime)                   IsCurrentQuarter(datetime)
IsCurrentWeek(datetime)                   IsCurrentYear(datetime)

//====================================================================
// Equality
//====================================================================
IsSameMinute(a, b)                        IsSameMonth(a, b)
IsSameHour(a, b)                          IsSameQuarter(a, b)
IsSameDay(a, b)                           IsSameYear(a, b)
IsSameWeek(a, b)

7. Calendar Information

Query calendar properties such as month lengths, week counts and leap years.

//====================================================================
// Calendar Information
//====================================================================
DaysInMonth(2026, 7)                      WeeksInMonth(2026, 7)
DaysInYear(2026)                          WeeksInYear(2026)

IsLeapYear(2028)

8. Formatting & Display

Format dates and durations using built-in helpers or customizable format strings for reports, logs and user interfaces.

//====================================================================
// Formatting
//====================================================================
string DayName(ENUM_DAY_OF_WEEK)
string MonthName(7)

string SecondsToString(3661)

string t2s(datetime, TIME_DATE | TIME_MINUTES)
string TimeFormat(datetime, "YYYY.MM.DD hh:mm")

List of all available time formats for TimeFormat():

//+------------------------------------------------------------------+
//| Get the formatted time according to the passed string of tokens. |
//| List of all available formats:                                   |
//| Format  Output            Description                            |
//| ------  ----------------  -------------------------------------  |
//| YY      18                Two-digit year                         |
//| YYYY    2018              Four-digit year                        |
//| M       1-12              The month, beginning at 1              |
//| MM      01-12             The month, 2-digits                    |
//| MMM     Jan-Dec           The abbreviated month name, 3-letters  |
//| MMMM    January-December  The full month name                    |
//| D       1-31              The day of the month                   |
//| DD      01-31             The day of the month, 2-digits         |
//| DDD     Sun-Sat           The short name of the day of the week  |
//| DDDD    Sunday-Saturday   The name of the day of the week        |
//| h       0-23              The hour                               |
//| hh      00-23             The hour, 2-digits                     |
//| H       1-12              The hour, 12-hour clock                |
//| HH      01-12             The hour, 12-hour clock, 2-digits      |
//| m       0-59              The minute                             |
//| mm      00-59             The minute, 2-digits                   |
//| s       0-59              The second                             |
//| ss      00-59             The second, 2-digits                   |
//| A       AM PM                                                    |
//| a       am pm                                                    |
//+------------------------------------------------------------------+
//| Sample formats:                                                  |
//| "YYYY.MM.DD hh:mm"           // "2024.12.08 22:05"  (default)    |
//| "DDD, YYYY.MM.DD hh:mm:ss"   // "Sun, 2024.12.08 22:05:21"       |
//| "D MMMM YYYY, HH:mm a"       // "8 December 2024, 10:05 pm"      |
//| "DD/MM/YYYY"                 // "08/12/2024"                     |
//+------------------------------------------------------------------+
string TimeFormat(const datetime t, const string format = "YYYY.MM.DD hh:mm");

9. Trading Sessions

Utilities for recurring daily trading sessions, including the four major FX sessions (Sydney, Tokyo, London and New York) as well as user-defined sessions. Sessions may occur within a single day or span midnight, and built-in FX sessions can be converted from UTC into broker server time (or any other time zone) by supplying the corresponding UTC offset, for example:

   int offset = (int)(TimeTradeServer() - TimeGMT());


//====================================================================
// Generic Daily Sessions                  Major FX Sessions
//====================================================================
IsInSession(8,0,17,0)                     IsInMarketSession(SESSION_LONDON)

IsInSession(time,                         IsInMarketSession(time,
            8,0,17,0)                                       SESSION_LONDON,
                                                            utcOffsetSeconds)

IsInSession("08:00",                      CurrentMarketSession()
            "17:00")

                                          CurrentMarketSession(time,
                                                               utcOffsetSeconds)

                                          MarketSessionName(CurrentMarketSession())

                                          IsSessionOverlap(SESSION_LONDON,
                                                           SESSION_NEW_YORK)

                                          IsSessionOverlap(time,
                                                           SESSION_LONDON,
                                                           SESSION_NEW_YORK,
                                                           utcOffsetSeconds)

10. Economic Calendar / News

TimeUtils also provides lightweight helpers for working with the MQL5 Economic Calendar, including news blackout detection and countdowns to upcoming economic events.

//====================================================================
// News Checks                            Time Until News
//====================================================================
IsNearNews(checkTime,                     TimeUntilNews(from,
           eventTime,                                   events)
           before,
           after)

IsNearNews(checkTime,                     TimeUntilNews(from,
           before,                                      CALENDAR_IMPORTANCE_HIGH)
           after,
           CALENDAR_IMPORTANCE_HIGH)


Basic example:

#include "TimeUtils.mqh"

void OnStart()
  {
   datetime t = TimeLocal();
   Print( t2s(t, TIME_DATE|TIME_SECONDS) );  // Formats time with the weekday name

   Print( Year(t)   );
   Print( Month(t)  );
   Print( Day(t)    );
   Print( Hour(t)   );
   Print( Minute(t) );
   Print( Second(t) );
   Print( DayOfWeek(t) );
   Print( DayOfYear(t) );

   MqlDateTime dt[1] = {};
   TimeToStructFast(t, dt[0]);
   ArrayPrint(dt);

   dt[0].year += 1;
   Print(StructToTimeFast(dt[0]));
  }

The two attached scripts "basic.mq5" and "advanced.mq5" shows the basic and advanced usage examples.

An example output from "basic.mq5" script:



An example output from "advanced.mq5" script:

/*
 example output:

 1. TimeToString(t, TIME_DATE|TIME_SECONDS) = 2026.07.18 19:54:28
 2. t2s( t, TIME_DATE|TIME_SECONDS) = Sat, 2026.07.18 19:54:28
 3. TimeToStructFast(t, dt[0]) = true
     [year] [mon] [day] [hour] [min] [sec] [day_of_week] [day_of_year]
 [0]   2026     7    18     19    54    28             6           198
 4. DateFrom(2022, 03, 25) = 2022.03.25 00:00:00
 5. StructToTimeFast(dt[0]) = 2026.07.18 19:54:28
 6. Second(t) = 28
 7. Minute(t) = 54
 8. Hour(t) = 19
 9. Day(t) = 18
 10. Month(t) = 7
 11. Year(t) = 2026
 12. DateOnly(t) = 2026.07.18 00:00:00
 13. TimeOnly(t) = 1970.01.01 19:54:28
 14. DayOfWeek(t) = 6
 15. DayOfYear(t) = 198
 16. WeekOfMonth(t) = 3
 17. WeekOfYear(t) = 29
 18. MinuteIndex(t) = 29740074
 19. HourIndex(t) = 495667
 20. DayIndex(t) = 20652
 21. WeekIndex(t) = 2950
 22. MonthIndex(t) = 678
 23. QuarterIndex(t) = 226
 24. YearIndex(t) = 56
 25. Quarter(t) = 3
 26. HourOfWeek(t) = 139
 27. HourOfMonth(t) = 427
 28. HourOfQuarter(t) = 427
 29. HourOfYear(t) = 4771
 30. MinuteOfDay(t) = 1194
 31. MinuteOfWeek(t) = 8394
 32. MinuteOfMonth(t) = 25674
 33. MinuteOfQuarter(t) = 25674
 34. MinuteOfYear(t) = 286314
 35. SecsElapsedOfMinute(t) = 28
 36. SecsElapsedOfHour(t) = 3268
 37. SecsElapsedOfDay(t) = 71668
 38. SecsElapsedOfWeek(t) = 503668
 39. SecsElapsedOfMonth(t) = 1540468
 40. SecsElapsedOfQuarter(t) = 1540468
 41. SecsElapsedOfYear(t) = 17178868
 42. SecsElapsedOfEpoch(t) = 1784404468
 43. StartOfMinute(t) = 2026.07.18 19:54:00
 44. StartOfHour(t) = 2026.07.18 19:00:00
 45. StartOfDay(t) = 2026.07.18 00:00:00
 46. StartOfWeek(t) = 2026.07.13 00:00:00
 47. StartOfMonth(t) = 2026.07.01 00:00:00
 48. StartOfQuarter(t) = 2026.07.01 00:00:00
 49. StartOfYear(t) = 2026.01.01 00:00:00
 50. StartOfToday() = 2026.07.18 00:00:00
 51. StartOfTomorrow() = 2026.07.19 00:00:00
 52. StartOfYesterday() = 2026.07.17 00:00:00
 53. EndOfMinute(t) = 2026.07.18 19:54:59
 54. EndOfHour(t) = 2026.07.18 19:59:59
 55. EndOfDay(t) = 2026.07.18 23:59:59
 56. EndOfWeek(t) = 2026.07.19 23:59:59
 57. EndOfMonth(t) = 2026.07.31 23:59:59
 58. EndOfQuarter(t) = 2026.09.30 23:59:59
 59. EndOfYear(t) = 2026.12.31 23:59:59
 60. EndOfToday() = 2026.07.18 23:59:59
 61. EndOfTomorrow() = 2026.07.19 23:59:59
 62. EndOfYesterday() = 2026.07.17 23:59:59
 63. LastDayOfWeek(t) = 2026.07.19 00:00:00
 64. LastDayOfMonth(t) = 2026.07.31 00:00:00
 65. LastDayOfQuarter(t) = 2026.09.30 00:00:00
 66. LastDayOfYear(t) = 2026.12.31 00:00:00
 67. RoundToMinute(t) = 2026.07.18 19:54:00
 68. RoundToHour(t) = 2026.07.18 20:00:00
 69. RoundToDay(t) = 2026.07.19 00:00:00
 70. RoundToWeek(t) = 2026.07.20 00:00:00
 71. CeilToMinute(t) = 2026.07.18 19:55:00
 72. CeilToHour(t) = 2026.07.18 20:00:00
 73. CeilToDay(t) = 2026.07.19 00:00:00
 74. CeilToWeek(t) = 2026.07.20 00:00:00
 75. NextSunday(t) = 2026.07.19 00:00:00
 76. NextMonday(t) = 2026.07.20 00:00:00
 77. NextTuesday(t) = 2026.07.21 00:00:00
 78. NextWednesday(t) = 2026.07.22 00:00:00
 79. NextThursday(t) = 2026.07.23 00:00:00
 80. NextFriday(t) = 2026.07.24 00:00:00
 81. NextSaturday(t) = 2026.07.25 00:00:00
 82. PreviousSunday(t) = 2026.07.12 00:00:00
 83. PreviousMonday(t) = 2026.07.13 00:00:00
 84. PreviousTuesday(t) = 2026.07.14 00:00:00
 85. PreviousWednesday(t) = 2026.07.15 00:00:00
 86. PreviousThursday(t) = 2026.07.16 00:00:00
 87. PreviousFriday(t) = 2026.07.17 00:00:00
 88. PreviousSaturday(t) = 2026.07.11 00:00:00
 89. FirstWeekdayOfMonth(t, SUNDAY) = 2026.07.05 00:00:00
 90. LastWeekdayOfMonth(t, SUNDAY) = 2026.07.26 00:00:00
 91. WithSecond(t, 05) = 2026.07.18 19:54:05
 92. WithMinute(t, 30) = 2026.07.18 19:30:28
 93. WithHour(t, 10) = 2026.07.18 10:54:28
 94. WithDay(t, 26) = 2026.07.26 19:54:28
 95. WithMonth(t, 2) = 2026.02.18 19:54:28
 96. WithYear(t, 2024) = 2024.07.18 19:54:28
 97. WithDayOfWeek(t, FRIDAY) = 2026.07.17 19:54:28
 98. WithTime(t, 10, 30, 05) = 2026.07.18 10:30:05
 99. WithDate(t, 2024, 2, 26) = 2024.02.26 19:54:28
 100. AddSeconds(t, 30) = 2026.07.18 19:54:58
 101. AddMinutes(t, 30) = 2026.07.18 20:24:28
 102. AddHours(t, 2) = 2026.07.18 21:54:28
 103. AddDays(t, 10) = 2026.07.28 19:54:28
 104. AddBusinessDays(t, 10) = 2026.07.31 19:54:28
 105. AddWeeks(t, 4) = 2026.08.15 19:54:28
 106. AddMonths(t, 2) = 2026.09.18 19:54:28
 107. AddQuarters(t, 3) = 2027.04.18 19:54:28
 108. AddYears(t, 5) = 2031.07.18 19:54:28
 109. SubSeconds(t, 30) = 2026.07.18 19:53:58
 110. SubMinutes(t, 30) = 2026.07.18 19:24:28
 111. SubHours(t, 2) = 2026.07.18 17:54:28
 112. SubDays(t, 10) = 2026.07.08 19:54:28
 113. SubBusinessDays(t, 10) = 2026.07.06 19:54:28
 114. SubWeeks(t, 4) = 2026.06.20 19:54:28
 115. SubMonths(t, 2) = 2026.05.18 19:54:28
 116. SubQuarters(t, 3) = 2025.10.18 19:54:28
 117. SubYears(t, 5) = 2021.07.18 19:54:28
 118. DifferenceInCalendarDays( StartOfYear(t), EndOfYear(t)) = 364
 119. DifferenceInCalendarWeeks( StartOfYear(t), EndOfYear(t)) = 52
 120. DifferenceInCalendarMonths( StartOfYear(t), EndOfYear(t)) = 11
 121. DifferenceInCalendarQuarters( StartOfYear(t), EndOfYear(t)) = 3
 122. DifferenceInCalendarYears( StartOfYear(t), EndOfYear(t)) = 0
 123. DifferenceInSeconds( t, EndOfDay(t)) = 14731
 124. DifferenceInMinutes( t, EndOfDay(t)) = 245
 125. DifferenceInHours( t, EndOfDay(t)) = 4
 126. DifferenceInDays( StartOfYear(t), EndOfYear(t)) = 364
 127. DifferenceInWeeks( StartOfYear(t), EndOfYear(t)) = 52
 128. DifferenceInMonths( StartOfYear(t), EndOfYear(t)) = 11
 129. DifferenceInQuarters( StartOfYear(t), EndOfYear(t)) = 3
 130. DifferenceInYears( StartOfYear(t), EndOfYear(t)) = 0
 131. DifferenceInBusinessDays( StartOfYear(t), EndOfYear(t)) = 260
 132. DifferenceInWeekendDays( StartOfYear(t), EndOfYear(t)) = 104
 133. CalendarDifference( t, D'2035.10.9', diff ) = true
 134. CalendarDifferenceToString( t, D'2035.10.9' ) = 9y 2m 20d 04:05:32
 135. AddPeriod( t, diff ) = 2035.10.09 00:00:00
 136. SubPeriod( D'2035.10.9', diff ) = 2026.07.18 19:54:28
 137. IsFirstDayOfMonth(t) = false
 138. IsLastDayOfMonth(t) = false
 139. IsWeekend(t) = true
 140. IsWeekday(t) = false
 141. IsSunday(t) = false
 142. IsMonday(t) = false
 143. IsTuesday(t) = false
 144. IsWednesday(t) = false
 145. IsThursday(t) = false
 146. IsFriday(t) = false
 147. IsSaturday(t) = true
 148. IsSameMinute(t, AddHours(t, 25)) = false
 149. IsSameHour(t, AddHours(t, 25)) = false
 150. IsSameDay(t, AddHours(t, 25)) = false
 151. IsSameWeek(t, AddHours(t, 25)) = true
 152. IsSameMonth(t, AddHours(t, 25)) = true
 153. IsSameQuarter(t, AddHours(t, 25)) = true
 154. IsSameYear(t, AddHours(t, 25)) = true
 155. IsCurrentMinute(t) = true
 156. IsCurrentHour(t) = true
 157. IsCurrentWeek(t) = true
 158. IsCurrentMonth(t) = true
 159. IsCurrentQuarter(t) = true
 160. IsCurrentYear(t) = true
 161. IsToday(t) = true
 162. IsTomorrow(t) = false
 163. IsYesterday(t) = false
 164. IsInSession(8, 0, 17, 0) = false
 165. IsInSession(22:00, 06:00) = false
 166. IsLeapYear(2100) = false
 167. DaysInYear(2100) = 365
 168. DaysInMonth(2024, 2) = 29
 169. WeeksInYear(2026) = 53
 170. WeeksInMonth(2026, 8) = 6
 171. TimeToString(t) = 2026.07.18 19:54
 172. t2s(t) = Sat, 2026.07.18 19:54
 173. TimeFormat(t) = 2026.07.18 19:54
 174. TimeFormat(t, DDD, YYYY.MM.DD hh:mm:ss) = Sat, 2026.07.18 19:54:28
 175. TimeFormat(t, DDDD, D MMM YYYY, HH:mm a) = Saturday, 18 Jul 2026, 07:54 pm
 176. SecondsToString( SecsElapsedOfDay(t) ) = 19:54:28

*/

Updates:

2024.12.03 - v.1.10 : Initial release.

2024.12.05 - v1.15 : Added functions for business days and for Nth() weekday of the month. Optimized calculations in DaysInMonth() function.

2024.12.09 - v1.20 : Added TimeFormat() function to format the time according to the passed string of tokens.

2024.12.09 - v1.25 : Optimized calculations in TimeToStructFast() function.

2024.12.10 - v1.30 : Optimized TimeFormat(), StartOfYear() and EndOfYear() functions. Updated descriptions of some functions.

2024.12.15 - v1.35 : Optimized NthWeekdayOfYearMonth() function.

2024.12.18 - v1.40 : Added IsCurrentXXX(), IsToday(), IsTomorrow() and IsYesterday() functions.

2024.12.19 - v1.45 : Optimized AddMonths() and NthWeekdayOfYearMonth() functions.

2024.12.20 - v1.50 : More code clean-up.

2024.12.21 - v1.55 : Simplified calculations in AddMonths(): replaced the complex logic of adjusting months and years with a simpler total months calculation.

2026.06.22 - v1.60 : Optimized AddBusinessDays() and DifferenceInBusinessDays() functions. Added various IsXXX() check functions.

2026.07.06 - v2.00 : New March-based conversion routines derived from the Neri–Schneider calendar algorithm + validation script. Removed "performance" mode.

2026.07.07 - v2.10 : Added DaysInYear(), LastDayOf(), MinuteOf(), HourOf() and CalendarDifferenceToString() functions.

2026.07.10 - v2.30 : Added date construction, quarter, calendar difference, and relative-date helper functions. Introduced configurable week start and renamed several functions for a cleaner, more consistent API.

2026.07.12 - v2.35 : Updated CalendarDifferenceToString() to return the precise elapsed time, including hours, minutes, and seconds.

2026.07.14 - v2.40 : Redesigned the year/month decomposition in CalendarDifferenceToString() to correctly handle February 29 edge cases. Simplified and optimized the DifferenceInXXX() functions. Added epoch-based index functions.

2026.07.15 - v2.45 : Added CalendarDifference() to return the calendar difference between two dates, broken down into years, months, days, hours, minutes, and seconds.

2026.07.18 - v2.50 : Added AddPeriod() and SubPeriod() for calendar period arithmetic. Added WithXxx() functions for replacing individual date/time fields. Added IsInSession() for checking recurring daily sessions.

2026.07.20 - v2.55 : Optimized week-based functions. Added SessionOverlap() for calculating overlapping time intervals. Improved documentation and API comments.

2026.07.21 - v2.60 : Added comprehensive Trading Sessions and Economic Calendar utilities, including IsInSession(), IsInMarketSession(), IsSessionOverlap(), CurrentMarketSession(), MarketSessionName(), IsNearNews(), TimeUntilNews(), and NextOrSame()/PreviousOrSame() for inclusive weekday navigation. General code cleanup and documentation improvements.

2026.08.10 - v2.65 : Code cleanup and documentation improvements.



Converted from Pine Script - Fibonacci Bollinger Bands Converted from Pine Script - Fibonacci Bollinger Bands

This Fibonacci Bollinger Bands indicator is a conversion from Pine Script (by Rashad) to the MQL5 language.

CloseAgent CloseAgent

Optimal closing tool with Bollinger Bands and RSI.

MarketPredictor MarketPredictor

MarketPredictor for MetaTrader 5 The MarketPredictor is an innovative Expert Advisor (EA) for MetaTrader 5 that leverages mathematical models such as sine functions, Fast Fourier Transform (FFT), sigmoid functions, and Monte Carlo simulations to analyze and predict market movements. This project is designed for developers, math enthusiasts, and traders interested in combining technological and financial innovations. Feel free to suggest, discuss, and implement code ideas directly in this thread. Whether it’s new features, improvement suggestions, or strategies – every contribution is welcome to further develop and optimize the MarketPredictor. You’re also welcome to add me to clarify questions privately, collaborate on the GitHub project, or send me your feedback directly. Let’s cook up something amazing together and take this project to the next level!

Price increase Indicator Price increase Indicator

Price increase Indicator