Questions from Beginners MQL5 MT5 MetaTrader 5 - page 517

 

MT4, please help the community.
1. In the Expert Advisor, working (by ticks or on a timer 0.5 sec) on the M1 chart, you need to make calculations at the moment of a new candle on the H1 chart.
We can catch the start of the M1 candle, following the last M1 candle of each hour (NN:00 minutes), but it is not very nice ... Besides, the H1 candlestick may not appear at that moment.
Maybe somebody did it more elegantly ?
2. How much longer it takes to get the indicator values through iCustom(), compared to the indicator code in the Expert Advisor text?
3. if the code of the indicator in the text of the Expert Advisor is much faster to be calculated, how in the Expert Advisor attached to the M1 chart, to calculate the indicator value on the H1 chart ?

 
MikeZv:

MT4, I am asking for help from the community.
1. In the Expert Advisor, working (by ticks or by timer 0.5 sec) on the M1 chart, you need to make calculations at the moment when a new candle appears on the H1 chart.
We can catch the start of the M1 candle, following the last M1 candle of each hour (NN:00 minutes), but it is not very nice ... Besides, the H1 candlestick may not appear at that moment.
Maybe somebody did it more elegantly ?
2. How much longer to get the indicator values through iCustom(), compared to the indicator code in the Expert Advisor text?
3. if the code of the indicator in the text of the Expert Advisor is much faster to be calculated, how in the Expert Advisor attached to the M1 chart, to calculate the indicator value on the H1 chart ?

The answer to the first question, the function defines a new bar.

bool NevBar(int period)
  {
   static datetime StatTime;

   if(StatTime!=iTime(NULL,period,0))
     {
      StatTime=iTime(NULL,period,0);
      return(true);
     }
   return(false);
  }

example of use

int OnInit()
  {
//---
   NevBar(PERIOD_H1);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(NevBar(PERIOD_H1))
     {
      производим вычисления.
     }
  }
//+------------------------------------------------------------------+

 
Sergey Gritsay:

The answer to the first question, the function defines a new bar.

Thanks a lot Sergey, I've looked through the documentation, but somehow I missed iTime() ... :
And whycall NevBar() inOnInit() ?

 
MikeZv:

The first function call, otherwise it will be triggered immediately. To make surethat OnInit() is not called

bool NevBar(int period)
  {
   static datetime StatTime;
   
   if(StatTime==0)StatTime=iTime(NULL,period,0);

   if(StatTime!=iTime(NULL,period,0))
     {
      StatTime=iTime(NULL,period,0);
      return(true);
     }
   return(false);
  }
 
Sergey Gritsay:

The first function call, otherwise it will be triggered immediately. To make surethat OnInit() is not called

Thank you, Sergei! On forum I was frightened strongly: "Don't use static variables ! " :)
 
MikeZv:
Thank you Sergei ! On the forum, I was scared stiff: "Don't use static variables ! " :)
Don't listen, if you have to use it - use it bravely but correctly!
 
MikeZv:
Thank you Sergei ! On the forum, I was scared stiff: "Don't use static variables ! " :)
It's the first time I've heard about it, I wonder what's so scary about them, I rarely use them too, but they're better for certain tasks.
 
