Issues with TimeHour Error when Coding EA

 

Hello, wondering if anyone could help out, im trying to code my EA in Mql5 but i get this error, Any help on how to resolve this.

Thank you in Advance

Note: The starthour and endhour are already defined


Part of the Code concerning time...

void OnTick()
{
   datetime currentTime = TimeCurrent();
   int currentHour = TimeHour(TimeCurrent());
   if (currentHour < startHour || currentHour > endHour) return; 

Erro Returned...

'TimeHour' - undeclared identifier
'TimeCurrent' - some operator expected

...

Improperly formatted code edited by moderator. Please use the CODE button (Alt-S) when inserting code or log output.

Code button in editor


 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Steven256: im trying to code my EA in Mql5 but i get this error, Any help on how to resolve this.

The function "TimeHour()" does not exist in MQL5. It's from MQL4. Is this A.I. generated code?

Please, don't request help for ChatGPT (or other A.I.) generated code. It generates horrible invalid code, often mixing MQL4 and MQL5.

To learn MQL5 programming, you can research the many available Articles on the subject, or examples in the Codebase, as well as reference the online Book and Documentation

If you do not want to learn to code, that is not a problem. You can either look at the Codebase if something free already exists, or in the Market for paid products (also sometimes free). However, recommendations or suggestions for Market products are not allowed on the forum, so you will have to do your own research.

Finally, you also have the option to hire a programmer in the Freelance section.

 
Steven256:

Hello, wondering if anyone could help out...

Inputs:

input group "---------------------------------------";
input group "Turn trading time filter on/off";
input bool UseTimer = true;
input group "---------------------------------------";
input group "Use personal computer time to filter? ==> if false, broker time is used";
input bool UsePCtime = true;
input group "---------------------------------------";
input group "Set time to enable trading ==> Intraday and overnight are supported";
input string StartTime = "21:00";
input group "---------------------------------------";
input group "Set time to disable trading";
input string StopTime = "16:00";
input group "---------------------------------------";

Variables on global scope:

datetime dLocalTime, dStartTime, dStopTime;
ulong uLocalTime, uStartTime, uStopTime,

In OnTick():

   dStartTime = StringToTime(StartTime);
   uStartTime = ulong(dStartTime);
   
   dStopTime = StringToTime(StopTime);
   uStopTime = ulong(dStopTime);
   
   if(UsePCtime == true)
    {   
     dLocalTime = TimeLocal();
    }
   else
    {
     dLocalTime = TimeCurrent();
    }
    
   uLocalTime = ulong(dLocalTime);

   if(uStartTime < uStopTime) // intraday start trading time is earlier than intraday stop trading time
    {
     if(UseTimer == true)
      {
       if(uLocalTime >= uStartTime
        && uLocalTime < uStopTime)
         {
          runBot = true;
        
          if(timerPrinted != 1)
           {
            Print("Timer is ON. Current time is within set trading times. Bot is ON.");
            timerPrinted = 1;
           }
         }
       else
         {
          runBot = false;
        
          if(timerPrinted != 2)
           {
            Print("Timer is ON. Current time is outside of set trading times. Bot is OFF.");
            CancelOrder();
            timerPrinted = 2;
           }
         }
      }
    }
   
   if(uStartTime > uStopTime) // intraday start trading time is later than intraday stop trading time
    {
     if(UseTimer == true)
      {
       if(uLocalTime >= uStopTime
        && uLocalTime < uStartTime)
         {
          runBot = false;

          if(timerPrinted != 2)
           {
            Print("Timer is ON. Current time is outside of set trading times. Bot is OFF.");
            timerPrinted = 2;
           }
         }
       else
         {
          runBot = true;

          if(timerPrinted != 1)
           {
            Print("Timer is ON. Current time is within set trading times. Bot is ON.");
            timerPrinted = 1;
           }
         }
      }
    }
   
   if(UseTimer == false) 
    {
     runBot = true;
     
     if(timerPrinted != 3)
      {
       Print("Timer is OFF. Bot is ON.");
       timerPrinted = 3;
      }
    }

And then put your trading code inside:

   if(runBot == true)
    {


    //all of your trading code


    }
(I prefer to leave trade exits code outside of the time filter).
 
Ryan L Johnson #:

Inputs:

Variables on global scope:

In OnTick():

And then put your trading code inside:

Nice , but i'd use TimeTradeServer() instead of TimeCurrent() - if i remember correctly time current is for the last tick

 
Lorentzos Roussos #:

Nice , but i'd use TimeTradeServer() instead of TimeCurrent() - if i remember correctly time current is for the last tick

Thanks. If you set UsePCtime = true, then TimeLocal() is used.

I see that TimeTradeServer() is simulated on the local machine based on TimeCurrent(). That's clear as mud to me. How exactly does TimeTradeServer() differ from TimeLocal()... other than the additional time stucture required?

 
Ryan L Johnson #:

Thanks. If you set UsePCtime = true, then TimeLocal() is used.

I see that TimeTradeServer() is simulated on the local machine based on TimeCurrent(). That's clear as mud to me. How exactly does TimeTradeServer() differ from TimeLocal()... other than the additional time stucture required?

The offset of the server and the offset of the machine i guess.

 
Lorentzos Roussos #:

The offset of the server and the offset of the machine i guess.

Got it. Thanks.