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:
8462
Rating:
(15)
Published:
Updated:
TimeUtils.mqh (193.85 KB) view
basic.mq5 (2.36 KB) view
advanced.mq5 (43.27 KB) view
validate.mq5 (4.48 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 120 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)
 

Documentation & Core Capabilities

The library exposes over 120 self-explanatory functions organized into distinct, logical categories to eliminate boilerplate datetime logic.

1. Fast Component Extraction

    Extract specific time units directly from a datetime timestamp without manually declaring or filling an MqlDateTime structure:

  • Year(t) , Month(t) , Day(t) , Hour(t) , Minute(t) , Second(t)

  • DayOfWeek(t) , DayOfYear(t) , DayOfEpoch(t)

  • WeekOfMonth(t) , WeekOfYear(t) , Quarter(t)

2. Relative Navigation & Adjusters

    Easily snap to specific calendar boundaries or jump to relative days:

  • Boundaries: StartOfDay() , EndOfToday() , EndOfWeek() , LastDayOfMonth()

  • Rounding: RoundToHour() , CeilToMinute()

  • Weekday Navigation: NextFriday() , PreviousMonday() , FirstWeekdayOfMonth() , LastWeekdayOfMonth()

3. Advanced Calendar Math

    Add or subtract complex intervals natively while automatically handling leap years, varying month lengths, and trading weekends:

  • AddBusinessDays() , SubBusinessDays()

  • AddMonths() , SubQuarters() , AddYears()

4. Precise Date Differences (Duration)

    Calculate exact durations between two timestamps without error-prone manual math:

  • DifferenceInBusinessDays(from, to)

  • DifferenceInWeekendDays(from, to)

  • DifferenceInCalendarMonths(from, to)

