Guarda come scaricare robot di trading gratuitamente
Ci trovi su Twitter!
Unisciti alla nostra fan page
Script interessante?
Pubblica il link!
lasciare che altri lo valutino
Ti è piaciuto lo script? Provalo nel Terminale MetaTrader 5
Librerie

Check for Market Open Hours - libreria per MetaTrader 5

Visualizzazioni:
3550
Valutazioni:
(6)
Pubblicato:
2023.09.26 11:23
Hai bisogno di un robot o indicatore basato su questo codice? Ordinalo su Freelance Vai a Freelance

The MarketOpenHours.mqh file checks the Market Open Hours against the server time at the broker. Its input is only the Symbol name as string type. As a result you get a bool, true - the market is open, false - the market is closed. In the following image i show the Open Hours of my broker DarwinEx for the symbol EURUSD.

Market Open Hours for EURUSD

You can see how different these open times are on different weekdays. So here is your include file:

//+------------------------------------------------------------------+
//|                                              MarketOpenHours.mqh |
//|                                        Wolfgang Melz, wm1@gmx.de |
//|                                                 https://melz.one |
//+------------------------------------------------------------------+
#property copyright "Wolfgang Melz, wm1@gmx.de"
#property link      "https://melz.one"

//+------------------------------------------------------------------+
//| MarketOpenHours                                                  |
//+------------------------------------------------------------------+
bool MarketOpenHours(string sym) {
  bool isOpen = false;                                  // by default market is closed
  MqlDateTime mdtServerTime;                            // declare server time structure variable
  datetime dtServerDateTime = TimeTradeServer();        // store server time 
  if(!TimeToStruct(dtServerDateTime,                    // is servertime correctly converted to struct?
                   mdtServerTime)) {
    return(false);                                      // no, return market is closed
  }

  ENUM_DAY_OF_WEEK today = (ENUM_DAY_OF_WEEK)           // get actual day and cast to enum
                            mdtServerTime.day_of_week;

  if(today > 0 || today < 6) {                          // is today in monday to friday?
    datetime dtF;                                       // store trading session begin and end time
    datetime dtT;                                       // date component is 1970.01.01 (0)
    datetime dtServerTime = dtServerDateTime % 86400;   // set date to 1970.01.01 (0)
    if(!SymbolInfoSessionTrade(sym, today,              // do we have values for dtFrom and dtTo?
                               0, dtF, dtT)) {
      return(false);                                    // no, return market is closed
    }
    switch(today) {                                     // check for different trading sessions
      case 1:
        if(dtServerTime >= dtF && dtServerTime <= dtT)  // is server time in 00:05 (300) - 00:00 (86400)
          isOpen = true;                                // yes, set market is open
        break;
      case 5:
        if(dtServerTime >= dtF && dtServerTime <= dtT)  // is server time in 00:04 (240) - 23:55 (86100)
          isOpen = true;                                // yes, set market is open
        break;
      default:
        if(dtServerTime >= dtF && dtServerTime <= dtT)  // is server time in 00:04 (240) - 00:00 (86400)
          isOpen = true;                                // yes, set market is open
        break;
    }
  }
  return(isOpen);
}

//+------------------------------------------------------------------+

The first part checks for the weekday, Mon - Fri to allow opening trades and trailing stop during the week. In the switch command the actual server time is checked against the broker's symbol session.

Implications:

  • If you run this script on every tick or every minute, it will give the exact open hours according to the broker's server like you see in the above image.
  • If you run this script every 5 minutes on my broker account, it is correct only on Monday 00:05 - 00:00
  • If you run this script on the open of each new bar, the first bar of every weekday get false, market closed also for the last bar on Friday
  • If you run this script on PERIOD_D1, PERIOD_W1, PERIOD_MN1 it will not give a true signal for my broker because it runs at 00:00 and market open is at 00:04
Feel free to use it in your EA at your own risk and backtest well.

    ATR - Average True Range - code for beginners by William210 ATR - Average True Range - code for beginners by William210

    ATR - Average True Range - beginner tutorial to learn how to code in MQL5

    Symbol Filling Policy Determination Symbol Filling Policy Determination

    This function allows you to retrieve and return the filling policy of a symbol in MetaTrader 5. This is useful for making trading decisions based on the filling policy of a specific financial instrument.

    Breakout Martin Gale Expert Advisor for MT5 Breakout Martin Gale Expert Advisor for MT5

    This is an mql5 Expert Advisor trading the breakouts and using Martin Gale risk management.

    ATR Trailing Stop with 1 Buffer only ATR Trailing Stop with 1 Buffer only

    This is an edit of the Mod_ATR_Trailing_Stop by MQL5 user @Scriptor found here https://www.mql5.com/en/code/20423 . MT5 indicator .mql5 and .ex5 files, report any bugs, I'll fix.