String comparison

ammer.jens
112

Good morning, I compare times 'hh:mm' in an if-clause as follows:

if("14:35">"11:00") { // ... do something }

I tried a various amount of different times and MQL5 seems to compare it correctly. I am just wondering how the comparison is made in the background? I know of the function StringCompare (StringCompare - String Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5) and was wondering if it does it in the same way?

So, for the example above, it probably first compares the two "1" and then "4" with "1" and then returns true?

The reason for comparing the time strings this way is just more logical for me than putting them into the StringCompare function.

Thank you

Documentation on MQL5: String Functions / StringCompare
Documentation on MQL5: String Functions / StringCompare
  • www.mql5.com
StringCompare - String Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Dominik Christian Egert
2340
Times should be compared by an integer comparison. Especially datetime.

StringCompare uses the number representation of characters and compares their values. Read the doc, the return value is not true/false.

It's about ASCII/Unicode value for representing the corresponding character. Also see StringToCharArray.


ammer.jens
112
ammer.jens  
Dominik Christian Egert #:
Times should be compared by an integer comparison. Especially datetime.

StringCompare uses the number representation of characters and compares their values. Read the doc, the return value is not true/false.

It's about ASCII/Unicode value for representing the corresponding character. Also see StringToCharArray.


Hi and thanks for your comment. I know that you can easily extract the hh and mm from a datetime variable using:

MqlDateTime dt;
TimeToStruct(TimeTradeServer(),dt);
dt.hour;
dt.min;

But I haven't yet figured out how to combine hour and mm to the format of hh:mm to compare it as datetime/integer variables. I know that a datetime object date and time is represented as an integer in seconds elapsed since 1970. However, I do not care about the year/exact date but only about the hh:mm. Thus comparing it as strings by using TimeToString is very convenient. And it seems to work even though it might not be the best solution for comparison

William Roeder
27851
William Roeder  
  1. if("14:35">"11:00") { // ... do something }

    This is always true, as 14 is alphabetically after 11. But you don't want alphabetically, you want numerically. Convert to datetimes and compare.

  2. ammer.jens #: But I haven't yet figured out how to combine hour and mm to the format of hh:mm to compare it as datetime/integer variables.

    Perhaps you should read the manual. StringToTime - Conversion Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

ammer.jens
112
ammer.jens  

hello and thanks for your inputs. Stock trading via Exchange includes pre and after market hours at which I do not want to trade. I changed my code accordingly to compare times using datetime variables as follows:

datetime tradeStart_Stocks=StringToTime(TimeToString(TimeTradeServer(),TIME_DATE)+" 14:35");
datetime tradeEnd_Stocks=StringToTime(TimeToString(TimeTradeServer(),TIME_DATE)+" 20:55");

if(TimeTradeServer()>tradeStart_Stocks && TimeTradeServer()<tradeEnd_Stocks) return true;

Is this a correct way of doing it as you have suggested? 

Dominik Christian Egert
2340
Since you only want to compare the hour and minute component of time, you will have to use the Mql time struct datatype variable to do the comparison.

The current version you are showing will compare absolute time values.
ammer.jens
112
ammer.jens  

Hi Dominik, it is not clear to me why this won't work. I mean the absolut time values?

As said in an comment above I know how to extract hour and mm from MqlDateTime. However then I have to compare hour and min separately: dt_defined.hour>dt_server.hour and the same for the minutes. William pointed me to the datetime variable but if I combine the hours and minutes together into a datetime variable again it automatically adds the year again. So this is somehow going round in circles. But probably I am not educated well enough in mql5 to 
not see the forest for the trees? So maybe you are willing to give me a small example how you would do it? thank you! 

William Roeder
27851
William Roeder  
Dominik Christian Egert #: you will have to use the Mql time struct datatype variable to do the comparison.

Or just extract the time portion and compare.

#define HR1435 52500 // 14:35
#define HR2055 75300 // 20:55
int TOD = time( TimeTradeServer() );
return HR1435 <= TOD && TOD < HR2055;
          date/time (2017)
          Find bar of the same time one day ago - MQL4 programming forum (2017)

When dealing with time, a lot of people use strings; they can not be optimized. Using (int) seconds or (double) hours, or (int) hours and minutes can be.

See also Dealing with Time (Part 1): The Basics - MQL5 Articles (2021.10.01)
Dealing with Time (Part 2): The Functions - MQL5 Articles (2021.10.08)

Dominik Christian Egert
2340

I would say, william showed a good way to do it.

Here is what I meant:

sinput int start_hr = 14;
sinput int start_mn = 35;
sinput int stop_hr = 20;
sinput int stop_mn = 55;

bool check_time(const int _start_hr, const int _start_mn, const int _stop_hr, const int _stop_mn)
{
        MqlDateTime curr_tm = {};
        TimeTradeServer(curr_tm);

        return(    (curr_tm.hour >= _start_hr)
                && (curr_tm.min >= _start_mn)
                && (curr_tm.hour < _stop_hr)
                && (curr_tm.min > _stop_mn)
        );
}


void OnTick()
{
        // Check for trading times
        if(!check_time(start_hr, start_mn, stop_hr, stop_mn))
        { return; }

        // Do trading stuff...

        // Return
        return;
}
ammer.jens
112
ammer.jens  

Thank you William for your suggested solution!


1. Hoever, is

time ( .... )

MQL4 as this function does not exist in MQL5? What would be the MQL5 pendant?

2.Nevertheless, I would still like to understand/know if my provided solution would be correct or if it might lead to any false results?

William Roeder
27851
William Roeder  
Dominik Christian Egert #: I would say, william showed a good way to do it.

Here is what I meant:

        return(    (curr_tm.hour >= _start_hr)
                && (curr_tm.min >= _start_mn)
                && (curr_tm.hour < _stop_hr)
                && (curr_tm.min > _stop_mn)

This code will not work. It will not allow 15:00 … 15:34, 16:00 … 16:34, etc.