Code Help, Please Clarify

 

Hi

Can someone please clarify that the coding below is correct?

I want to trade between 1.30 am and 9.30 pm and halt trading for news release between 1.00 pm and 3.00 pm 

Therefore is my coding correct?? will this function correctly??

Any feedback is appreciated............regards Mickey 

 

extern int StartTrade="01:30";    
extern int StopTrade="21:30";
extern int StopTrade_A="13:00";
extern int StartTrade_A="15:00";

int start()
 {
  if((Hour()>=StartTrade && Hour()<=StopTrade) || (Hour()>=StopTrade_A && Hour()<=StartTrade_A))
   {

   }
 }

 

No, it will not work! You are mixing strings (text between quotes), date & times and integers (numbers).

For example, the text "01:30" is string. It has no meaning besides a simple string of text. Therefore you cannot just assign it to an "int" variable which is a number (-2,-1,01,2,3,4, etc).

Also, "Hour()" is a function that returns the Hour, just as the name says. It does not return the time.

In MQL4 time and date are combined into a single data type, so I suggest you at least read the following documentation:

 

OK but would this work?

extern int StartTrade="01:00";    
extern int StopTrade="21:00";
extern int StopTrade_A="13:00";
extern int StartTrade_A="15:00";

int start()
 {
  if((Hour()>=StartTrade && Hour()<=StopTrade) || (Hour()>=StopTrade_A && Hour()<=StartTrade_A))
   {

   }
 }

 

what about this?

extern string StartTrade = "01:30";   

extern string StopTrade  = "21:30"; 

extern string StopTrade_A="13:00";

extern string StartTrade_A="15:00";



datetime now = TimeCurrent();



int start()

 {

  if((now >= StringToTime(StartTrade) && now < StringToTime(StopTrade) || (now >= StringToTime(StopTrade_A) && now < StringToTime(StartTrade_A))

   {



   }

 } 
 
mickeyferrari:

OK but would this work?

No, it will not work! You are still mixing strings (text between quotes), date & times and integers (numbers).

Here is an example code for the correct use of testing hours (using the modern style of MQL4+, which you should strive to use instead of the old style):

#property strict

extern int
   StartHour =  1,    
   StopHour  = 21;

void OnTick()
{
   int CurrentHour = Hour(); // Assigned, just in case it is not buffered and the hour changes between calls
   
   if( ( CurrentHour >= StartHour ) && ( CurrentHour <= StopHour ) )
   {
      // Do Something Here
   }
}
 
mickeyferrari:

what about this?

Still will not work, because as I wrote, the date time data type holds both date and time together and not as separate entities. Your strings will convert as being on January 1st, 1970 which will obviously not match the current date.

Please do me a favour and first read the documentation, that I referenced. Don't try to make "fixes" without really understanding what you are doing first.

 
FMIC:

No, it will not work! You are still mixing strings (text between quotes), date & times and integers (numbers).

Here is an example code for the correct use of testing hours (using the modern style of MQL4+, which you should strive to use instead of the old style):

No it will not work, because the logic is not good. But this code doesn't mix anything :

  if((now >= StringToTime(StartTrade) && now < StringToTime(StopTrade) || (now >= StringToTime(StopTrade_A) && now < StringToTime(StartTrade_A))

It's all datetime.

 

 
FMIC:

Still will not work, because as I wrote, the date time data type holds both date and time together and not as separate entities. Your strings will convert as being on January 1st, 1970 which will obviously not match the current date.

False. It will be converted on today's date+hour like D'2016.04.18 01:30:00' for example.

 

Please do me a favour and first read the documentation, that I referenced. Don't try to make "fixes" without really understanding what you are doing first.

Please do me a favour and first read the documentation, that you referenced. 
 
angevoyageur:

No it will not work, because the logic is not good. But this code doesn't mix anything :

It's all datetime.

 

  1. Why are you quoting me, instead of the OP?
  2. Your code will also not work because, since the sample text " 21:20" does not have a date section, the StringToTime() function will assume the date January 1st, 1970, which will obviously not match the current time.
 
mickeyferrari:

what about this?

  if((now >= StringToTime(StartTrade) && now < StringToTime(StopTrade) || (now >= StringToTime(StopTrade_A) && now < StringToTime(StartTrade_A))

 

The logic is not good as you condition will be true for trading time and news time.

Example if now is  D'2016.04.18 14:00:00' the above if will be true, but it should be false.

PS: Please use SRC button when posting code.

PS2: Your code doesn't compile "'(' - unbalanced right parenthesis ..."

 
FMIC:
  1. Why are you quoting me, instead of the OP?

Because you are given 2 wrong answer already in this. You can't be contradicted ?

 2. Your code will also not work because, since the sample text " 21:20" does not have a date section, the StringToTime() function will assume the date January 1st, 1970, which will obviously not match the current time.

It's not my code. Please read my post and check your claim before repeating false assertions. You are wrong and you probably can't see it because you are not happy to be wrong ?

Calm down and relax. 

Reason: