거래 로봇을 무료로 다운로드 하는 법을 시청해보세요
당사를 Facebook에서 찾아주십시오!
당사 팬 페이지에 가입하십시오
스크립트가 흥미로우신가요?
그렇다면 링크 to it -
하셔서 다른 이들이 평가할 수 있도록 해보세요
스크립트가 마음에 드시나요? MetaTrader 5 터미널에서 시도해보십시오
라이브러리

Check for Market Open Hours - MetaTrader 5용 라이브러리

조회수:
4385
평가:
(6)
게시됨:
2023.09.26 11:23
이 코드를 기반으로 한 로봇이나 지표가 필요하신가요? 프리랜스로 주문하세요 프리랜스로 이동

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.