How to set a pending order at a specific Hour

 

Hi,

I need to set a pending order that calculate the entry and place itself exactly at 9am every day (Mon to Friday)

I've set the following code, but seems that it is not opening anything. I have to backtest it so I'm not sure if I'm using the "system time" or the "backtesting time", not sure how to explain it.

I'm a very basic user on this world. Thanks for help

#include<Trade\Trade.mqh>
CTrade trade;

// Dichiarazione delle variabili globali
double lotSize = 0.1;
input int stopLossPips = 5;
input datetime Time=D'09:00:00';

int OnInit()
{
   //---
   return(INIT_SUCCEEDED);
}

void OnTick()
{

datetime time=TimeLocal();
string minutesAndSeconds=TimeToString(time,TIME_SECONDS);
if (StringSubstr(minutesAndSeconds,0,8)==Time)
 
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
 

The first tick after 9 am does not necessarily appear at exactly 09:00:00. This tick may appear at 09:00:01 or 09:00:02, for example. Because of this, your conditions may not be met.

It will also be useful to understand the difference between TimeLocal and TimeCurrent.

 

You need to come up with an algorithm that works taking this into account:

Vladislav Boyko #:
The first tick after 9 am does not necessarily appear at exactly 09:00:00. This tick may appear at 09:00:01 or 09:00:02, for example

Don't rush to write code. First, describe this algorithm in words. You can use pen and paper for example. After you describe your algorithm in human language, write code according to it.


Pay attention to MqlDateTime. This may not be the only way to implement it, but it's certainly better than converting to a string.

 

It could be something like this:

  • On the previous tick the time was less than 9 am
  • On this tick the time is >= 9 am
This method is a bit similar to the method of checking a new bar.
 

You can convert the time to the number of minutes of the day:

MqlDateTime curr;
datetime currDt = TimeCurrent(curr);
int currMinsOfDay = curr.hour * 60 + curr.min;

For 9 am the number of minutes of the day is 540 (9 * 60 + 0).

Then this condition:

Vladislav Boyko #:
  • On the previous tick the time was less than 9 am
  • On this tick the time is >= 9 am
  • might look like this:

    • On the previous tick the minsOfDay < 540
    • On this tick the minsOfDay >= 540
     

    Converting the parameter to minutes of the day is a little more complicated:

    #property script_show_inputs
    
    input string inpTime = "09:00";
    
    void OnStart()
      {
       int minsOfDay;
       if(!stringToMinsOfDay(inpTime, minsOfDay))
          return;
       Alert(minsOfDay);
      }
    
    bool stringToMinsOfDay(const string inString,int &result)
      {
       MqlDateTime inStruct;
       if(!stringToStruct(inString, inStruct))
         {
          result = -1;
          return(false);
         }
       result = structToMinsOfDay(inStruct);
       return(true);
      }
    
    int structToMinsOfDay(const MqlDateTime &inStruct)
      {
       return(inStruct.hour * 60 + inStruct.min);
      }
    
    bool stringToStruct(const string inString,MqlDateTime &var)
      {
       if(!dtToStruct(StringToTime(inString), var))
          return(false);
       string back = IntegerToString(var.hour, 2, '0') + ":" + IntegerToString(var.min, 2, '0');
       if(inString == back)
          return(true);
       PrintFormat("Error converting time from string to MqlDateTime. \"%s\" converted to \"%s\"", inString, back);
       return(false);
      }
    
    bool dtToStruct(datetime a_dt,MqlDateTime &var)
      {
       ResetLastError();
       if(TimeToStruct(a_dt, var))
          return(true);
       PrintFormat("%s error %i, value %I64i", __FUNCTION__, GetLastError(), a_dt);
       return(false);
      }