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

Detecting the start of a new bar or candle - MetaTrader 5용 expert

조회수:
5789
평가:
(36)
게시됨:
2022.04.24 00:38
업데이트됨:
2022.04.25 09:58
NewBar.mq5 (2.8 KB) 조회
이 코드를 기반으로 한 로봇이나 지표가 필요하신가요? 프리랜스로 주문하세요 프리랜스로 이동

For an Expert Advisor (EA), when a new tick quote arrives, the MetaTrader terminal calls the default OnTick() event handling function. However, there is no default event handling function for when a new bar (candle) starts or opens.

To detect this, one needs to monitor the opening time of the currently most recent bar. Once it changes, it signifies the start of a new bar, and one can react to it and handle the event. The following sample code, compatible with both MQL4 and MQL5, shows one such method on how this can be achieved:

// Default tick event handler
   void OnTick()
   {
      // Check for new bar (compatible with both MQL4 and MQL5).
         static datetime dtBarCurrent  = WRONG_VALUE;
                datetime dtBarPrevious = dtBarCurrent;
                         dtBarCurrent  = iTime( _Symbol, _Period, 0 );
                bool     bNewBarEvent  = ( dtBarCurrent != dtBarPrevious );

      // React to a new bar event and handle it.
         if( bNewBarEvent )
         {
            // Detect if this is the first tick received and handle it.
               /* For example, when it is first attached to a chart and
                  the bar is somewhere in the middle of its progress and
                  it's not actually the start of a new bar. */
               if( dtBarPrevious == WRONG_VALUE )
               {
                  // Do something on first tick or middle of bar ...
               }
               else
               {
                  // Do something when a normal bar starts ...
               };

            // Do something irrespective of the above condition ...
         }
         else
         {
            // Do something else ...
         };

      // Do other things ...
   };

In the above code, the static variable keeps track of the bar's opening time, even when returning from the OnTick() function. Unlike a normal local variable, it memorizes its data content and does not release it when leaving the function. This is the key to detecting a change in the opening time of the current bar.

It's also important to note, that when the EA is first placed on a chart, the above code reacts as if the bar has just opened. This condition requires special handling if the situation needs to be handled differently.

Please note, that all my CodeBase publications' source code are now also available in "Public Projects" tab of MetaEditor under the name "FMIC".
Trend Two Lines Trend Two Lines

The indicator in the subwindow shows a comparison of High and Low prices

Listing all MT5 Signals' properties to a CSV file Listing all MT5 Signals' properties to a CSV file

This simple quick & dirty script code will output a CSV file of all the Signals' properties as reported by the MQL5 Trade Signals functionality.

Trading Volume Line Trading Volume Line

The indicator compares OHLC prices

ZigZagExtremaOnArray ZigZagExtremaOnArray

The ZigZagExtremaOnArray is an mqh include file which contains the calculation function of MetaQuotes' Examples ZigZag, BUT you can use it on any buffer that contains curves or histograms, similarly to the MovingAverages.mqh from include. The idea was to get highs and lows of an indicator curve quickly with the help of an "onBuffer" function.