Time FIlter Not Working MQL4

 

Hi I have tried to implement a time filter to turn off trading during high spread times, my broker is in Greenwich mean time (GMT+3), I want to turn off the ea 1 hour before New York close, and continue to untill 2 hours after the asian open. Basically it does not turn off. Any ideas? Thanks.

//Code//

bool eaRun=false;

void OnTick()

{

if(Hour()>=23 && Hour()<=2)

{

eaRun=false;

}

else

{

//ea code should run otherwise 

}

}

 
CodeBerries:

Hi I have tried to implement a time filter to turn off trading during high spread times, my broker is in Greenwich mean time (GMT+3), I want to turn off the ea 1 hour before New York close, and continue to untill 2 hours after the asian open. Basically it does not turn off. Any ideas? Thanks.

//Code//

bool eaRun=false; // SHOULD BE TRUE 

void OnTick()

{

if(Hour()>=23 || Hour()<=2)

{

eaRun=false; 

}

else

{

//ea code should run otherwise 

}

}

1. Use Code (Alt+S)

2. earun should be true

 
if(Hour()>=23 && Hour()<=2)


There is no number which is at the same time greater than 23 AND less than 2.

You should use logical OR instead, or change the operators.

if(Hour()>=23 || Hour()<=2)
{
   eaRun = false;
}
else
{
  //ea code should run otherwise 
}

 or this:

if(Hour()< 23 && Hour()>2)
{
   eaRun = true;
}
else
{
  //ea code should not run in this case 
}
 
  1. You set the variable to false, but A) never use it, and B) never set it to true.
  2. Simplify
    if(Hour()< 23 && Hour()>2) run();