EA open at time error - page 2

 
Keith Watford:

What does that have to do with my post?

Don't use StringToTime().

That would be a rather silly way to make it work.

ok , what is the solution after remove  StringToTime()?

 
Oussama Mansour:

ok , what is the solution after remove  StringToTime()?

You could use something like this

#property strict

input string             OpenTime="01:00";//Time to place trades (HH:MM)

long TradeTimeStartSeconds;
long TradeTimeEndSeconds;
int  Count;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   string temp[];
   Count=StringSplit(OpenTime,':',temp);
   if(Count!=2)
      Alert("Incorrect input for Time to place trades (HH:MM). Please check");
   else
      {
      TradeTimeStartSeconds=StringToInteger(temp[0])*3600 + StringToInteger(temp[1])*60;
      TradeTimeEndSeconds=TradeTimeStartSeconds+PeriodSeconds();
      }
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   if(Count==2)
      {
      long daysSeconds=TimeCurrent()-iTime(_Symbol,PERIOD_D1,0);
      if(daysSeconds>=TradeTimeStartSeconds && daysSeconds<TradeTimeEndSeconds)
         {
         //Check for placing trade
         }
      }
}
//+------------------------------------------------------------------+
Note that this will only work if the start and end time is in the same day. So it will need to be refined if the trade time window passes through midnight.
 
Keith Watford:

You could use something like this

Note that this will only work if the start and end time is in the same day. So it will need to be refined if the trade time window passes through midnight.

so the EA open trades if time is 01:00 even broker time is different of local time ?

 
Oussama Mansour:

so the EA open trades if time is 01:00 even broker time is different of local time ?

The code does not use local time, only platform time.

 
  1. TradeTimeStartSeconds=temp[0])*3600 + temp[1]*60;
    SECONDS now = time();
    if(TradeTimeTimeStart >= now && now < TradeTimeEndSeconds
    Stop using strings and just specify the time
              Find bar of the same time one day ago - MQL4 programming forum 2017.10.06

  2. long daysSeconds=TimeCurrent()-iTime(_Symbol,PERIOD_D1,0);
    Same as my time() function, except you have to handle 4066 errors.

    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

 
Keith Watford:

The code does not use local time, only platform time.

alright

 

William Roeder:

TradeTimeStartSeconds=temp[0])*3600 + temp[1]*60;
SECONDS now = time();
if(TradeTimeTimeStart >= now && now < TradeTimeEndSeconds

Stop using strings and just specify the time
          Find bar of the same time one day ago - MQL4 programming forum 2017.10.06 

Why stop using strings?

How else to input the time of day to use?

William Roeder:

long daysSeconds=TimeCurrent()-iTime(_Symbol,PERIOD_D1,0);

Same as my time() function, except you have to handle 4066 errors.

Why would I replace my simple line of code with yours?

I considered 4066 errors and in this case there is nothing to handle as it is not necessary.

      long daysSeconds=TimeCurrent()-iTime(_Symbol,PERIOD_D1,0);
      if(daysSeconds>=TradeTimeStartSeconds && daysSeconds<TradeTimeEndSeconds)
         {
         }

The iTime() call will return the correct time if it is updated.

If it is not updated it will return 0 or the time from some days previous. In either case daysSeconds wll be calculated to much larger than TradeTimeEndSeconds so the if() will not be executed.

If I had been concerned about 4066 errors, I would have used midnight instead.

   datetime timeNow=TimeCurrent();
   datetime midnight=timeNow-(timeNow%86400);