Watch how to download trading robots for free
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Libraries

New Bar Event - library for MetaTrader 5

Views:
1229
Rating:
votes: 29
Published:
2022.07.05 09:34
Updated:
2022.07.05 09:38
\MQL5\Include\

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





Two pending orders 2 Two pending orders 2

Two pending orders at the beginning of the day

At random Full At random Full

The Expert Advisor is a joke: the position is opened randomly. Now there is a Stop Loss, Take Profit and much more

Trailing Stop by Fixed Parabolic SAR Trailing Stop by Fixed Parabolic SAR

Modify to allow direct specification of the starting point of the Parabolic SAR.

Symbol Trade Made Simple With Functions Symbol Trade Made Simple With Functions

This library shows useful informations and do a lot of functions to a symbol.