Sergey Gritsay:
This is the first time I've heard about them, I wonder what's so scary about them, I rarely use them too, but they are better suited for certain tasks.
The matter is that a static variable can be used only if NevDay() is called for the same symbol and timeframe during the entire runtime of the EA. If there are 2 or more symbols, NevDay() will return incorrect values. My Expert Advisor is multi-currency ... :(
 
MikeZv:
The point is that static variable can only be used if NevDay() is called for the same symbol and timeframe. If there are 2 or more symbols, NevDay() will return incorrect values. My Expert Advisor is multi-currency ... :(

If you need it for multicurrency mode, here is an example, it works in MT4 and MT5

//+------------------------------------------------------------------+
//|                                                         тест.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   int SymbolTotal=SymbolsTotal(true);
   Comment("SymbolTotal = ",SymbolTotal);
   for(int i=0;i<SymbolTotal;i++)
     {
      if(NewBar(SymbolTotal,i,SymbolName(i,true),PERIOD_H1))
        {
         Alert(SymbolName(i,true)," Новый бар на Н1");
        }
     }

  }
//+------------------------------------------------------------------+
bool NewBar(int size,int i,string symbol,ENUM_TIMEFRAMES  period)
  {
   datetime curbar[];
   static datetime lastbar[];
   ArrayResize(curbar,size);
   ArrayResize(lastbar,size);

   curbar[i]=(datetime) SeriesInfoInteger(symbol,period,SERIES_LASTBAR_DATE);

   if(lastbar[i]==0)lastbar[i]=(datetime)SeriesInfoInteger(symbol,period,SERIES_LASTBAR_DATE);
   if(lastbar[i]!=curbar[i])
     {
      lastbar[i]=curbar[i];
      return(true);
     }
   return(false);
  }
//+------------------------------------------------------------------+
 
MikeZv:
The point is that the static variable can only be used if NevDay() is called for the same symbol and timeframe. If there are 2 or more symbols, NevDay() will return incorrect values. My Expert Advisor is multi-currency ... :(

Here is another option using the class

//+------------------------------------------------------------------+
//|                                                         тест.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CNevBar
  {
private:
   datetime          curbar;
   datetime          lastbar;
public:
                     CNevBar();
                    ~CNevBar();
   bool              new_bar(string symbol,ENUM_TIMEFRAMES period);
  };
//+------------------------------------------------------------------+
void CNevBar::CNevBar()
  {

  }
//+------------------------------------------------------------------+
void CNevBar::~CNevBar(void)
  {

  }
//+------------------------------------------------------------------+
bool CNevBar:: new_bar(string symbol,ENUM_TIMEFRAMES period)
  {
   curbar=(datetime) SeriesInfoInteger(symbol,period,SERIES_LASTBAR_DATE);
   if(lastbar==0)lastbar=(datetime)SeriesInfoInteger(symbol,period,SERIES_LASTBAR_DATE);
   if(lastbar!=curbar)
     {
      lastbar=curbar;
      return(true);
     }
   return(false);
  }

CNevBar *eurusd_H1; // объявим экземпляр класса для симмвола EURUSD
CNevBar *eurusd_M15; // объявим экземпляр класса для симмвола EURUSD

CNevBar *audusd_M30; // объявим экземпляр класса для симмвола AUDUSD
CNevBar *audusd_M5; // объявим экземпляр класса для симмвола AUDUSD

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   eurusd_H1=new CNevBar();// создадим экземпляр класса для симмвола EURUSD
   audusd_M30=new CNevBar();// создадим экземпляр класса для симмвола AUDUSD
   eurusd_M15=new CNevBar();// создадим экземпляр класса для симмвола EURUSD
   audusd_M5=new CNevBar();// создадим экземпляр класса для симмвола AUDUSD

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   delete eurusd_H1;// удалим экземпляр класса для симмвола EURUSD
   delete audusd_M30;// удалим экземпляр класса для симмвола AUDUSD
   delete eurusd_M15;// удалим экземпляр класса для симмвола EURUSD
   delete audusd_M5;// удалим экземпляр класса для симмвола AUDUSD

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

   if(eurusd_H1.new_bar("EURUSD",PERIOD_H1))
     {
      Alert("EURUSD"," Новый бар на Н1");
     }

   if(eurusd_M15.new_bar("EURUSD",PERIOD_M15))
     {
      Alert("EURUSD"," Новый бар на M15");
     }

   if(audusd_M30.new_bar("AUDUSD",PERIOD_M30))
     {
      Alert("AUDUSD"," Новый бар на M30");
     }
   if(audusd_M5.new_bar("AUDUSD",PERIOD_M5))
     {
      Alert("AUDUSD"," Новый бар на M5");
     }

//---

  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

Test result

2016.02.20 21:09:43     2015.01.10 00:00  тест3 test started
2016.02.20 21:09:45     2015.01.12 00:00  тест3 EURUSD,M1: Alert: EURUSD Новый бар на M15
2016.02.20 21:09:47     2015.01.12 00:00  тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M30
2016.02.20 21:09:50     2015.01.12 00:00  тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.20 21:09:53     2015.01.12 00:05  тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.20 21:09:59     2015.01.12 00:10  тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.20 21:10:04     2015.01.12 00:15  тест3 EURUSD,M1: Alert: EURUSD Новый бар на M15
2016.02.20 21:10:04     2015.01.12 00:15  тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.20 21:10:10     2015.01.12 00:20  тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.20 21:10:13     2015.01.12 00:25  тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.20 21:10:16     2015.01.12 00:30  тест3 EURUSD,M1: Alert: EURUSD Новый бар на M15
2016.02.20 21:10:16     2015.01.12 00:30  тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M30
2016.02.20 21:10:16     2015.01.12 00:30  тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.20 21:10:23     2015.01.12 00:35  тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.20 21:10:26     2015.01.12 00:40  тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.20 21:10:29     2015.01.12 00:45  тест3 EURUSD,M1: Alert: EURUSD Новый бар на M15
2016.02.20 21:10:29     2015.01.12 00:45  тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.20 21:10:34     2015.01.12 00:50  тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.20 21:10:36     2015.01.12 00:55  тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.20 21:10:39     2015.01.12 01:00  тест3 EURUSD,M1: Alert: EURUSD Новый бар на Н1
2016.02.20 21:10:39     2015.01.12 01:00  тест3 EURUSD,M1: Alert: EURUSD Новый бар на M15
2016.02.20 21:10:39     2015.01.12 01:00  тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M30
2016.02.20 21:10:39     2015.01.12 01:00  тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.20 21:10:46     2015.01.12 01:05  тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.20 21:10:50     2015.01.12 01:10  тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.20 21:10:55     2015.01.12 01:15  тест3 EURUSD,M1: Alert: EURUSD Новый бар на M15
2016.02.20 21:10:55     2015.01.12 01:15  тест3 EURUSD,M1: Alert: AUDUSD Новый бар на M5
2016.02.20 21:10:57     2015.01.12 01:17  Tester: stop button pressed