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

 
I have all sorts of nonsense like indicator parameters etc. as parameters in my class. In general, things that will be static and the same. So why should I pass these values every time I create an object?
 
STARIJ:

is this shorter? bool barup=... WindowRedraw(); will still execute - taken from if

it can also be shorter ... see my file

Thank you. How do I correctly set the interval at which Fibo levels are drawn? If it is (time) 9.00, it should draw levels from 8.00 to 8.59 on М1.
 
Juer:

Kind of like the statement of the question is clear, isn't it. The point is to initialise the same thing.

How are you going to initialise something that doesn't already exist? After the object is created, it starts to exist, and that's when it can be initialized.

I told you right away - make an object a parametric constructor, and pass to it the required parameters, which will be used to initialize the newly created object.

You can pass parameters through a structure, which you fill with required (constant for all such objects) data in OnInit() of the program. Then, in OnTick(), for example, or in any other standard handler, you create your objects, but when you create it you write the following:
CMyObject object = new CMyObject(a_here_structure_with_set_parameters);

Or, if the object data doesn't change from program to program and always has the same values, then explicitly initialize them with the required parameters in the constructor through the initialization list, as Igor said above:

Forum on Trading, Automated Trading Systems and Strategy Testing

Any questions from newbies on MQL4, help and discussion on algorithms and codes

Ihor Herasko, 2018.03.28 17:31

This is done in the initialization list, which is located in the class constructor:

class CCandle
{
   int      m_nA;
   double   m_fB;
   datetime m_dtC;


   void CCandle::CCandle(void);
};

CCandle::CCandle(void)
            : m_nA(0)
            , m_fB(0.0)
            , m_dtC(0)
{
}

 
Artyom Trishkin:

How are you going to initialise something that doesn't already exist? After the object is created, it starts to exist, and that's when it can be initialized.

I told you right away - make an object a parametric constructor, and pass the required parameters to it, which will be used to initialize the newly created object.

You can pass parameters through a structure, which you will fill with the required (constant for all such objects) data in OnInit() of the program. Then, in OnTick(), for example, or in any other standard handler, you create your objects, but at creation you write the following:
CMyObject object = new CMyObject(a_here_structure_with_set_parameters);

Yes, I see. Thank you. Well somehow just thought there was some solution so that when an object is created it has some pre-set parameters right away without having to pass them to the constructor or whatever. Through some parent class or something.

 
Juer:
In my class I have all sorts of stuff like indicator parameters etc. as parameters. In general, things that will be static and the same. So why should I pass these values every time I create an object?

So don't pass it on. Initialize in the initialization list, as I've shown. At the moment of creating an instance of the class, the class constructor will be automatically called, which will initialize all members of the class, specified in the initialization list. Put the Print function in the CCandle method and you'll see how the constructor is called without any reference to it.

 
Juer:

Yes, I see. Thank you. Well somehow just thought there was some solution, so that when an object is created it has some pre-set parameters right away without having to pass them to the constructor or whatever. Via some parent class or something.

Again. You can do it without transferring the data to the constructor.

 
Ihor Herasko:

So don't pass it on. Initialize in the initialization list, as I've shown. At the moment of creating an instance of the class, the class constructor will be automatically called, which will initialize all members of the class, specified in the initialization list. Put the Print function in CCandle method and you'll see how the constructor is called without any reference to it.

I don't get it, what difference does it make whether to pass them through the constructor or through the initialization function?

 
Ihor Herasko:

So don't pass it on. Initialize in the initialization list, as I've shown. At the moment of creating an instance of the class, the class constructor will be automatically called, which will initialize all members of the class, specified in the initialization list. Put the Print function in CCandle method and see how the constructor is called without any reference to it.

What will they be initialized with? With what values? I don't understand .

Suppose, I have initial values in the Expert Advisor input parameters. So, I need to pass them to each object of the class. But not to pass them to each newly created object.

 

how come in this cycle my limit order gets deleted only if after placing a limit order on the next bar another limit order has not been placed, i.e. a limit order gets deleted only if it is placed and then followed by two bars with no limit orders

 int i;
   for(i=0;i<=OrderTicket();i++)
     {
      takelimit=OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES);

      if(iBarShift(Symbol(),0,OrderOpenTime())==2)//здесь нужно что бы на 2-ом баре если есть ордер его удалить, но удаляется он только если после его установки небыло на след баре еще лимитника
        {
         deletelimit=OrderDelete(ticket);
         if(!deletelimit)
           {
            Comment(GetLastError());
           }
        }
     }
if(условие для байлимита)
{ордерсенд}
else if(условие для селлимита)
{ордерсенд}
 
Juer:

What will they be initialised with? With what values? Not clear

The ones you write in the initialisation list.

Suppose I have initialized values in input parameters of Expert Advisor. So, I need to pass them to each object of this class. But not to pass them to each newly created object.

Here it is:

input int      i_nA     = 10;
input double   i_fB     = 20.0;
input datetime i_dtC    = D'2018.03.08 10:12:14';

class CCandle
{
   int         m_nA;
   double      m_fB;
   datetime    m_dtC;
   
public:
                     CCandle(void);
};

CCandle::CCandle(void)
         : m_nA(i_nA)
         , m_fB(i_fB)
         , m_dtC(i_dtC)
{
   Print("Создан объект со значениями: A = ", m_nA, ", B = ", m_fB, ", C = ", m_dtC);
}

int OnInit()
{
   CCandle arrclass[10];
     
   return(INIT_SUCCEEDED);
}

If we run it, we get:

0       22:09:36.706    Test EURUSD,M1 inputs: i_nA=10; i_fB=20.0; i_dtC=1520503934; 
0       22:09:36.995    Test EURUSD,M1: Создан объект со значениями: A = 10, B = 20.0, C = 2018.03.08 10:12:14
0       22:09:36.995    Test EURUSD,M1: Создан объект со значениями: A = 10, B = 20.0, C = 2018.03.08 10:12:14
0       22:09:36.995    Test EURUSD,M1: Создан объект со значениями: A = 10, B = 20.0, C = 2018.03.08 10:12:14
0       22:09:36.995    Test EURUSD,M1: Создан объект со значениями: A = 10, B = 20.0, C = 2018.03.08 10:12:14
0       22:09:36.995    Test EURUSD,M1: Создан объект со значениями: A = 10, B = 20.0, C = 2018.03.08 10:12:14
0       22:09:36.995    Test EURUSD,M1: Создан объект со значениями: A = 10, B = 20.0, C = 2018.03.08 10:12:14
0       22:09:36.995    Test EURUSD,M1: Создан объект со значениями: A = 10, B = 20.0, C = 2018.03.08 10:12:14
0       22:09:36.995    Test EURUSD,M1: Создан объект со значениями: A = 10, B = 20.0, C = 2018.03.08 10:12:14
0       22:09:36.995    Test EURUSD,M1: Создан объект со значениями: A = 10, B = 20.0, C = 2018.03.08 10:12:14
0       22:09:36.995    Test EURUSD,M1: Создан объект со значениями: A = 10, B = 20.0, C = 2018.03.08 10:12:14
0       22:09:36.995    Test EURUSD,M1: initialized
0       22:09:36.995    Test EURUSD,M1: uninit reason 0
0       22:09:37.062    Script Test EURUSD,M1: removed
Reason: