Stop Trading Between Times

 

Hi All,

I've searched all over the forum, but I can't find a way around my problem, which is to prevent my EA from continuing to trade between two times. I can do this between two hours (Hour()), but when it comes to times that include minutes, I get stuck. I just can't get my head around the logic. This is what I have so far:

ext string EndTime = "14:30" // Do not trade after (server time)...
ext string StartTime = "15:30" // Do not trade before (server time)...

datetime   _start = StringToTime(StartTime);
datetime   _finish = StringToTime(EndTime);
datetime   _currentservertime = TimeCurrent();

void OnTick()
{
   _currentservertime = TimeCurrent();
   
   if( _currentservertime>=_finish && _currentservertime<=_start)       // It's after the finish time so  ....
   {
      if(ObjectFind(0,"OutOfHours_Label")<0) OutOfHours();              // Display the out of hours notice if it doesn't exist
      return;                                                           // return to beginning and wait for the next tic
   }


   if( _currentservertime>_start && _currentservertime<_finish)         // Continue trading and remove the out of hours label if it exists
   {
      ObjectDelete("OutOfHours_Label");                                 // and continue to next line of code
   }

        /////////////////// Rest of code

} 

//////////////////////////////////////////

void OutOfHours()
{
   ObjectCreate("OutOfHours_Label",OBJ_LABEL,0,0,0);

   ObjectSetInteger(0,"OutOfHours_Label",OBJPROP_CORNER,0);
   ObjectSet("OutOfHours_Label",OBJPROP_XDISTANCE,300);
   ObjectSet("OutOfHours_Label",OBJPROP_YDISTANCE,25);         
   ObjectSetText("OutOfHours_Label","Outside trading hours.",20,"Arial",Red);
} 

My aim is to stop trading between when the server time is between the finish and start times, but to then continue trading when the server time is after the start time.

I can't work out how to make this work properly. I get a different result when the start time is before the finish time on the same day (eg. 1730 start/1830 finish), and when the start time is after the finish time but on the next day (eg. 2330 finish/0130 start).

Please help, I'm going nuts over this :)

 

You need to get the time of the current day. I did it like this:

