Download MetaTrader 5

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

2012.02.13 07:53
Libraries

IsNewBar - library for MetaTrader 5

| English Русский 中文 Español Deutsch 日本語 Português

Views:
2625
Rating:
votes: 13
\MQL5\Include\
isnewbar.mqh (1.41 KB)view
\MQL5\Experts\

СIsNewBar class is necessary for the efficient work of the Expert Advisors that make calculations at the moment the new bar appears.

Usually IsNewBar() function is used for such things instead of a class. But such a function contains a static variable, and therefore we cannot use several calls of this function. To be able to reuse such function repeatedly in Expert Advisor's code, it would be much easier to make it a class member. In this case that has been accomplished using IsNewBar.mqh.

The library code should be included in the file content on the global level using #include directive:

#include <IsNewBar.mqh>

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

static CIsNewBar NB1,NB2;

After that we can make calls to IsNewBar() functions

bool IsNewBar(string symbol,            // currency symbol
              ENUM_TIMEFRAMES timeframe)// calculation chart timeframe

in the Expert Advisor code:

if(NB1.IsNewBar(Symbol(),PERIOD_D1)) // checking for a new bar
     {
      /* Here is a trading signal 1 receiving block code */
     }

Here is the possible example of the code that includes СIsNewBar class inside OnTick() function:

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//----

   double iClose1[1],iClose2[1];
//---- declaration of static variables
   static bool Recount1=true,Recount2=true;
   static CIsNewBar NB1,NB2;

//+----------------------------------------------+
//| Detecting market entry signals               |
//+----------------------------------------------+
   if(NB1.IsNewBar(Symbol(),PERIOD_D1) || Recount1) // checking for a new bar
     {
      Recount1=false;
      
      //---- copy newly appeared data in the arrays
      if(CopyClose(Symbol(),PERIOD_D1,1,1,iClose1)<=0) {Recount1=true; return;}
      
      /* Here is a trading signal 1 receiving block code */
      
     }
     
   if(NB2.IsNewBar(Symbol(),PERIOD_H4) || Recount2) // checking for a new bar
     {
      Recount2=false;
      
      //---- copy newly appeared data in the arrays
      if(CopyClose(Symbol(),PERIOD_H4,1,1,iClose2)<=0) {Recount2=true; return;}
      
      /* Here is a trading signal 2 receiving block code */
      
     }

Translated from Russian by MetaQuotes Software Corp.
Original code: https://www.mql5.com/ru/code/768

Accelerator Oscillator (AC) Accelerator Oscillator (AC)

The Acceleration/Deceleration Indicator (AC) measures acceleration and deceleration of the current driving force.

Average Directional Movement Index (ADX) Average Directional Movement Index (ADX)

The Average Directional Movement Index Indicator (ADX) helps to determine if there is a price trend.

Holt's double exponential smoothing trend Holt's double exponential smoothing trend

Holt's double exponential smoothing - the trend component (mt4 version).

Holt's double exponential smoothing (mt4 version) Holt's double exponential smoothing (mt4 version)

Holt's double exponential smoothing with linear forecasting.