Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1566

 
Mikhail Tkachev:

If you have an EA of three lines, then of course you don't need OOP).

But if I had to create Goblin with one arm and three legs, and then with one leg and three arms - then, yes, that's where OOP would really help me out. Although... I don't know either.

What's the point of having it here?

And it doesn't matter at all how many "lines" you have. ;)
 
Mikhail Tkachev:

Only int or double?
What if the variable is an object of a class?
And if number of such objects is unknown beforehand [before execution of OnInit()]?
And if the parameters of constructors in advance [before OnInit()] are unknown?

You gave a good link and example about declaration of classes.

In any case, the global ones are declared before OnInit(); the others are declared each time they are needed

 
MakarFX:

In any case, the Global ones are declared before OnInit(), the others are declared each time they are needed

This is clear in theory.
In practice, after :

declaration of 'NB_M1' hides global variable -> per line in OnInit()

see previous declaration of 'NB_M1'

the expert works as if NB_M1 was global (and correctly initialized), even though it is hidden by a local declaration and initialized as local in OnInit().

By idea, it shouldn't work at all.

 
Mikhail Tkachev:

In theory, this makes sense.
In practice, after :

declaration of 'NB_M1' hides global variable -> per line in OnInit()

see previous declaration of 'NB_M1'

the expert works as if NB_M1 was global (and correctly initialized), although it is hidden by the local declaration in OnInit().

By idea, it shouldn't work at all.

What is NB_M1? Perhaps it is already declared in the library? And without any code at all, like deaf with blind)
 
Сергей Таболин:


And it doesn't matter how many "strings" you have. ;)

When you have a hundred or two lines of code, because you want to start with this, then forget what this is for, then add this, then naturally (?) the thought will arise: "How to organize it?" Or not).

 
MakarFX:
What is NB_M1? Perhaps it is already declared in the library? And without code at all, it's like deaf and blind)

NB_M1 is an object of class CIsNewBar to define the start of a new bar for each character. No, it is not declared in the library.
Code in first post :https://www.mql5.com/ru/forum/160683/page1547#comment_23712294

 
Mikhail Tkachev:

NB_M1 is an object of class CIsNewBar to define the start of a new bar for each character. No, it is not declared in the library.
The code in the first post :https://www.mql5.com/ru/forum/160683/page1547#comment_23712294

So, NB_M1 is declared inside the class and can be accessed there

Can we have a look at the code of this class?

 
NB_M1 is an object of this class. How to put it inside ?
//+------------------------------------------------------------------+
//|                                                     IsNewBar.mqh |
//|                               Copyright © 2011, Nikolay Kositsin |
//|                              Khabarovsk,   farria@mail.redcom.ru | 
//+------------------------------------------------------------------+ 
#property copyright "2011,   Nikolay Kositsin"
#property link      "farria@mail.redcom.ru"
#property version   "1.00"
//+------------------------------------------------------------------+
//|  Алгоритм определения момента появления нового бара              |
//| Для каждого таймфрейма каждого символа нужно создавать объект    |
//+------------------------------------------------------------------+  
#include <Object.mqh>

class CIsNewBar:public CObject // Сделан наследником класса для возможности работы с классом CArrayObj (из библиотеки)
// class CIsNewBar   // Первоначальная редакция
  {
   //----
public:

      //---- функция определения момента появления нового бара
   bool IsNewBar()
     {
      //---- получим время появления текущего бара
      datetime TNew=datetime(SeriesInfoInteger(m_Symbol,m_TimeFrame,SERIES_LASTBAR_DATE));

      if(TNew!=m_TOld && TNew) // проверка на появление нового бара
        {
         m_TOld=TNew;
         return(true); // появился новый бар!
        }
      //----
      return(false); // новых баров пока нет!
     };

   //---- конструктор класса    
                     CIsNewBar(const string &pSymbol, const ENUM_TIMEFRAMES pTimeFrame){
                        m_Symbol=pSymbol; m_TimeFrame=pTimeFrame;
                        m_TOld=-1;};

protected:
   datetime          m_TOld;        // Время хранится 
   ENUM_TIMEFRAMES   m_TimeFrame;   //    для каждого таймфрейма
   string            m_Symbol;      //    каждого символа

   //---- 
  };
//+------------------------------------------------------------------+
 
Mikhail Tkachev:
NB_M1 is an object of this class. How to put it inside ?
what is this NB_M1 variable for ?
 
Mikhail Tkachev:
NB_M1 is an object of this class. How to put it inside ?

I think that's the way it should be

bool NB_M1;
int OnInit()
{
}

void OnTick()
{
   NB_M1=new CIsNewBar(_Symbol,PERIOD_M1);  // Эта строка потом будет в цикле для разных символов
   if (NB_M1)  // (1) Если появился новый бар M1
        {....}
    ......
}
Reason: