Is there any way to collect market close time so we can close positions before market closes?

 

Is there any way to collect market close time so we can close positions before market closes?
No matter general or specifically for each instrument, to prevent market gaps' affections.

It is MQL5 question, however if you have any solution for MQL4 too, it would be double appreciated!

 
HosseinKOGO:

Is there any way to collect market close time so we can close positions before market closes?
No matter general or specifically for each instrument, to prevent market gaps' affections.

It is MQL5 question, however if you have any solution for MQL4 too, it would be double appreciated!

You are looking for these functions:

 
Dominik Christian Egert #:
You are looking for these functions:

Thank you man!
Since there is no example for this in documentation, I have these questions:
1. What is session index?
2. I have no idea about how to automate date from and date to values. I mean for example for day of the week, I check what is the day today in a function before this line, then I put the function as the second parameter of this function. How can I automate the date from and date to? Assume I need to detect the "nearest" market close time for the symbol.

If possible, please show me an example. I do not know even what should I search for this case.

Cheers!

 
HosseinKOGO #:

Thank you man!
Since there is no example for this in documentation, I have these questions:
1. What is session index?
2. I have no idea about how to automate date from and date to values. I mean for example for day of the week, I check what is the day today in a function before this line, then I put the function as the second parameter of this function. How can I automate the date from and date to? Assume I need to detect the "nearest" market close time for the symbol.

If possible, please show me an example. I do not know even what should I search for this case.

Cheers!

When you open Symbol specifications (CTRL-U) and you look at the session times, you will find some symbols have more than one session in a day, that is the session index.

Any other is just retrieve the values relevant for you and compare them as you need... - Dont know exactly what I could contribute to that?

EDIT:

   datetime         from = NULL;
   datetime         to = NULL;
bool success = SymbolInfoSessionTrade(Symbol(), MONDAY, 0, from, to);

printf("First Session on Monday begins at %s and ends at %s", TimeToString(from), TimeToString(to));  
 

You can querry multiple sessions like this also :

bool IsInTradingSession(){
MqlDateTime mqt;
if(TimeToStruct(TimeTradeServer(),mqt)){
  //flatten
    ENUM_DAY_OF_WEEK dow=(ENUM_DAY_OF_WEEK)mqt.day_of_week;
    mqt.hour=0;mqt.min=0;mqt.sec=0;
    datetime base=StructToTime(mqt),get_from=0,get_to=0;
  //now loop in all the trading sessions 
    uint session=0;
    while(SymbolInfoSessionTrade(_Symbol,dow,session,get_from,get_to)){
    //so if this succeeds a session exists and fills up get from and get to , but it just fills up with hour , minute + second
      //that means we have to project it on the base time we flattened above for today
        get_from=(datetime)(base+get_from);
        get_to=(datetime)(base+get_to);
        Print("Session [ "+IntegerToString(session)+" ] ("+TimeToString(get_from,TIME_DATE|TIME_MINUTES|TIME_SECONDS)+")->("+TimeToString(get_to,TIME_DATE|TIME_MINUTES|TIME_SECONDS)+")");
    //and we pump one session in 
      session++;
    //and we check , if we happen to be inside that range , we return true because we can trade
      if(TimeTradeServer()>=get_from&&TimeTradeServer()<=get_to){return(true);}
    }  
}
return(false);
}
 
Lorentzos Roussos #:

You can querry multiple sessions like this also :

Much better example, in fact. A working version.
 
Thank you guys for your concentration <3