Download MetaTrader 5

Code Help, Please Clarify

To add comments, please log in or register
mikegreen
285
mikegreen  

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))
   {

   }
 }

Fernando Carreiro
2202
Fernando Carreiro  

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:

mikegreen
285
mikegreen  

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))
   {

   }
 }

mikegreen
285
mikegreen  

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))

   {



   }

 } 
Fernando Carreiro
2202
Fernando Carreiro  
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
   }
}
Fernando Carreiro
2202
Fernando Carreiro  
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.

Alain Verleyen
Moderator
32030
Alain Verleyen  
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.

 

Alain Verleyen
Moderator
32030
Alain Verleyen  
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. 
Fernando Carreiro
2202
Fernando Carreiro  
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.
Alain Verleyen
Moderator
32030
Alain Verleyen  
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 ..."

Alain Verleyen
Moderator
32030
Alain Verleyen  
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. 

12
To add comments, please log in or register