datetime timeofday=TimeCurrent() % 86400;
if(timeofday>=StartTime && ...

It means filter out the seconds that build up a whole day (24 * 3600).

 
lippmaje:

You need to get the time of the current day. I did it like this:

It means filter out the seconds that build up a whole day (24 * 3600).

Sorry, I don't understand. I ahve seen the % a few times on the forum, but I have no idea what it means ...
 
Graham from Brisbane:

Hi All,

I've searched all over the forum, but I can't find a way around my problem, which is to prevent my EA from continuing to trade between two times. I can do this between two hours (Hour()), but when it comes to times that include minutes, I get stuck. I just can't get my head around the logic. This is what I have so far:

My aim is to stop trading between when the server time is between the finish and start times, but to then continue trading when the server time is after the start time.

I can't work out how to make this work properly. I get a different result when the start time is before the finish time on the same day (eg. 1730 start/1830 finish), and when the start time is after the finish time but on the next day (eg. 2330 finish/0130 start).

Please help, I'm going nuts over this :)

This is one way:

string EndTime = "23:55"; // Do not trade after (server time)...
string StartTime = "01:30"; // Do not trade before (server time)...
bool TradeHours = false;

   MqlDateTime _start_mdt, _end_mdt, _server_mdt;
   TimeToStruct(StartTime,_start_mdt);
   TimeToStruct(EndTime,_end_mdt);
   TimeToStruct(TimeCurrent(),_server_mdt);
   
   bool CrossedDay = false;
   int StartMin = _start_mdt.hour*60+_start_mdt.min;
   int EndMin = _end_mdt.hour*60+_end_mdt.min;
   int ServerMin = _server_mdt.hour*60+_server_mdt.min;
   if (StartMin>EndMin)
      CrossedDay = true;
      
   if ((!CrossedDay && ServerMin>=StartMin && ServerMin<EndMin) ||
       (CrossedDay && (ServerMin>=StartMin || ServerMin<EndMin)))
   {
      // Trade time!!!
      :
   }
   else
   {
      // Not trade time!!!
   }

Basically (1) compare hours+mins alone, and (2) either use a boolean flag, or check that StartMin is more or less than EndMin so that the time range can be checked properly.

 
Graham from Brisbane:
Sorry, I don't understand. I ahve seen the % a few times on the forum, but I have no idea what it means ...
It's called Modulo.
 
lippmaje:
It's called Modulo.

Sadly, I just don't understand. Maybe it's because of my age (70+). And I thought you got smarter as you got older. Don't you believe it ...

Would you be able to give me a coded example of how you would use this if you were trying to achieve the same end as me, perhaps using my own code as the base?
 
void OnTick()
{
   _currentservertime = TimeCurrent();
   datetime servertimeofday = _currentservertime % 86400;
   
   if( servertimeofday >=_finish || servertimeofday <=_start)       // It's after the finish time so  ....
   {
      if(ObjectFind(0,"OutOfHours_Label")<0) OutOfHours();              // Display the out of hours notice if it doesn't exist
      return;                                                           // return to beginning and wait for the next tic
   }


   if( servertimeofday >_start && servertimeofday <_finish)         // Continue trading and remove the out of hours label if it exists
   {
      ObjectDelete("OutOfHours_Label");                                 // and continue to next line of code
   }
...

The modulo just strips the date of the server time. E.g. 2019.03.25 04:25:01 will become 04:25:01, the amount of seconds since midnight, since 86400 seconds make up a day.

 

I just tried this, but it doesn't work for me. I immediately call up the OutOfHours(), even though the server time is about 10:30 at the moment. I shouldn't be blocked from trading for another 13 hours or so ...

I have set end time at 2330 and start time at 0130. Please try it yourself if you have a few moments to spare, and let me know what you think :)

extern string EndTime = "23:30";    // Do not trade after (server time)...
extern string StartTime = "01:30";  // Do not trade before (server time)...

datetime   _start = StringToTime(StartTime);
datetime   _finish = StringToTime(EndTime);
datetime _currentservertime;


void OnTick()
{

   _currentservertime = TimeCurrent();
   datetime servertimeofday = _currentservertime % 86400;
   
   if( servertimeofday >=_finish || servertimeofday <=_start)           // It's after the finish time so  ....
   {
      if(ObjectFind(0,"OutOfHours_Label")<0) OutOfHours();              // Display the out of hours notice if it doesn't exist
      return;                                                           // return to beginning and wait for the next tic
   }


   if( servertimeofday >_start && servertimeofday <_finish)             // Continue trading and remove the out of hours label if it exists
   {
      ObjectDelete("OutOfHours_Label");                                 // and continue to next line of code
   }

}

//+------------------------------------------------------------------+
//|                  OUT OF HOURS LABEL                              |
//+------------------------------------------------------------------+

void OutOfHours()
{
   ObjectCreate("OutOfHours_Label",OBJ_LABEL,0,0,0);

   ObjectSetInteger(0,"OutOfHours_Label",OBJPROP_CORNER,0);
   ObjectSet("OutOfHours_Label",OBJPROP_XDISTANCE,300);
   ObjectSet("OutOfHours_Label",OBJPROP_YDISTANCE,25);         
   ObjectSetText("OutOfHours_Label","Outside trading hours.",20,"Arial",Red);

Thanks all for the help.

 

It should work. Maybe the conversion StringToTime went wrong. Just Print the values of _start, _end and servertimeofday to make sure. And this might fix it:

_start = _start % 86400;
_finish = _finish % 86400;
 

Input parameters:

static input int    HStart=2;              // Start Trading Hour
static input int    HStop =22;             // Stop Trading Hour

Logic 4:

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   //--- check trading time
    if(TimeHour(TimeCurrent())>=HStart && TimeHour(TimeCurrent())<HStop)
     {
      // Trade...
     }  
   }
For 5 use time struct.
//--- 
   MqlDateTime time1; 
   TimeToStruct(TimeCurrent(),time1); 
   
   int Hour=time1.hour;
     {
      //--- check trading time
       if(Hour(TimeCurrent())>=HStart && Hour(TimeCurrent())<HStop)
        {
         // Trade...
        }
     }
 
Marco vd Heijden:

Input parameters:

Logic 4:

For 5 use time struct.
Thanks Marco ... his doesn't address the minutes component though ...
Reason: