Guarda come scaricare robot di trading gratuitamente
Ci trovi su Twitter!
Unisciti alla nostra fan page
Script interessante?
Pubblica il link!
lasciare che altri lo valutino
Ti è piaciuto lo script? Provalo nel Terminale MetaTrader 5
Librerie

High-Performance Time Functions (TimeUtils) - libreria per MetaTrader 5

Visualizzazioni:
90
Valutazioni:
(14)
Pubblicato:
Freelance MQL5 Hai bisogno di un robot o indicatore basato su questo codice? Ordinalo su Freelance Vai a Freelance

Questa libreria contiene più di 80 funzioni diverse per gestire le variabili temporali. L'obiettivo principale è quello di fornire funzioni temporali ad alte prestazioni. La modalità performance (che può essere controllata in fase di compilazione tramite una #define) è disabilitata per impostazione predefinita. Questa modalità non è un requisito per includere la libreria nei progetti, poiché può essere inclusa normalmente anche senza.


TIMEUTILS_PERFORMANCE_MODE

Opzionalmente, la modalità performance può essere attivata in fase di compilazione tramite #define prima di #include:

// abilita la modalità performance per la libreria
#define  TIMEUTILS_PERFORMANCE_MODE
#include "TimeUtils.mqh"

Questo reindirizzerà tutte le chiamate alle funzioni integrate di MQL TimeToStruct e StructToTime verso alternative più efficienti.

