day and time trading conditions

 

hi everyone,

I'm still a beginner, is my code correct?

trading hours: from 8Am to to 4Pm (8h to 16h)
trading day : Monday to Thursday

int OnInit()
  {
   MqlDateTime myLocalTime;   
   TimeLocal(myLocalTime); 
   
   int startHour = 8;    
   int stopHour  = 16;     
   
   bool C1 =  myLocalTime.hour >= startHour 
            && myLocalTime.hour < stopHour ;    
               
   bool C2 = (myLocalTime.day_of_week   == 1) 
            || (myLocalTime.day_of_week == 2)
            || (myLocalTime.day_of_week == 3)
            || (myLocalTime.day_of_week == 4);            
    
   if (C1 && C2)
   {  // Do somethink
      Print("day : ", myLocalTime.day);
      Print("day of week : ", myLocalTime.day_of_week);
      Print("day of year : ", myLocalTime.day_of_year);
   }else 
      Print("Error");
      
   return(INIT_SUCCEEDED);
  }

Is my code correct for this type of condition, or am I out of my depth?

Thank you in advance for all your insights 😊


Best Reguards,
ZeroCafeine

 
ZeroCafeine: Is my code correct for this type of condition,

Use the debugger or print out your variables, including _LastError and prices. Do you really expect us to debug your code for you?
          Code debugging - Developing programs - MetaEditor Help
          Error Handling and Logging in MQL5 - MQL5 Articles (2015)
          Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
          Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

 

First of all, thank you very much for your reply 😊, I'll also take a look at all the links you've provided

But

Seeing your answer, I realise that I've phrased my question incorrectly. Does my code seem correct in terms of the MQL5 language?
or for my two conditions C1(trading time) and C2(trading days) can you advise me on other ways of writing the code, or is that enough for a beginner like me 😊?

and thank you again @ William Roeder 

 
ZeroCafeine #: Does my code seem correct in terms of the MQL5 language?

or for my two conditions C1(trading time) and C2(trading days) can you advise me on other ways of writing the code, or is that enough for a beginner like me 😊?

It's fine except it allows unlimited trading if, and only if, you start the EA on the correct date/time. Move it to OnTick.

 
William Roeder #:

It's fine except it allows unlimited trading if, and only if, you start the EA on the correct date/time. Move it to OnTick.

ok tks you, and yes we use only this C1 and c2 condition it  allows unlimited trading, I will add other condition, this is just for asking about time condition


what is the result of this line :

Print("day : ", myLocalTime.day);

it's not day of and not day of year, So i don't know what kind of day it is ?

 
ZeroCafeine #:

ok tks you, and yes we use only this C1 and c2 condition it  allows unlimited trading, I will add other condition, this is just for asking about time condition


what is the result of this line :

it's not day of and not day of year, So i don't know what kind of day it is ?

Yes, it's the "day_of_month", shortened to "day", see that example : https://www.mql5.com/fr/docs/constants/structures/mqldatetime

Documentation sur MQL5: Constantes, Enumérations et Structures / Structures de Données / Structure du Type Date
Documentation sur MQL5: Constantes, Enumérations et Structures / Structures de Données / Structure du Type Date
  • www.mql5.com
Structure du Type Date - Structures de Données - Constantes, Enumérations et Structures - Référence MQL5 - Référence sur le langage de trading algorithmique/automatisé pour MetaTrader 5
 
Tks for your answer @Mickael Alain Guy Millerand, 

Where do you see this information on the documentation, I can't see it 🙂 ? Or maybe I'm too old 🤣
 
ZeroCafeine #: yes we use only this C1 and c2 condition it  allows unlimited trading, I will add other condition, this is just for asking about time condition

Read this again:

William Roeder #: It's fine except it allows unlimited trading if, and only if, you start the EA on the correct date/time. Move it to OnTick.

If you do not start the EA on the correct day/time, it allows no trading, ever.

Emphasis on start. Move it to OnTick, to only allow trading on the correct date/time.

 
tks you so much for your light @William Roeder
Reason: