Hour()

 

Hi Everyone,, hope all's well. Been working on moving code from mql4 to mql5 but having a slight issue specifying for when variables are to be reverted back to false at the start of the day.

Here's the mql4 code to revert bool variables CAN_BUY and CAN_SELL back to false,,

 if(Hour() < 1 )
     {    
        if(CAN_BUY==true||CAN_SELL==true)    
          {    
                CAN_BUY=false; 
                
                // 
                CAN_SELL=false;   
                   
          }     
     }    

Any help will be highly appreciated.If possible to revert variables during first 30 minutes of the day,,,every day. Thanks folks!!

 
Kenneth Njuguna:

Hi Everyone,, hope all's well. Been working on moving code from mql4 to mql5 but having a slight issue specifying for when variables are to be reverted back to false at the start of the day.

Here's the mql4 code to revert bool variables CAN_BUY and CAN_SELL back to false,,

Any help will be highly appreciated.If possible to revert variables during first 30 minutes of the day,,,every day. Thanks folks!!

If you use hour to test then assuming this is in OnTick you will be resetting for a whole hour every tick.

Better to check the day or the week  or the time of the latest daily bar say and compare against a variable say lastdate if not the same:

set lastdate to the date and set CAN_BUY and CAN_SELL to false  no need to check what they are already if your desired outcome is false.

The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
The idea of ​​automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
 
Paul Anscombe:

If you use hour to test then assuming this is in OnTick you will be resetting for a whole hour every tick.

Better to check the day or the week  or the time of the latest daily bar say and compare against a variable say lastdate if not the same:

set lastdate to the date and set CAN_BUY and CAN_SELL to false  no need to check what they are already if your desired outcome is false.

Hey Paul,,, first off thanks,,, appreciated plenty. As the EA sends alerts during the day,,, CAN_BUY and CAN_SELL are altered to true. I need to reset back to false withing the first hour of the day, whether Monday, Wednesday or any trading day for that matter. Then after the 1st hour i await alerts as CAN_BUY and CAN_SELL are then false. Yes, its OnTick,, or how better can i do it??? Thanksagain>

 
 MqlDateTime time;

 TimeToStruct(TimeCurrent(),time);
 int Year         = time.year;        // Year
 int Month        = time.mon;         // Month
 int Day_of_Month = time.day;         // Day
 int Hour         = time.hour;        // Hour
 int Minutes      = time.min;         // Minutes
 int Seconds      = time.sec;         // Seconds
 int Day_of_Week  = time.day_of_week; // Day of week (0-Sunday, 1-Monday, ... ,6-Saturday)
 int Day_of_Year  = time.day_of_year; // Day number of the year (January 1st is assigned the number value of zero)
 
Kenneth Njuguna:

Hey Paul,,, first off thanks,,, appreciated plenty. As the EA sends alerts during the day,,, CAN_BUY and CAN_SELL are altered to true. I need to reset back to false withing the first hour of the day, whether Monday, Wednesday or any trading day for that matter. Then after the 1st hour i await alerts as CAN_BUY and CAN_SELL are then false. Yes, its OnTick,, or how better can i do it??? Thanksagain>

You have solution given by Marco if you intend to work with specific hour/Minute etc. Here is your implementation:

if(Hour ==0 &&  Minutes <=30 ) //First 30 Min of day  
//--do reset


Another typical way is to check if you are on new day as suggested by Paul. Its good for VPS and you need set lastdate to Global var or save to file  to avoid skipping reset. Sample:

bool NewDay()
  {
   static datetime last_date = 0; 
   if(last_date <iTime(NULL,PERIOD_D1,0))
     {
      last_date = iTime(NULL,PERIOD_D1,0);
      return true;
     }
   return false;
  }
//+------------------------------------------------------------------+


 if(NewDay()) 
   //--do reset
 
Marco vd Heijden:

Much appreciated Marco!!!! Thanks!

 
Langat Naftali:

You have solution given by Marco if you intend to work with specific hour/Minute etc. Here is your implementation:


Another typical way is to check if you are on new day as suggested by Paul. Its good for VPS and you need set lastdate to Global var or save to file  to avoid skipping reset. Sample:

Thank you much Langat Naftali!!!

Reason: