Trading time, hour:min code

m1213
282
m1213  
input string StartTime = "08:00"; 
input string StopTime = "20:00";
/--------

bool HoursFilter()

  {   

   datetime time = TimeCurrent();
   string hours_minutes1 = TimeToString(time,TIME_MINUTES);

   if(StringToInteger(StringSubstr(hours_minutes1,0,5))>=StringToInteger(StartTime) && StringToInteger(StringSubstr(hours_minutes1,0,5))<=StringToInteger(StopTime))
     {
      return true;
     }

   return false;
     
  }

Hello Expert Coders,

Above mql5 code for specifying trading times in "hour:min" isn't working, the EA generates only zero values in backtesting.

I hope it's a minor coding error, and would appreciate your expert input. 


Thanks in advance for your kind assistance.

M.

Sardion Maranatha
761
Sardion Maranatha  
m1213:

Hello Expert Coders,

Above mql5 code for specifying trading times in "hour:min" isn't working, the EA generates only zero values in backtesting.

I hope it's a minor coding error, and would appreciate your expert input. 


Thanks in advance for your kind assistance.

M.

//+------------------------------------------------------------------+
//|                                                InTradingTime.mq5 |
//|                             Copyright 2021, Rosh Jardine Capital |
//|                                          https://roshjardine.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, Rosh Jardine Capital"
#property link      "https://roshjardine.com"
#property version   "1.00"
#property script_show_inputs
//--- input parameters
input int      InpStartHour = 0;
input int      InpStartMinute = 59;
input int      InpEndHour = 23;
input int      InpEndMinute = 58;

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
  InTradingTime();
  }
//+------------------------------------------------------------------+

bool InTradingTime()
{
  datetime Now = TimeCurrent();
  MqlDateTime NowStruct;
  TimeToStruct(Now,NowStruct);
  int StartTradingSeconds = (InpStartHour*3600) + (InpStartMinute*60);
  int EndTradingSeconds = (InpEndHour*3600) + (InpEndMinute*60);
  int runningseconds = (NowStruct.hour*3600) + (NowStruct.min*60);
  ZeroMemory(NowStruct);
  if ((runningseconds>StartTradingSeconds)&&(runningseconds<EndTradingSeconds))
  {
    Print("within trading time");
    return(true);
  }
  Print("outside of trading time");
  return(false);
}
m1213
282
m1213  
Sardion Maranatha:

Thanks @roshjardine for instant reply with code in detail.

My ea script doesn't have void OnStart() type function, instead have void OnTick() and ends with return;} operator.

I tried inserting InTradingTime(); before and after return; of OnTick(), without retun; operator and also at int OnInit(), but none worked. I still get same issue of zero values in backtesting. And if I were to use void OnStart() code as it is for InTradtingTime(); then I get a lot of errors!

Do you know the correct place to insert InTradingTime(); function?

Thanks again and regards,

M.

m1213
282
m1213  
Sardion Maranatha:

Hi Sardion,

It's sorted, just had to place within void OnEveryTick(), bit of other code re-arrangement and it woked perfectly.

Thanks again for your code and file.

Regards,

M.