Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

New Bar Event - MetaTrader 5 Library | MT5 EA Download - MetaTrader 5 Resources

author EAcpu | 3 reads | 0 comments |

Allows you to determine the occurrence of new bar events in the Multicurrency Expert Advisor.

In multi-currency Expert Advisors, new bar formation events may occur at different times for different instruments. But since the EA always works on the chart of one instrument, it is necessary to correctly determine in the OnTick() event handler whether a new bar has been formed on another instrument within the required time period.

This library implements such functionality. In order to determine the start time of a new bar in the desired time period, information about the time of the first tick of the bar of each instrument of interest is stored at each tick. When a new bar starts, the start time of the new bar changes. Then the time remains constant until a new bar appears. In this library, all the rough work is done to create the necessary variables to store the times for different instruments and cycles. The user only needs to apply the IsNewBar() and UpdateNewBar() functions at the correct location.

To use it, you need to include the NewBarEvent.mqh library, then:

Usage examples are given below and in the NewBarEventExample.mq5 file.

 Blank check () { //--- 1. Important! Call UpdateNewBar() function at the beginning of OnTick() update new bar(); //--- 2.1 Check event if (is new bar( symbol (), period ())) { print ( "new bar" + enum to string ( period ()) + "for" + symbol ()); }
  
if (is new bar( "EURGBP" , PERIOD_M15 )) { print ( "EURGBP new bar of PERIOD_M15" ); }
if (is new bar( "USDCAD" , PERIOD_M15 )) { print ( "new bar of USDCAD PERIOD_M15" ); }
if (is new bar( "USDCHF" , PERIOD_M15 )) { print ( "The new bar of USDCHF is PERIOD_M15" ); }
  
if (is new bar( "EURGBP" , PERIOD_H1 )) { print ( "EURGBP new bar of PERIOD_H1" ); }
if (is new bar( "USDCAD" , PERIOD_H1 )) { print ( "new bar of USDCAD PERIOD_H1" ); }
if (is new bar( "USDCHF" , PERIOD_H1 )) { print ( "USDCHF PERIOD_H1 new bar" ); } //--- do something... sleep ( 1500 ); //--- 2.2 Check the event again if needed if (is new bar( symbol (), period ()))) { print ( "new bar" + enum to string ( period ()) + "order" + symbol () + "the second time at the same moment" ); }
  
if (is new bar( "EUR GBP" , PERIOD_M15 )) { print ( "EUR GBP appears for the second time on the same price move in a new bar of PERIOD_M15" ); }
if (is new bar( "USDCAD" , PERIOD_M15 )) { print ( "USDCAD has a new bar at PERIOD_M15 for the second time on the same price move" ); }
if (is new bar( "USDCHF" , PERIOD_M15 )) { print ( "USDCHF has a new bar of PERIOD_M15 for the second time in the same price move" ); }
  
if (is new bar( "EUR GBP" , PERIOD_H1 )) { print ( "EUR GBP appears for the second time on the same price tick in a new bar of PERIOD_H1" ); }
if (is new bar( "USDCAD" , PERIOD_H1 )) { print ( "USDCAD has a new bar at PERIOD_H1 for the second time in the same price move" ); }
if (is new bar( "USDCHF" , PERIOD_H1 )) { print ( "USDCHF has a new bar at PERIOD_H1 for the second time in the same price move" ); }
}

When running on GBPAUD M30 you can see output similar to the following

2022.04 . 01 22:30:00 New bar located at PERIOD_M30 for GBPAUD 2022.04 . 01 22:30:00 The new bar is located in PERIOD_M15 for EUR GBP 2022.04 . 01 22:30:00 New bar located at PERIOD_M15 for USDCAD 2022.04 . 01 22:30:00 The new bar is located at PERIOD_M15 for USD CHF 2022.04 . 01 22:30:02 New bar located at PERIOD_M30 for GBPAUD at the same time point for the second time 2022.04 . 01 22:30:02 The new bar is located at PERIOD_M15 for EURUSD for the second time at the same time 2022.04 . 01 22:30:02 The new bar is located at PERIOD_M15 for the second time USD / CAD at the same time 2022.04 . 01 22:30:02 The new bar is located at PERIOD_M15 for USDCHF for the second time at the same moment 2022.04 . 01 22:45:01 The new bar is located in PERIOD_M15 for EUR GBP 2022.04 . 01 22:45:01 The new bar is located at PERIOD_M15 for USDCAD 2022.04 . 01 22:45:01 The new bar is located at PERIOD_M15 for USD CHF 2022.04 . 01 22:45:02 The new bar is located at PERIOD_M15 for EURUSD for the second time at the same time 2022.04 . 01 22:45:02 The new bar is located at PERIOD_M15 for the second time USD / CAD at the same time 2022.04 . 01 22:45:02 New bar located at PERIOD_M15 for USDCHF for the second time at the same moment



Attachment download

📎 newbarevent.mqh (3.59 KB)

📎 newbareventexample.mq5 (2.58 KB)

Source: MQL5 #39899

Verification code Refresh