5. Token-Based Formatting

     Format dates using customizable token strings or generate human-readable elapsed text:

  • TimeFormat(TimeCurrent(), "DDD, YYYY.MM.DD hh:mm:ss")

  • CalendarDifferenceToString(from, to)  -> Outputs: "2 years 3 months 5 days"

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 library bloat your compiled EA binary file or slow it down?

    It is a common concern, but including the library will not bloat your compiled EA binary file or slow it down. Here is why it is perfectly safe to include in a small project:

    1. The Compiler Automatically Deletes Unused Code

    MQL5 uses an optimizing compiler. When you include a file like TimeUtils.mqh, the compiler looks at your code and only compiles the exact functions you actually call.

    • If the library has 120 functions and you only use Hour() and IsFriday(), the other 118 functions are completely stripped out during compilation. Your final .ex5 executable file remains small and lightweight.

    2. Zero Runtime Overhead

    Unlike other programming languages that load heavy external libraries (like .dll files) into memory while running, MQL5 source libraries (.mqh) are compiled statically. This means the code you use is baked directly into your EA at compile time, resulting in zero extra memory overhead or startup delay when you attach it to a chart.

    3. It Cleans Up Your Source File

    Instead of writing 10–20 lines of your own structural math code to check date boundaries or skip weekends, you just write one clean line. This keeps your main EA .mq5 file small, readable, and easy to debug.

    Listing of all functions in the library:

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

    1. Construction

    //####################################################################
    //#                                                                  #
    //#                          Construction                            #
    //#                                                                  #
    //####################################################################
    
    //+==================================================================+
    //| Create datetime From Components                                  |
    //+==================================================================+
    datetime DateFrom(int year,           Year
                      int mon,            Month
                      int day,            Day
                      int hour = 0,       Hour
                      int min = 0,        Minutes
                      int sec = 0)        Seconds
    
    datetime  DateFromString(string text)
    
    datetime  StructToTimeFast(
       MqlDateTime&  dt_struct   // structure of the date and time
       )
    
    

    2. Calendar Components

    //####################################################################
    //#                                                                  #
    //#                       Calendar Components                        #
    //#                                                                  #
    //####################################################################
    
    //+==================================================================+
    //| Break datetime To Components                                     |
    //+==================================================================+
    bool TimeToStructFast(
       datetime      dt,         // Date value to convert
       MqlDateTime&  dt_struct   // structure for the adoption of values
       )
    
    //+==================================================================+
    //| Extract Components of datetime: Sunday, yyyy.mm.dd hh:mm:ss      |
    //| Get() Units                                                      |
    //+==================================================================+
    int Second( )
    int Minute( )
    int Hour( )
    int Day( )
    int Month( )
    int Year( )
    
    int Second( datetime t )
    int Minute( datetime t )
    int Hour( datetime t )
    int Day( datetime t )
    int Month( datetime t )
    int Year( datetime t )
    
    datetime DateOnly( datetime t )
    datetime TimeOnly( datetime t )
    
    //+==================================================================+
    //| Day() Number                                                     |
    //+==================================================================+
    int DayOfWeek( )
    int DayOfYear( )
    int DayOfWeek( datetime t )
    int DayOfYear( datetime t )
    
    //+==================================================================+
    //| Week() Number                                                    |
    //+==================================================================+
    int WeekOfMonth( datetime t )
    int WeekOfYear( datetime t )
    
    //+==================================================================+
    //| Epoch-Based Indices                                              |
    //+==================================================================+
    int MinuteIndex( datetime t )
    int HourIndex( datetime t )
    int DayIndex( datetime t )
    int WeekIndex( datetime t )
    int MonthIndex( datetime t )
    int QuarterIndex( datetime t )
    int YearIndex( datetime t )
    
    //+==================================================================+
    //| Quarter() Number                                                 |
    //+==================================================================+
    int Quarter( datetime t )
    
    //+==================================================================+
    //| Hour() Number                                                    |
    //+==================================================================+
    int HourOfWeek( datetime t )
    int HourOfMonth( datetime t )
    int HourOfQuarter( datetime t )
    int HourOfYear( datetime t )
    
    //+==================================================================+
    //| Minute() Number                                                  |
    //+==================================================================+
    int MinuteOfDay( datetime t )
    int MinuteOfWeek( datetime t )
    int MinuteOfMonth( datetime t )
    int MinuteOfQuarter( datetime t )
    int MinuteOfYear( datetime t )
    
    //+==================================================================+
    //| SecsElapsedOf()                                                  |
    //+==================================================================+
    int  SecsElapsedOfMinute( datetime t )
    int  SecsElapsedOfHour( datetime t )
    int  SecsElapsedOfDay( datetime t )
    int  SecsElapsedOfWeek( datetime t )
    int  SecsElapsedOfMonth( datetime t )
    int  SecsElapsedOfQuarter( datetime t )
    int  SecsElapsedOfYear( datetime t )
    long SecsElapsedOfEpoch( datetime t )
    
    

    3. Adjusters / Navigation

    //####################################################################
    //#                                                                  #
    //#                    Adjusters / Navigation                        #
    //#                                                                  #
    //####################################################################
    
    //+==================================================================+
    //| StartOf() Units                                                  |
    //+==================================================================+
    datetime StartOfMinute( datetime t )
    datetime StartOfHour( datetime t )
    datetime StartOfDay( datetime t )
    datetime StartOfWeek( datetime t )
    datetime StartOfMonth( datetime t )
    datetime StartOfQuarter( datetime t )
    datetime StartOfYear( datetime t )
    datetime StartOfToday( )
    datetime startOfTomorrow( )
    datetime startOfYesterday( )
    
    //+==================================================================+
    //| EndOf() Units                                                    |
    //+==================================================================+
    datetime EndOfMinute( datetime t )
    datetime EndOfHour( datetime t )
    datetime EndOfDay( datetime t )
    datetime EndOfWeek( datetime t )
    datetime EndOfMonth( datetime t )
    datetime EndOfQuarter( datetime t )
    datetime EndOfYear( datetime t )
    datetime EndOfToday( )
    datetime EndOfTomorrow( )
    datetime EndOfYesterday( )
    
    //+==================================================================+
    //| LastDayOf() Units                                                |
    //+==================================================================+
    datetime LastDayOfWeek( datetime t )
    datetime LastDayOfMonth( datetime t )
    datetime LastDayOfQuarter( datetime t )
    datetime LastDayOfYear( datetime t )
    
    //+==================================================================+
    //| RoundTo() / Nearest() Units                                      |
    //+==================================================================+
    datetime RoundToMinute( datetime t )
    datetime RoundToHour( datetime t )
    datetime RoundToDay( datetime t )
    datetime RoundToWeek( datetime t )
    
    //+==================================================================+
    //| CeilTo() / Next() Units                                          |
    //+==================================================================+
    datetime CeilToMinute( datetime t )
    datetime CeilToHour( datetime t )
    datetime CeilToDay( datetime t )
    datetime CeilToWeek( datetime t )
    
    //+==================================================================+
    //| Next() Weekday                                                   |
    //+==================================================================+
    datetime NextDayOfWeek(datetime t, ENUM_DAY_OF_WEEK weekday = SUNDAY)
    datetime NextSunday( datetime t )
    datetime NextMonday( datetime t )
    datetime NextTuesday( datetime t )
    datetime NextWednesday( datetime t )
    datetime NextThursday( datetime t )
    datetime NextFriday( datetime t )
    datetime NextSaturday( datetime t )
    
    //+==================================================================+
    //| Previous() Weekday                                               |
    //+==================================================================+
    datetime PreviousDayOfWeek(datetime t, ENUM_DAY_OF_WEEK weekday = SUNDAY)
    datetime PreviousSunday( datetime t )
    datetime PreviousMonday( datetime t )
    datetime PreviousTuesday( datetime t )
    datetime PreviousWednesday( datetime t )
    datetime PreviousThursday( datetime t )
    datetime PreviousFriday( datetime t )
    datetime PreviousSaturday( datetime t )
    
    //+==================================================================+
    //| Nth() Weekday                                                    |
    //+==================================================================+
    datetime NthWeekdayOfYearMonth( int year, int month, int Nth, weekday = SUNDAY)
    datetime FirstWeekdayOfMonth( datetime t,  ENUM_DAY_OF_WEEK weekday = SUNDAY)
    datetime LastWeekdayOfMonth( datetime t,  ENUM_DAY_OF_WEEK weekday = SUNDAY)
    
    

    4. Add / Subtract

    //####################################################################
    //#                                                                  #
    //#                   Addition and Subtraction                       #
    //#                                                                  #
    //####################################################################
    
    //+==================================================================+
    //| Add() Units                                                      |
    //+==================================================================+
    datetime AddSeconds( datetime t, int amount )
    datetime AddMinutes( datetime t, int amount )
    datetime AddHours( datetime t, int amount )
    datetime AddDays( datetime t, int amount )
    datetime AddBusinessDays( datetime t, int amount )
    datetime AddWeeks( datetime t, int amount )
    datetime AddMonths( datetime t, int amount )
    datetime AddQuarters( datetime t, int amount )
    datetime AddYears( datetime t, int amount )
    
    //+==================================================================+
    //| Sub() Units                                                      |
    //+==================================================================+
    datetime SubSeconds( datetime t, int amount )
    datetime SubMinutes( datetime t, int amount )
    datetime SubHours( datetime t, int amount )
    datetime SubDays( datetime t, int amount )
    datetime SubBusinessDays( datetime t, int amount )
    datetime SubWeeks( datetime t, int amount )
    datetime SubMonths( datetime t, int amount )
    datetime SubQuarters( datetime t, int amount )
    datetime SubYears( datetime t, int amount )
    
    

    5. Difference

    //+==================================================================+
    //| DifferenceIn() Units                                             |
    //+==================================================================+
    int    DifferenceInCalendarDays( datetime from, datetime to )
    int    DifferenceInCalendarWeeks( datetime from, datetime to )
    int    DifferenceInCalendarMonths( datetime from, datetime to )
    int    DifferenceInCalendarQuarters( datetime from, datetime to )
    int    DifferenceInCalendarYears( datetime from, datetime to )
    long   DifferenceInSeconds( datetime from, datetime to )
    int    DifferenceInMinutes( datetime from, datetime to )
    int    DifferenceInHours( datetime from, datetime to )
    int    DifferenceInDays( datetime from, datetime to )
    int    DifferenceInWeeks( datetime from, datetime to )
    int    DifferenceInMonths( datetime from, datetime to )
    int    DifferenceInQuarters( datetime from, datetime to )
    int    DifferenceInYears( datetime from, datetime to )
    int    DifferenceInBusinessDays( datetime from, datetime to )
    int    DifferenceInWeekendDays( datetime from, datetime to )
    void   CalendarDifference( datetime from, datetime to, MqlDateTime& diff )
    
    

    6. Comparison and Checks

    //####################################################################
    //#                                                                  #
    //#                      Comparison and Checks                       #
    //#                                                                  #
    //####################################################################
    
    //+==================================================================+
    //| Is() Checks                                                      |
    //+==================================================================+
    bool IsFirstDayOfMonth( datetime t )
    bool IsLastDayOfMonth( datetime t )
    bool IsWeekend( datetime t )
    bool IsWeekday( datetime t )
    bool IsSunday( datetime t )
    bool IsMonday( datetime t )
    bool IsTuesday( datetime t )
    bool IsWednesday( datetime t )
    bool IsThursday( datetime t )
    bool IsFriday( datetime t )
    bool IsSaturday( datetime t )
    
    //+==================================================================+
    //| IsSame() Units                                                   |
    //+==================================================================+
    bool IsSameMinute( datetime a, datetime b )
    bool IsSameHour( datetime a, datetime b )
    bool IsSameDay( datetime a, datetime b )
    bool IsSameWeek( datetime a, datetime b )
    bool IsSameMonth( datetime a, datetime b )
    bool IsSameQuarter( datetime a, datetime b )
    bool IsSameYear( datetime a, datetime b )
    
    //+==================================================================+
    //| IsCurrent() Units                                                |
    //+==================================================================+
    bool IsCurrentMinute( datetime t )
    bool IsCurrentHour( datetime t )
    bool IsCurrentWeek( datetime t )
    bool IsCurrentMonth( datetime t )
    bool IsCurrentQuarter( datetime t )
    bool IsCurrentYear( datetime t )
    bool IsToday( datetime t )
    bool IsTomorrow( datetime t )
    bool IsYesterday( datetime t )
    
    //+==================================================================+
    //| Misc                                                             |
    //+==================================================================+
    bool IsLeapYear( int year )
    int  DaysInYear( int year )
    int  DaysInMonth( int year, int month )
    int  WeeksInYear( int year )
    int  WeeksInMonth( int year, int month )
    
    

    7. Format & display

    //####################################################################
    //#                                                                  #
    //#                        Format & Display                          #
    //#                                                                  #
    //####################################################################
    
    string t2s( datetime t, int mode = TIME_DATE | TIME_MINUTES )
    string SecondsToString( int seconds )
    string MonthName( int monthNo )
    string DayName( int dayNo )
    string TimeFormat( datetime t, string format = "YYYY.MM.DD hh:mm" )
    string CalendarDifferenceToString( datetime from, datetime to )
    
    

    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");
    

    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 st[1] = {};
       TimeToStructFast(t, st[0]);
       ArrayPrint(st);
    
       st[0].year += 1;
       Print(StructToTimeFast(st[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.14 14:53:43
     2. t2s( t, TIME_DATE|TIME_SECONDS) = Tue, 2026.07.14 14:53:43
     3. TimeToStructFast(t, st[0]) = true
         [year] [mon] [day] [hour] [min] [sec] [day_of_week] [day_of_year]
     [0]   2026     7    14     14    53    43             2           194
     4. DateFrom(2022, 03, 25) = 2022.03.25 00:00:00
     5. StructToTimeFast(st[0]) = 2026.07.14 14:53:43
     6. Second(t) = 43
     7. Minute(t) = 53
     8. Hour(t) = 14
     9. Day(t) = 14
     10. Month(t) = 7
     11. Year(t) = 2026
     12. DateOnly(t) = 2026.07.14 00:00:00
     13. TimeOnly(t) = 1970.01.01 14:53:43
     14. DayOfWeek(t) = 2
     15. DayOfYear(t) = 194
     16. WeekOfMonth(t) = 3
     17. WeekOfYear(t) = 29
     18. MinuteIndex(t) = 29734013
     19. HourIndex(t) = 495566
     20. DayIndex(t) = 20648
     21. WeekIndex(t) = 2950
     22. MonthIndex(t) = 678
     23. QuarterIndex(t) = 226
     24. YearIndex(t) = 56
     25. Quarter(t) = 3
     26. HourOfWeek(t) = 38
     27. HourOfMonth(t) = 326
     28. HourOfQuarter(t) = 326
     29. HourOfYear(t) = 4670
     30. MinuteOfDay(t) = 893
     31. MinuteOfWeek(t) = 2333
     32. MinuteOfMonth(t) = 19613
     33. MinuteOfQuarter(t) = 19613
     34. MinuteOfYear(t) = 280253
     35. SecsElapsedOfMinute(t) = 43
     36. SecsElapsedOfHour(t) = 3223
     37. SecsElapsedOfDay(t) = 53623
     38. SecsElapsedOfWeek(t) = 140023
     39. SecsElapsedOfMonth(t) = 1176823
     40. SecsElapsedOfQuarter(t) = 1176823
     41. SecsElapsedOfYear(t) = 16815223
     42. SecsElapsedOfEpoch(t) = 1784040823
     43. StartOfMinute(t) = 2026.07.14 14:53:00
     44. StartOfHour(t) = 2026.07.14 14:00:00
     45. StartOfDay(t) = 2026.07.14 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.14 00:00:00
     51. startOfTomorrow() = 2026.07.15 00:00:00
     52. startOfYesterday() = 2026.07.13 00:00:00
     53. EndOfMinute(t) = 2026.07.14 14:53:59
     54. EndOfHour(t) = 2026.07.14 14:59:59
     55. EndOfDay(t) = 2026.07.14 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.14 23:59:59
     61. EndOfTomorrow() = 2026.07.15 23:59:59
     62. EndOfYesterday() = 2026.07.13 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.14 14:54:00
     68. RoundToHour(t) = 2026.07.14 15:00:00
     69. RoundToDay(t) = 2026.07.15 00:00:00
     70. RoundToWeek(t) = 2026.07.13 00:00:00
     71. CeilToMinute(t) = 2026.07.14 14:54:00
     72. CeilToHour(t) = 2026.07.14 15:00:00
     73. CeilToDay(t) = 2026.07.15 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.15 00:00:00
     79. NextThursday(t) = 2026.07.16 00:00:00
     80. NextFriday(t) = 2026.07.17 00:00:00
     81. NextSaturday(t) = 2026.07.18 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.07 00:00:00
     85. PreviousWednesday(t) = 2026.07.08 00:00:00
     86. PreviousThursday(t) = 2026.07.09 00:00:00
     87. PreviousFriday(t) = 2026.07.10 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. AddSeconds(t, 30) = 2026.07.14 14:54:13
     92. AddMinutes(t, 30) = 2026.07.14 15:23:43
     93. AddHours(t, 2) = 2026.07.14 16:53:43
     94. AddDays(t, 10) = 2026.07.24 14:53:43
     95. AddBusinessDays(t, 10) = 2026.07.28 14:53:43
     96. AddWeeks(t, 4) = 2026.08.11 14:53:43
     97. AddMonths(t, 2) = 2026.09.14 14:53:43
     98. AddQuarters(t, 3) = 2027.04.14 14:53:43
     99. AddYears(t, 5) = 2031.07.14 14:53:43
     100. SubSeconds(t, 30) = 2026.07.14 14:53:13
     101. SubMinutes(t, 30) = 2026.07.14 14:23:43
     102. SubHours(t, 2) = 2026.07.14 12:53:43
     103. SubDays(t, 10) = 2026.07.04 14:53:43
     104. SubBusinessDays(t, 10) = 2026.06.30 14:53:43
     105. SubWeeks(t, 4) = 2026.06.16 14:53:43
     106. SubMonths(t, 2) = 2026.05.14 14:53:43
     107. SubQuarters(t, 3) = 2025.10.14 14:53:43
     108. SubYears(t, 5) = 2021.07.14 14:53:43
     109. DifferenceInCalendarDays( StartOfYear(t), EndOfYear(t)) = 364
     110. DifferenceInCalendarWeeks( StartOfYear(t), EndOfYear(t)) = 52
     111. DifferenceInCalendarMonths( StartOfYear(t), EndOfYear(t)) = 11
     112. DifferenceInCalendarQuarters( StartOfYear(t), EndOfYear(t)) = 3
     113. DifferenceInCalendarYears( StartOfYear(t), EndOfYear(t)) = 0
     114. DifferenceInSeconds( t, EndOfDay(t)) = 32776
     115. DifferenceInMinutes( t, EndOfDay(t)) = 546
     116. DifferenceInHours( t, EndOfDay(t)) = 9
     117. DifferenceInDays( StartOfYear(t), EndOfYear(t)) = 364
     118. DifferenceInWeeks( StartOfYear(t), EndOfYear(t)) = 52
     119. DifferenceInMonths( StartOfYear(t), EndOfYear(t)) = 11
     120. DifferenceInQuarters( StartOfYear(t), EndOfYear(t)) = 3
     121. DifferenceInYears( StartOfYear(t), EndOfYear(t)) = 0
     122. DifferenceInBusinessDays( StartOfYear(t), EndOfYear(t)) = 260
     123. DifferenceInWeekendDays( StartOfYear(t), EndOfYear(t)) = 104
     124. CalendarDifferenceToString( SubDays(t,666), t ) = 1 year 9 months 28 days
     125. IsFirstDayOfMonth(t) = false
     126. IsLastDayOfMonth(t) = false
     127. IsWeekend(t) = false
     128. IsWeekday(t) = true
     129. IsSunday(t) = false
     130. IsMonday(t) = false
     131. IsTuesday(t) = true
     132. IsWednesday(t) = false
     133. IsThursday(t) = false
     134. IsFriday(t) = false
     135. IsSaturday(t) = false
     136. IsSameMinute(t, AddHours(t, 25)) = false
     137. IsSameHour(t, AddHours(t, 25)) = false
     138. IsSameDay(t, AddHours(t, 25)) = false
     139. IsSameWeek(t, AddHours(t, 25)) = true
     140. IsSameMonth(t, AddHours(t, 25)) = true
     141. IsSameQuarter(t, AddHours(t, 25)) = true
     142. IsSameYear(t, AddHours(t, 25)) = true
     143. IsCurrentMinute(t) = false
     144. IsCurrentHour(t) = false
     145. IsCurrentWeek(t) = true
     146. IsCurrentMonth(t) = true
     147. IsCurrentQuarter(t) = true
     148. IsCurrentYear(t) = true
     149. IsToday(t) = true
     150. IsTomorrow(t) = false
     151. IsYesterday(t) = false
     152. IsLeapYear(2100) = false
     153. DaysInYear(2100) = 365
     154. DaysInMonth(2024, 2) = 29
     155. WeeksInYear(2026) = 53
     156. WeeksInMonth(2026, 8) = 6
     157. TimeToString(t) = 2026.07.14 14:53
     158. t2s(t) = Tue, 2026.07.14 14:53
     159. TimeFormat(t) = 2026.07.14 14:53
     160. TimeFormat(t, DDD, YYYY.MM.DD hh:mm:ss) = Tue, 2026.07.14 14:53:43
     161. TimeFormat(t, DDDD, D MMM YYYY, HH:mm a) = Tuesday, 14 Jul 2026, 02:53 pm
     162. SecondsToString( SecsElapsedOfDay(t) ) = 14:53:43
    
    */
    

    Updates:

    2024.12.03 - v.1.10 : Initial release.

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

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

    2024.12.09 - v.1.25 : Optimized calculations in TimeToStructFast() function.

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

    2024.12.15 - v.1.35 : Optimized GetNthWeekdayInYearMonth() function.

    2024.12.18 - v.1.40 : Added IsCurrentXXX(), IsToday(), IsTomorrow() and IsYesterday() functions.

    2024.12.19 - v.1.45 : Optimized AddMonths() and GetNthWeekdayInYearMonth() functions.

    2024.12.20 - v.1.50 : More code clean-up.

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

    2026.06.22 - v.1.60 : Optimized AddBusinessDays() and DifferenceInBusinessDays() functions. Added various IsXXX() check functions.

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

    2026.07.07 - v.2.10 : Added DaysInYear(), LastDayOf(), MinuteOf(), HourOf() and CalendarDifference() functions.

    2026.07.10 - v.2.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 - v.2.35 : Updated CalendarDifferenceToString() to return the precise elapsed time, including hours, minutes, and seconds.

    2026.07.14 - v.2.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 - v.2.45 : Added CalendarDifference() to return the calendar difference between two dates, broken down into years, months, days, hours, minutes, and seconds.


    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