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

 
Aleksey Mavrin:

There is such a thing in MT4, I do not know whether when OnInit is triggered after changing the account, the environment has not had time to update the account number or some other bug, I decided to timer

a lot of things go wrong if the terminal with running EA is reloaded

imho, if the code is not for tester, then all initialization is better to do when the first tick comes - then 99.9% of everything will be initialized correctly

 
Igor Makanu:

a lot of things go wrong if the terminal with a running EA is overloaded

imho, if the code is not for a tester, then all initialization is better to do when the first tick comes - then 99.9% of everything will be initialized correctly

I agree, but probably there is 0,1% ))

 
Igor Makanu:

a lot of things go wrong if the terminal with a running EA is overloaded

imho, if the code is not for tester, then it's better to initialize everything when the first tick comes - then 99.9% of everything will be initialized correctly

better to do all initialization when first tick comes - then 99.9% of things will be initialized correctly... how is it??

 
ponochka:

all initialisations are best done on the arrival of the first tick - then 99.9% of everything will be initialised correctly... how is it??

void OnTick()
{
   static bool first_run = true;
   if(first_run)
   {
      MyOnInit();
      first_run = false;
   }
 
Igor Makanu:

You can do it straightforwardly, avoiding the 'My' kind of padding

int OnInit()
  {
   ...
    account=AccountNumber();
   ...
  }

void OnTick()
{
  static bool first_run = true;
   if(first_run)
   {
     if(account==0) { // Если терминал не успел получить данные с сервера
      OnInit(); // пробуем снова всё инициализировать
      return;
     }
      first_run = false;
   }
 
Vitaly Muzichenko:

You can directly do this by avoiding padding in the form of "My"

it's not padding, it's structured code.

Your example is an example of how lazy to write an extra couple of letters and get unsystematic code - here at least 2 times will be called OnInit() when running EA, not the fact that it will be necessary, or rather not violate what was written before - the level of questions you saw, right? )))

 
Igor Makanu:

it's not a pad, it's structured code

Your example is an example of how lazy to write an extra couple of letters and get unstructured code - here at least 2 times will be called OnInit() at startup EA, not the fact that it will be necessary, or rather will not break what was written before - the level of questions you saw, right? )))

first_run = true;

You also need to check in Init at first initialization and maybe already there "zero".

Otherwise yes, I agree.


P.S. I don't agree, it won't be called 2 times if you managed to connect to the server and get actual data from it.

 
Igor Makanu:

does not work, because it is written in the documentation thatOnTick() does not work in indicators!

 
ponochka:

doesn't work, because the documentation says thatOnTick() doesn't work in indicators!

Well, this is the limit :(

Replace OnTick() with

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
And I'll go to the other branch.
 
Vitaly Muzichenko:

Well, that's the limit :(

I wrote above, about the level of questions ;)

And you started looking for optimality of initialization in OnTick/OnInit - everything is simpler there, you need protection for the found code to share with the same friends ))))

Reason: