Questions from Beginners MQL5 MT5 MetaTrader 5 - page 681
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
So is it possible to trade MT5 shares in Otkritie or not?
Vladimir, don't confuse the man!
From the documentation:
https://www.mql5.com/ru/docs/basis/variables/globalThe 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.
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 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:
{
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).
Vladimir is partly right.
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
);
}
//+------------------------------------------------------------------+
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.
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?