Questions from Beginners MQL5 MT5 MetaTrader 5 - page 681

 
post_ek:
So is it possible to trade MT5 shares in Otkritie or not?
You can! Only it will be either stocks or futures (only one MT5 platform is given for free). For the second one, you will have to pay a monthly fee. As far as I remember, it is so. Look for details in my personal account at Otkritie.
 
Alexey Kozitsyn:

Vladimir, don't confuse the man!

From the documentation:

https://www.mql5.com/ru/docs/basis/variables/global
The habit of uninitialising variables is detrimental, as this behaviour often leads to a "black swan". That's why you should make it a rule - create a variable, initialise it at once.
 
Vladimir Karputov:
The habit of uninitialising variables is detrimental, because this behaviour often results in a "black swan". Therefore, the rule of thumb is to create a variable and initialise it immediately.
Stop, stop, stop. Let's not confuse "to make a rule" and"it is necessary to explicitly initialize (assign values to) these variablesin OnInit()" - which is frankly untrue!
 
Alexey Kozitsyn:
Whoa, whoa, whoa. Let's you not confuse "take it as a rule" and"you must explicitly initialise (assign values to) these variablesin OnInit()" - which is blatantly untrue!
You don't have to initialise the variables - the money is yours.
 
Vladimir Karputov:
You don't have to initialise the variables - the money is yours.
Once again. Don't confuse opportunities with responsibilities!
 

Vladimir is partly right.

Here's an example,

Once upon a time, I don't remember when, I declared a variable

bool STOP;

and actually use it in OnTick .

By default, the bool should be false

I did so:

void OnTick ()

{

if(!STOP) {Print("STOP");return;}

}

The code failed to work because I hadn't specified boolSTOP=false;

This situation occurred once, on some build, and I didn't reproduce it later (there was some bug with the build or something else - I don't know).

 
Vladislav Andruschenko:

Vladimir is partly right.

I am by no means calling to leave variables uninitialised. But, when you give advice to a newbie, don't say that your advice is the only right solution, especially since it's not.
 
Vladimir Karputov:
The habit of variables uninitialization is detrimental, because this behavior often leads to a "black swan". Therefore, you should make it a rule - create a variable, initialize it immediately.

You can lose variable values when switching timeframes during their initialization in OnInit() - hence possible errors in program logic.

//+------------------------------------------------------------------+
//|                                    exTextInitGlobalVariables.mq5 |
//|              Copyright 2015, Artem A. Trishkin, Skype artmedia70 |
//|                       https://login.mql5.com/ru/users/artmedia70 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, Artem A. Trishkin, Skype artmedia70"
#property link      "https://login.mql5.com/ru/users/artmedia70"
#property version   "1.00"
//--- input parameters
input int      TestInput=0;            // Внешняя переменная
int testInputGlobal=TestInput;         // Значение внешней переменной, присвоенное на глобальном уровне
int testInputInit;                     // Значение внешней переменной, присвоенное в OnInit()

//--- global variables
int   TestValueGlobalNoInitNo;         // Не инициализирована значением нигде
int   TestValueGlobalYesInitNo=0;      // Инициализирована значением 0 при объявлении, не инициализирована в OnInit()
//---
int   TestValueGlobalNoInitYes;        // Не инициализирована значением при объявлении, инициализирована в OnInit() нулём
int   TestValueGlobalYesInitYes=0;     // Инициализирована нулём и при объявлении, и в OnInit()
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   testInputInit=TestInput;
   TestValueGlobalNoInitYes=0;
   TestValueGlobalYesInitYes=0;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   testInputInit++;
   testInputGlobal++;
   TestValueGlobalNoInitNo++;
   TestValueGlobalNoInitYes++;
   TestValueGlobalYesInitNo++;
   TestValueGlobalYesInitYes++;
   Comment
         (
         "\n"+"============================================="+
         "\n"+"Значение внешней переменной TestInput(",TestInput,"), присвоенное в OnInit(): ",(string)testInputGlobal+
         "\n"+"Значение внешней переменной TestInput(",TestInput,"), присвоенное на глобальном уровне: ",(string)testInputGlobal+
         //---
         "\n"+"============================================="+
         "\n"+"Не инициализирована значением нигде: ",(string)TestValueGlobalNoInitNo+
         "\n"+"Инициализирована значением 0 при объявлении, не инициализирована в OnInit(): "+(string)TestValueGlobalYesInitNo+
         //---
         "\n"+"============================================="+
        "\n"+"Не инициализирована значением при объявлении, инициализирована в OnInit() нулём: "+(string)TestValueGlobalNoInitYes+
         "\n"+"Инициализирована нулём и при объявлении, и в OnInit(): "+(string)TestValueGlobalYesInitYes
         );
  }
//+------------------------------------------------------------------+
 
Artyom Trishkin:

You can lose variable values when switching timeframes during their initialization in OnInit() - hence possible errors in program logic.

If you do not take into account:

Init

Immediately after the client terminal loads the program (Expert Advisor or custom indicator) and starts the initialization of global variables, the Init event will be sent, which is handled by theOnInit() function, if it has one. This event is also generated after a security and/or chart period change, after recompiling the program in MetaEditor, after a change of input parameters from an Expert Advisor or a custom indicator setting window. The Expert Advisor is also initialized after the account is changed. The Init event is not generated for scripts.

then you can make anything you want.

 
Vladimir Karputov:

If you don't take into account:

Init

Immediately after the client terminal loads a program (an Expert Advisor or a custom indicator) and starts the initialization of global variables, the Init event will be sent, which is handled byOnInit(), if it has one. This event is also generated after a security and/or chart period change, after recompiling the program in MetaEditor, after a change of input parameters from an Expert Advisor or a custom indicator setting window. The Expert Advisor is also initialized after the account is changed. The Init event is not generated for scripts.

You can do anything you want.

I speak of Thomas, he speaks of Yours...

How do you communicate in such a situation?

I speak about a pernicious habit of thoughtlessly initializing global variables in OnInit().

At the same time you impose your detrimental habit to all newcomers in an unquestioning form. You're breeding a generation of E.S.T.'s? Clones of those who don't think, but blindly follow their sensei?

Reason: