New Bar Event – library MetaTrader 5

Allows you to determine the occurrence of a new bar event in a multicurrency Expert Advisor.

In a multicurrency Expert Advisor, the event of a new bar formation can occur at different times for different instruments. But since the EA always works on the chart of one instrument, it becomes necessary to correctly determine in the OnTick() event handler whether a new bar has already formed for the required period on another instrument.

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

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

  1. Necessarily   call the UpdateNewBar() function   at the beginning of OnTick()
  2. Use the function IsNewBar(symbol, timeframe)   to check the occurrence of an event in OnTick()

An example of usage is given below and in the NewBarEventExample.mq5 file.

void OnTick() {
//--- 1. IMPORTANT! Call UpdateNewBar() function at begin of OnTick()
   UpdateNewBar();

//--- 2.1 Check events
   if(IsNewBar(Symbol(), Period()))   { Print("New Bar at " + EnumToString(Period()) + " for " + Symbol()); }
   
   if(IsNewBar("EURGBP", PERIOD_M15)) { Print("New Bar at PERIOD_M15 for EURGBP"); }
   if(IsNewBar("USDCAD", PERIOD_M15)) { Print("New Bar at PERIOD_M15 for USDCAD"); }
   if(IsNewBar("USDCHF", PERIOD_M15)) { Print("New Bar at PERIOD_M15 for USDCHF"); }
   
   if(IsNewBar("EURGBP", PERIOD_H1))  { Print("New Bar at PERIOD_H1  for EURGBP"); }
   if(IsNewBar("USDCAD", PERIOD_H1))  { Print("New Bar at PERIOD_H1  for USDCAD"); }
   if(IsNewBar("USDCHF", PERIOD_H1))  { Print("New Bar at PERIOD_H1  for USDCHF"); }

//--- Perform some actions...
   Sleep(1500);

//--- 2.2 Check events again if needed
   if(IsNewBar(Symbol(), Period()))   { Print("New Bar at " + EnumToString(Period()) + " for " + Symbol() + " second time at the same tick"); }
   
   if(IsNewBar("EURGBP", PERIOD_M15)) { Print("New Bar at PERIOD_M15 for EURGBP second time at the same tick"); }
   if(IsNewBar("USDCAD", PERIOD_M15)) { Print("New Bar at PERIOD_M15 for USDCAD second time at the same tick"); }
   if(IsNewBar("USDCHF", PERIOD_M15)) { Print("New Bar at PERIOD_M15 for USDCHF second time at the same tick"); }
   
   if(IsNewBar("EURGBP", PERIOD_H1))  { Print("New Bar at PERIOD_H1  for EURGBP second time at the same tick"); }
   if(IsNewBar("USDCAD", PERIOD_H1))  { Print("New Bar at PERIOD_H1  for USDCAD second time at the same tick"); }
   if(IsNewBar("USDCHF", PERIOD_H1))  { Print("New Bar at PERIOD_H1  for USDCHF second time at the same tick"); }
}

When running on GBPAUD M30, you can see something like this output

2022.04.01 22:30:00   New Bar at PERIOD_M30 for GBPAUD
2022.04.01 22:30:00   New Bar at PERIOD_M15 for EURGBP
2022.04.01 22:30:00   New Bar at PERIOD_M15 for USDCAD
2022.04.01 22:30:00   New Bar at PERIOD_M15 for USDCHF
2022.04.01 22:30:02   New Bar at PERIOD_M30 for GBPAUD second time at the same tick
2022.04.01 22:30:02   New Bar at PERIOD_M15 for EURGBP second time at the same tick
2022.04.01 22:30:02   New Bar at PERIOD_M15 for USDCAD second time at the same tick
2022.04.01 22:30:02   New Bar at PERIOD_M15 for USDCHF second time at the same tick
2022.04.01 22:45:01   New Bar at PERIOD_M15 for EURGBP
2022.04.01 22:45:01   New Bar at PERIOD_M15 for USDCAD
2022.04.01 22:45:01   New Bar at PERIOD_M15 for USDCHF
2022.04.01 22:45:02   New Bar at PERIOD_M15 for EURGBP second time at the same tick
2022.04.01 22:45:02   New Bar at PERIOD_M15 for USDCAD second time at the same tick
2022.04.01 22:45:02   New Bar at PERIOD_M15 for USDCHF second time at the same tick
📈 ROBOTFX MetaTrader Expert Advisors and Indicators to maximize profits and minimize the risks