Converting from mql4 to mql5

 

Good day in Mql4 I can type as follows

if(Year() == 2020) //year
   if(Month() == 2) //Month
      if(Day() == 14) //14 february 2020
         Print ("fappy Vals Day");

now how do I write the above in MQL5. 

I tried 

   datetime date = TimeCurrent();
   MqlDateTime m_time;
   TimeToStruct(date,m_time); 
   if(m_time.year ==2020)
      if(m_time.mon == 2)
        if(m_time.day == 14)
           Print("Happy Valentines");

But its not working

 
The code is right... but let me do a question... how can you say that is not working if you are using a future date?
 
Fabio Cavalloni:
The code is right... but let me do a question... how can you say that is not working if you are using a future date?

because I had set a return INIT_FAILED and threw it into OnINIT 

datetime date = TimeCurrent();
   MqlDateTime m_time;
   TimeToStruct(date,m_time);
   if(m_time.year >=2020)
      if(m_time.mon >= 2)
        if(m_time.day >= 14)
        {
         Comment("It failed :(");
         return INIT_FAILED;
        }
 
Jefferson Metha:

because I had set a return INIT_FAILED and threw it into OnINIT 

I'm not sure that during OnInit the function TimeCurrent() returns a valid time... it return the last valid server time.

You can try to put it into OnTick, using an old date and then try to backtest in a period in which that date is contained.

 
Fabio Cavalloni:

I'm not sure that during OnInit the function TimeCurrent() returns a valid time... it return the last valid server time.

You can try to put it into OnTick, using an old date and then try to backtest in a period in which that date is contained.

It does I had to run 

Comment((string)Timecurrent());

 
Jefferson Metha:

It does I had to run 

Comment((string)Timecurrent());

   if(m_time.year >=2020)
      if(m_time.mon >= 2)
        if(m_time.day >= 14)

This code means that:

the year need to be >= 2020 (it is)

the month need to be >= 2 (it's not)

the day need to be >= 14 (it's not)

 

This script works, so there is something wrong with the date you are sending to your code.

Creating a separate function allows you to test the function easily, by the way.

Unit test all the things.

#property strict

void OnStart()
{
    IsValentinesDay(D'2020.02.14 00:00:00');
}

bool IsValentinesDay(const datetime dateTime)
{
    MqlDateTime dt;
    TimeToStruct(dateTime,dt);
    
    PrintFormat("%d.%d.%d", dt.year, dt.mon, dt.day);
    
    if ( dt.year == 2020 )
    {
        if ( dt.mon == 2 )
        {
            if ( dt.day == 14 )
            {
                Print("Is Valentine's Day");
                return true;
            }
        }
    }
    
    return false;
}
 
Jefferson Metha:

Good day in Mql4 I can type as follows

now how do I write the above in MQL5. 

I tried 

But its not working

void OnStart()
  {
   MqlDateTime m_time;
   TimeToStruct(TimeCurrent(),m_time);
   if(m_time.mon==1 && m_time.day==10) printf("*** today");
  }

To replace with the right month, day & event ; year isn't relevant ...