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

IsNewBar - MetaTrader 5 Library | MT5 EA Download - MetaTrader 5 Resources

author EAcpu | 5 reads | 0 comments |

The СIsNewBar class is necessary for the efficient work of the Expert Advisor that calculates when a new bar appears.

Usually the IsNewBar() function is used for such operations rather than classes. But such a function contains static variables so we cannot call the function multiple times. To be able to reuse such a function repeatedly in the Expert Advisor's code, it is much easier to make it a class member. In this case, this is done using IsNewBar.mqh.

Library code should be included in the file contents at the global level, using the #include directive:

 #include

Then, the required number of СIsNewBar class variables must be declared in the Expert Advisor's OnTick() block:

 static CIsNewBar NB1,NB2;

Then we can call the IsNewBar() function

 boolean is new column ( string symbol, // currency symbol ENUM_TIMEFRAMES approximate time) // calculated chart timeframe

In the EA trading code:

 if (NB1.IsNewBar( symbol (), PERIOD_D1 )) // Check for new bar {
/* Here is the trading signal 1 receiving block code */ }

The following is a possible code example of including the СIsNewBar class in the OnTick() function:

 //+------------------------------------------------------------------+
//|Expert check function |
//+------------------------------------------------------------------+
Blank check ()
  { //---- Double i closes 1[ 1 ], i closes 2[ 1 ]; //---- Declaration of static variables static Boolean value recount 1 = true , recount 2 = true ;
Static CIsNewBar NB1,NB2; //+---------------------------------------------------------+
//|Detect market entry signals |
//+--------------------------------------------------------+ if (NB1.IsNewBar( symbol (), PERIOD_D1 ) || Recount 1) // Check for new bar {
      Recount 1 = wrong ;
 
//---- Copy the newly appearing data in the array if ( copy off ( symbol (), PERIOD_D1 , 1 , 1 ,i off 1)<= 0 ) {recount 1= true ; return ;}
 
/* Here is the trading signal 1 receiving block code */ 
         }
  
if (NB2.IsNewBar( symbol (), PERIOD_H4 ) ||recount2) // check for new bar {
      Recount2= wrong ;
 
//---- Copy the newly appearing data in the array if ( copy off ( symbol (), PERIOD_H4 , 1 , 1 , i off 2) <= 0 ) {recount 2 = true ; return ;}
 
/*Here is the trading signal 2 receiving block code*/ 
         }


Attachment download

📎 isnewbar.mqh (1.41 KB)

📎 isnewbar_example.mq5 (1.73 KB)

Source: MQL5 #768

Verification code Refresh