È possibile compilare lo script "performance_mode.mq5" con e senza TIMEUTILS_PERFORMANCE_MODE per verificare la differenza di velocità sulla propria macchina. Ciò sarà utile per i programmi ad alte prestazioni che eseguono compiti pesanti o molti legati al tempo (ad esempio, la scansione di tutta la cronologia delle quotazioni per le barre H1 all'inizio delle settimane di trading o la raccolta di altre statistiche).


Elenco di tutte le funzioni della libreria:

I nomi delle funzioni dovrebbero essere autoesplicativi; inoltre, è possibile trovare una breve descrizione di una funzione specifica nel file TimeUtils.mqh.

//+==================================================================+
//| Creare datetime da componenti|
//+==================================================================+
datetime CreateDateTime(
   const int year,           // Anno
   const int mon,            // Mese
   const int day,            // Giorno
   const int hour = 0,       // Ora
   const int min = 0,        // Minuti
   const int sec = 0         // Secondi
   )
datetime CreateDateTime(MqlDateTime&  dt_struct);  // alternativa veloce a StructToTime()

//+==================================================================+
//| Interrompere datetime in Componenti|
//+==================================================================+
bool TimeToStructFast(
   datetime      dt,         // Valore della data da convertire
   MqlDateTime&  dt_struct   // struttura per l'adozione dei valori
   )

//+==================================================================+
//| Estrarre i componenti di datetime: Domenica, yyyy.mm.dd hh:mm:ss |
//| Unità Get()|
//+==================================================================+
int GetSecond(datetime t)
int GetMinute(datetime t)
int GetHour(datetime t)
int GetDay(datetime t)
int GetMonth(datetime t)
int GetYear(datetime t)

//+==================================================================+
//| Giorno() Numero|
//+==================================================================+
int DayOfWeek(datetime t)
int DayOfYear(datetime t)
int DayIndex(datetime t)

//+==================================================================+
//| Settimana() Numero|
//+==================================================================+
int WeekOfMonth(const datetime t, bool StartsOnMonday = false)
int WeekOfYear(const datetime t, bool StartsOnMonday = false)
int WeekIndex(datetime t, bool StartsOnMonday = false)

//+==================================================================+
//| Unità StartOf()|
//+==================================================================+
datetime StartOfMinute(datetime t)
datetime StartOfHour(datetime t)
datetime StartOfDay(datetime t)
datetime StartOfWeek(datetime t, bool StartsOnMonday = false)
datetime StartOfMonth(datetime t)
datetime StartOfYear(datetime t)

//+==================================================================+
//| Unità EndOf()|
//+==================================================================+
datetime EndOfMinute(datetime t)
datetime EndOfHour(datetime t)
datetime EndOfDay(datetime t)
datetime EndOfWeek(datetime t, bool StartsOnMonday = false)
datetime EndOfMonth(datetime t)
datetime EndOfYear(datetime t)

//+==================================================================+
//| SecsElapsedOf() Units|
//+==================================================================+
int SecsElapsedOfMinute(datetime t)
int SecsElapsedOfHour(datetime t)
int SecsElapsedOfDay(datetime t)
int SecsElapsedOfWeek(datetime t, bool StartsOnMonday = false)
int SecsElapsedOfMonth(datetime t)
int SecsElapsedOfYear(datetime t)

//+==================================================================+
//| RoundTo() / Nearest() Unità|
//+==================================================================+
datetime RoundToMinute(datetime t)
datetime RoundToHour(datetime t)
datetime RoundToDay(datetime t)
datetime RoundToWeek(datetime t, bool StartsOnMonday = false)

//+==================================================================+
//| Unità CeilTo() / Next()|
//+==================================================================+
datetime CeilToMinute(datetime t)
datetime CeilToHour(datetime t)
datetime CeilToDay(datetime t)
datetime CeilToWeek(datetime t, bool StartsOnMonday = false)

//+==================================================================+
//| Prossimo() Giorno della settimana|
//+==================================================================+
datetime NextWeekday(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)

//+==================================================================+
//| Precedente() Giorno della settimana|
//+==================================================================+
datetime PreviousWeekday(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() Giorno della settimana del mese|
//+==================================================================+
datetime FirstWeekdayOfTheMonth(datetime t, ENUM_DAY_OF_WEEK weekday = SUNDAY)
datetime LastWeekdayOfTheMonth(datetime t, ENUM_DAY_OF_WEEK weekday = SUNDAY)
datetime NthWeekdayOfTheMonth(datetime t, int Nth, ENUM_DAY_OF_WEEK weekday = SUNDAY)

//+==================================================================+
//| Unità Add()|
//+==================================================================+
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 AddYears(datetime t, int amount)

//+==================================================================+
//| Unità Sub()|
//+==================================================================+
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 SubYears(datetime t, int amount)

//+==================================================================+
//| Unità DifferenceIn()|
//+==================================================================+
int DifferenceInCalendarDays(datetime beginTime, datetime endTime)
int DifferenceInBusinessDays(datetime beginTime, datetime endTime)
int DifferenceInCalendarWeeks(datetime beginTime, datetime endTime, bool StartsOnMonday = false)
int DifferenceInCalendarMonths(datetime beginTime, datetime endTime)

//+==================================================================+
//| IsSame() Unità|
//+==================================================================+
bool IsSameMinute(datetime t1, datetime t2)
bool IsSameHour(datetime t1, datetime t2)
bool IsSameDay(datetime t1, datetime t2)
bool IsSameWeek(datetime t1, datetime t2, bool StartsOnMonday = false)
bool IsSameMonth(datetime t1, datetime t2)
bool IsSameYear(datetime t1, datetime t2)

//+==================================================================+
//| Unità IsCurrent()|
//+==================================================================+
bool IsCurrentMinute(datetime t)
bool IsCurrentHour(datetime t)
bool IsCurrentWeek(datetime t, bool StartsOnMonday = false)
bool IsCurrentMonth(datetime t)
bool IsCurrentYear(datetime t)
bool IsToday(datetime t)
bool IsTomorrow(datetime t)
bool IsYesterday(datetime t)

//+==================================================================+
//| Misc.|
//+==================================================================+
bool IsLeapYear(int year)
int  DaysInMonth(int year, int month)
datetime GetNthWeekdayInYearMonth(iYear, iMonth, Nth, weekday = SUNDAY)
datetime GetNthSundayInYearMonth(iYear, iMonth, Nth)

//+==================================================================+
//| Formattazione del tempo in stringa|
//+==================================================================+
string t2s(datetime t, const int mode = TIME_DATE | TIME_MINUTES)
string SecondsToString(int seconds)
string TimeFormat(datetime t, string format = "YYYY.MM.DD hh:mm")


Elenco di tutti i formati temporali disponibili:

//+------------------------------------------------------------------+
//| Ottenere l'ora formattata in base alla stringa di token passata. |
//| Elenco di tutti i formati disponibili:|
//| Formato Descrizione dell'uscita|
//| ------ ---------------- ------------------------------------- |
//| YY 18Anno a due cifre
//| YYYY 2018 Anno a quattro cifre|
//| M 1-12Il mese, a partire da 1 |
//| MM 01-12 Il mese, a 2 cifre.
//| MMM gen-dic Il nome abbreviato del mese, 3 lettere |
//| MMMM gennaio-dicembre Il nome completo del mese|
//| D 1-31Il giorno del mese |
//| DD 01-31Il giorno del mese, a 2 cifre.
//| DDD Sun-Sat Il nome breve del giorno della settimana |
//| DDDD domenica-sabato Il nome del giorno della settimana |
//| h 0-23 L'ora|
//| hh 00-23 L'ora, a 2 cifre
//| H 1-12L'ora, orologio a 12 ore |
//| HH 01-12 L'ora, orologio a 12 ore, 2 cifre.
//| m 0-59 Il minuto|
//| mm 00-59 Il minuto, a 2 cifre
//| s 0-59 Il secondo|
//| ss 00-59 Il secondo, a 2 cifre |
//| A AM PM|
//| a am pm|
//+------------------------------------------------------------------+
//| Formati di esempio:|
//| "YYYY.MM.DD hh:mm" // "2024.12.08 22:05" (predefinito) |
//| "DDD, YYYY.MM.DD hh:mm:ss" // "Sun, 2024.12.08 22:05:21" |
//| "D MMMM YYYY, HH:mm a" // "8 dicembre 2024, ore 22:05" |
//| "GG/MM/AAAA" // "08/12/2024"|
//+------------------------------------------------------------------+
string TimeFormat(const datetime t, const string format = "YYYY.MM.DD hh:mm");


I due script allegati "basic.mq5" e "advanced.mq5" mostrano gli esempi di utilizzo di base e avanzato.

Un esempio di output dallo script "advanced.mq5":

/*
 esempio di output:

 1. CreateDateTime(2022, 03, 25) = 2022.03.25 00:00:00
 [anno] [lun] [giorno] [ora] [min] [sec] [giorno_settimana] [giorno_anno]
 [0] 2024 12 18 17 27 25 3 352
 2. t2s(t, TIME_DATE|TIME_SECONDS) = Wed, 2024.12.18 18:27:25
 3. GetYear(t) = 2024
 4. GetMonth(t) = 2024 5. GetMonth(t) = 2024  GetMonth(t) = 12
 5. GetDay(t) = 18
 6. GetHour(t) = 18
 7. GetMinute(t) = 18 7. GetMinute(t) = 27
 8. GetSecond(t) = 25
 9. DayOfWeek(t) = 3
 10. DayOfYear(t) = 352
 11. DayIndex(t) = 352 12. DayIndex(t) = 352  DayIndex(t) = 20075
 12. WeekOfMonth(t) = 3
 13. WeekOfYear(t) = 51
 14. WeekIndex(t) = 2868
 15. WeekOfMonth(t, true) = 4
 16. WeekOfYear(t, true) = 4  WeekOfYear(t, true) = 51
 17. WeekIndex(t, true) = 2868  WeekIndex(t, true) = 2868
 18. StartOfMinute(t) = 2024.12.18 18:27:00
 19. StartOfHour(t) = 2024.12.18 18:00:00
 20. StartOfDay(t) = 2024.12.18 00:00:00
 21. StartOfWeek(t) = 2024.12.15 00:00:00
 22. StartOfWeek(t, true) = 2024.12.16 00:00:00
 23. StartOfMonth(t) = 2024.12.01 00:00:00
 24. StartOfYear(t) = 2024.01.01 00:00:00
 25. EndOfMinute(t) = 2024.12.18 18:27:59
 26. EndOfHour(t) = 2024.12.18 18:59:59
 27. EndOfDay(t) = 2024.12.18 23:59:59
 28. EndOfWeek(t) = 2024.12.18 23:59:59  EndOfWeek(t) = 2024.12.21 23:59:59
 29. EndOfWeek(t, true) = 2024.12.22 23:59:59
 30. EndOfMonth(t) = 2024.12.31 23:59:59
 31. EndOfYear(t) = 2024.12.31 23:59:59
 32. SecsElapsedOfMinute(t) = 25
 33. SecsElapsedOfHour(t) = 1645
 34. SecsElapsedOfDay(t) = 66445
 35. SecsElapsedOfWeek(t) = 325645
 36. SecsElapsedOfWeek(t) = 325645  SecsElapsedOfWeek(t, true) = 239245
 37. SecsElapsedOfWeek(t, true) = 239245  SecsElapsedOfMonth(t) = 1535245
 38. SecsElapsedOfYear(t) = 30479245
 39. RoundToMinute(t) = 2024.12.18 18:27:00
 40. RoundToHour(t) = 2024.12.18 18:00:00
 41. RoundToDay(t) = 2024.12.19 00:00:00
 42. RoundToWeek(t) = 2024.12.22 00:00:00
 43. RoundToWeek(t, true) = 2024.12.16 00:00:00
 44. CeilToMinute(t) = 2024.12.18 18:28:00
 45. CeilToHour(t) = 2024.12.18 19:00:00
 46. CeilToDay(t) = 2024.12.19 00:00:00
 47. CeilToWeek(t) = 2024.12.22 00:00:00
 48. CeilToWeek(t, true) = 2024.12.23 00:00:00
 49. NextSunday(t, true) = 2024.12.22 00:00:00 49. NextSunday(t) = 2024.12.22 00:00:00
 50. NextMonday(t) = 2024.12.23 00:00:00
 51. NextTuesday(t) = 2024.12.24 00:00:00
 52. NextWednesday(t) = 2024.12.25 00:00:00
 53. NextThursday(t) = 2024.12.19 00:00:00
 54. NextFriday(t) = 2024.12.20 00:00:00
 55. NextSaturday(t) = 2024.12.21 00:00:00
 56. Domenica precedente(t) = 2024.12.15 00:00:00
 57. Lunedì precedente(t) = 2024.12.16 00:00:00
 58. Martedì precedente(t) = 2024.12.17 00:00:00
 59. Mercoledì precedente(t) = 2024.12.11 00:00:00
 60. PrecedenteGiovedì(t) = 2024.12.12 00:00:00
 61. PrecedenteVenerdì(t) = 2024.12.12 00:00:00 61. PreviousFriday(t) = 2024.12.13 00:00:00
 62. PreviousSaturday(t) = 2024.12.14 00:00:00
 63. FirstWeekdayOfTheMonth(t, SUNDAY) = 2024.12.01 00:00:00
 64. LastWeekdayOfTheMonth(t, SUNDAY) = 2024.12.01 00:00:00  LastWeekdayOfTheMonth(t, SUNDAY) = 2024.12.29 00:00:00
 65. AddSeconds(t, 30) = 2024.12.18 18:27:55
 66. AddMinutes(t, 30) = 2024.12.18 18:57:25
 67. AggiungiOre(t, 2) = 2024.12.18 20:27:25
 68. AddDays(t, 10) = 2024.12.28 18:27:25
 69. AddBusinessDays(t, 10) = 2025.01.01 18:27:25
 70. AggiungiSettimane(t, 4) = 2025.01.15 18:27:25
 71. AddMonths(t, 2) = 2025.02.18 18:27:25
 72. AddYears(t, 5) = 2029.12.18 18:27:25
 73. SubSecondi(t, 30) = 2024.12.18 18:26:55
 74. SubMinuti(t, 30) = 2024.12.18 17:57:25
 75. SubOre(t, 2) = 2024.12.18 16:27:25
 76. SottoGiorni(t, 10) = 2024.12.08 18:27:25
 77. SubBusinessDays(t, 10) = 2024.12.04 18:27:25
 78. SubSettimane(t, 4) = 2024.11.20 18:27:25
 79. SubMonths(t, 2) = 2024.10.18 18:27:25
 80. SubYears(t, 5) = 2019.12.18 18:27:25
 81. DifferenceInCalendarDays(t, AddWeeks(t, 9)) = 63
 82. DifferenzaInBusinessDays(t, AddWeeks(t, 9)) = 45
 83. DifferenceInCalendarWeeks(t, AddWeeks(t, 9)) = 9
 84. DifferenzaInCalendarWeeks(t, AddWeeks(t, 9), true) = 9
 85. DifferenceInCalendarMonths(t, AddWeeks(t, 9)) = 2
 86. IsSameMinute(t, AddWeeks(t, 9), true) = 9 . IsSameMinute(t, AddHours(t, 25)) = false
 87. IsSameHour(t, AddHours(t, 25)) = false
 88. IsSameDay(t, AddHours(t, 25)) = false
 89. IsSameWeek(t, AddHours(t, 25)) = false  IsSameWeek(t, AddHours(t, 25)) = vero
 90. IsSameWeek(t, AddHours(t, 25), true) = true
 91. IsSameMonth(t, AddHours(t, 25)) = true  IsSameMonth(t, AddHours(t, 25)) = true
 92. IsSameYear(t, t, 25)) = true  IsSameYear(t, AddHours(t, 25)) = vero
 93. IsCurrentMinute(t) = false
 94. IsCurrentHour(t) = falso
 95. IsCurrentWeek(t) = true
 96. IsCurrentWeek(t, true) = vero
 97. IsCurrentMonth(t) = true
 98. IsCurrentYear(t) = true
 99. IsToday(t) = true
 100. IsTomorrow(t) = true 100. IsCurrentYear(t) = true  IsTomorrow(t) = falso
 101. IsYesterday(t) = falso
 102. IsLeapYear(2100) = falso
 103. DaysInMonth(2024, 2) = 29
 104. GetNthWeekdayInYearMonth(2024, 1, 1, SUNDAY) = 2024.01.07 00:00:00
 105. t2s(TimeCurrent()) = Wed, 2024.12.18 17:27
 106. SecondsToStar(SecsSec. SecondsToString(SecsElapsedOfDay(TimeCurrent()) = 17:27:25
 107. TimeFormat(TimeCurrent(), YYYY.MM.DD hh:mm) = 2024.12.18 17:27
 108. TimeFormat(TimeCurrent(), DDD, YYYY.MM.DD hh:mm:ss) = Wed, 2024.12.18 17:27:25
 109. TimeFormat(TimeCurrent(), D MMM YYYY, HH:mm a) = 18 dic 2024, 05:27 pm

*/


Aggiornamenti:

2024.12.03 - v.1.10 : Rilascio iniziale.

2024.12.05 - v.1.15 : Aggiunte funzioni per i giorni lavorativi e per il giorno Nth() del mese. Ottimizzazione dei calcoli nella funzione DaysInMonth().

2024.12.09 - v.1.20 : Aggiunta la funzione TimeFormat() per formattare l'ora in base alla stringa di token passata.

2024.12.09 - v.1.25 : Ottimizzati i calcoli nella funzione TimeToStructFast().

2024.12.10 - v.1.30 : Ottimizzate le funzioni TimeFormat(), StartOfYear() e EndOfYear(). Aggiornate le descrizioni di alcune funzioni.

2024.12.15 - v.1.35 : Ottimizzata la funzione GetNthWeekdayInYearMonth().

2024.12.18 - v.1.40 : Aggiunte le funzioni IsCurrentXXX(), IsToday(), IsTomorrow() e IsYesterday().

2024.12.19 - v.1.45 : Ottimizzate le funzioni AddMonths() e GetNthWeekdayInYearMonth().

2024.12.20 - v.1.50 : Maggiore pulizia del codice.

2024.12.21 - v.1.55 : Calcoli semplificati in AddMonths(): sostituita la complessa logica di aggiustamento dei mesi e degli anni con un più semplice calcolo dei mesi totali.



Tradotto dall’inglese da MetaQuotes Ltd.
Codice originale https://www.mql5.com/en/code/53970

ScatoleDarvas simmetriche ScatoleDarvas simmetriche

Corridoio simmetrico Darvas per il Forex.

Frattali fini Frattali fini

L'indicatore Fine Fractals mostrerà le curve, i picchi e le depressioni importanti dei prezzi laddove l'indicatore Fractals standard non funziona.

iTrend iTrend

Indicatore combinato di forza e direzione del trend.

ExCandele2 ExCandele2

L'indicatore ExCandles-v2 indica le combinazioni di candele con frecce sul grafico.