Libraries: Local Timezones and Local Session Hours - page 5

 

Update 26 October 2024 - version 1.95

Added two new static methods: TimeGMTOffset() and TimeDaylightSavings().

Renamed the HistoryBrokerOffset() method to TimeServerGMTOffset().

 

Update 28 October 2024 - version 1.97

Converted all macros for dealing with time to functions to avoid double evaluation of parameters inside macro body. More code clean-up in other lines.

 

Update 1 November 2024 - version 1.99

Added an option to switch off the default loading of Gold symbol for estimation of the server's TZ/DST.

Call CTimeZoneInfo::SetUsingGoldSymbol() with 'false' to use the current chart's symbol, instead.

 
   Print("MustBeZero? "+(int)(nyc.BeginLocalTime()-nyc.TimeGMTOffset()-TimeGMT()));
 

This looks a lot cleaner than using an int to set time.


input string    LocalOpenHourSydney     = "9:0-17:0";
input string    LocalOpenHourTokyo      = "8:0-17:0";
input string    LocalOpenHourFrankfurt  = "8:0-17:0";
input string    LocalOpenHourLondon     = "8:00-16:30";
input string    LocalOpenHourNewYork    = "9:30-16:0";

int startHours, startMinutes, endHours, endMinutes, startInt, endInt;

   ConvertTimeRangeToInt(LocalOpenHourSydney, startHours, startMinutes, endHours, endMinutes, startInt, endInt);
   syd.BeginLocalTime(startHours, startMinutes);
   syd.EndLocalTime(endHours, endMinutes);

// Function to convert double time to integer representation and output hours and minutes
int ConvertTimeToInt(double time, int &hours, int &minutes)
  {
// Extract the hours and minutes
   hours = (int)time;
   minutes = (int)((time - hours) * 100);

// Check for valid hour and minute values
   if(hours > 23 || hours < 0)
     {
      Print("Error: Invalid hour value. The value should be between 0 and 23.");
      return -1; // Indicate an error
     }

   if(minutes > 59 || minutes < 0)
     {
      Print("Error: Invalid minute value. The value should be between 0 and 59.");
      return -1; // Indicate an error
     }

// Combine hours and minutes into a single integer representation
   int timeInt = hours * 100 + minutes;

   return timeInt;
  }

// Function to convert time range string to integer representations
void ConvertTimeRangeToInt(string timeRange, int &startHours, int &startMinutes, int &endHours, int &endMinutes, int &startInt, int &endInt)
  {
// Split the time range string
   string times[];
   StringSplit(timeRange, '-', times);

// Convert start time
   double startTime = StringToDouble(times[0]);
   startInt = ConvertTimeToInt(startTime, startHours, startMinutes);

// Convert end time
   double endTime = StringToDouble(times[1]);
   endInt = ConvertTimeToInt(endTime, endHours, endMinutes);
  }
 
BillionerClub #:

This looks a lot cleaner than using an int to set time.


Nice suggestion, and thanks for the codes.

I know strings looks more human ;), but, I am using int for two reasons:

  • With integers, optimaizations are possible in the strategy tester (start, step, stop values for "int" session hours) to optimize trading a specific session.

  • Working with strings contradicts with the level of performance that won't slow the MT5 terminal. 
PS: the code reminds me of the int parser in compiler codes.
 

Update 13 November 2024 - version 2.00

Improved estimation of online server tz/dst and in the strategy strategy

TimeGMT library is no longer needed, all the functionality is included within TimeZoneInfo.

Constructor accepts place and time parameters.

New public methods for working with data of datetime type.

Improved error handling and debugging support.


 

Update 14 November 2024 - version 2.02

Fixed error ERR_HISTORY_NOT_FOUND (4401) when trying to access the XAUUSD,H1 quotes history by the server timezone object.

 

Update 12 December 2024 - version 2.10

Improved performance of HistoryServerGMTOffset() and HistoryServerDaylightSavings() functions, and other minor code changes.

 

Update 12 January 2025 - version 2.15

Fixed issue in HistoryServerDaylightSavings() of searching for quotes history earlier than the loaded history in the strategy tester.