EA not functioning as it did during backtests

 

Hi All,

Hoping there's something really obvious someone can point out to me.

I have an EA that performs perfectly on backtests, and almost perfect on live trades - except for one key feature.

At 2pm the EA analyses whether the current day price range is within the previous day price range.

That being FALSE (and the current day already broken out before 2pm), it should not allow any trades to occur that day.

The only problem I am getting, is this key feature isn't operating correctly on live trades.  So when 1400 comes around for the current day : it will instantly short (if day has broken lower than previous) or long (if broken higher) at 2pm, rather than stopping trades from occurring.

The code makes logical sense to me, and as stated, has performed perfectly on backtests without this issue.

Hope you can help me!

Here is the OnTick function -

void OnTick()
{  

   CheckTime();      

   if (tradeTime = True)
   {
      if (DayValid == True)
      {
         if (OrdersTotal() == 0)
         {
            double currentclose = iClose(_Symbol,PERIOD_D1,0),
                   previoushigh = iHigh(_Symbol,PERIOD_D1,1),
                   previouslow  = iLow(_Symbol,PERIOD_D1,1);
                   
            if(currentclose>previoushigh)
            {
               if (buyLock == False)
               {
                  BuyMarket();
               }   
            }   
            
            if(currentclose<previouslow)
            {
               if (sellLock == False)
               {
                  SellMarket();
               }   
            }        
         }   
      }
   }        
}


And here is the CheckTime() function -


void CheckTime()
{
   if((Hour()== SessionStart) && (reset == False)) 
   {
      tradeTime = True;
      reset = True;
      
      buyLock = False;
      sellLock = False;
      
      double CurrentLow = iLow(_Symbol, PERIOD_D1, 0);
      double CurrentHigh = iHigh(_Symbol, PERIOD_D1, 0);
      
      double PrevDayHigh = iHigh(_Symbol, PERIOD_D1, 1);
      double PrevDayLow = iLow(_Symbol, PERIOD_D1, 1);
      
      if (CurrentLow > PrevDayLow && CurrentHigh < PrevDayHigh)
      {
         DayValid = True;
      }
      else
      {
         DayValid = False;
      }               
   }           

   if(Hour() == SessionClose && reset == True)
   {
      tradeTime = False;
      reset = False;
      
      buyLock = True;
      sellLock = True;
      
      return;
   }
}

Variables for SessionStart and SessionClose are 14 and 23 respectively, set on initialisation.

 
and all other variables are set as extern's!
 
Maybe you did take it into account, maybe not: your code for CheckTime() will work at any time between 14:00:00 and 14:59:59, same for 23:00 ~ 24:00. This is because you check only the hour, not the minutes and seconds. Is this what is intended, or do you want it to run only once?
 
WindmillMQL:
Maybe you did take it into account, maybe not: your code for CheckTime() will work at any time between 14:00:00 and 14:59:59, same for 23:00 ~ 24:00. This is because you check only the hour, not the minutes and seconds. Is this what is intended, or do you want it to run only once?

Hi Windmil,

Yeah that's understood and intended - it should then set the variable tradeTime = True // False at those hours you've stated. (Using the variable 'reset' to ensure this happens only once at the start of those hours).

Then as the OnTick() goes through the hours in between it should have 'the green light' to trade (tradeTime = True) if the other conditions are met (the price break out).

It only stopped functioning correctly (after three months of forward testing) once I connected to a VPS.

 
I know that it is not obvious, but topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I will move your topic to the MQL4 and Metatrader 4 section.
 

On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
          Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4 2019.05.20

Reason: