Search code examples
mql4metatrader4

AccountInfo() and SymbolInfo() return 0 on MT4 startup


When I attach a custom indicator to a chart, close MT4 and reopen it, the indicator initialises normally but every instance of AccountInfo() or SymbolInfo() in the first run of start() returns 0.0, causing several functions to throw a 'zero divide' error. When I reinitialise the indicator (without closing MT4), AccountInfo() and SymbolInfo() return the values they usually do. If I comment out all functions that are dependent on these two, the indicator initialises without throwing errors after restarting MT4.

Has anybody had a similar issue?

To clarify: the problem only arises when I attach the indicator to the chart, close MT4 and reopen it again; when I attach it when MT4 is already open, AccountInfo() and SymbolInfo() return normal values.

Additional information:

  • using #property strict
  • using start() instead of OnCalculate() (so I can run the main function manually without waiting for a new tick)
  • the requested account or symbol property has no influence on the problem

Solution

  • It happens quite often that some data is not available in MT4 at some moment of time. The best thing you can do is to check whether the result is accepted (>0 if you call time, quotes, other data that cannot be zero) or to check the last error, then Sleep(50) and try again. Most likely 2-5th attempt is successful so you need that in a loop.

    It is possible that you need to know at least Account Number that cannot be zero. After you receive succesful result, all other data seems to loaded correctly.

    int OnInit()
       {
        if(!initializeAccountInfo())
           return(INIT_FAILED);
        // ... other checks that you need
        return(INIT_SUCCEEDED);
       }
    bool initializeAccountInfo()
       {
        int accountNumber=0, attempt=0, ATTEMPTS=50, SLEEP(50);
        while(attempt<ATTEMPTS)
           {
            accountNumber=AccountInfoInteger(ACCOUNT_LOGIN);
            attempt++;
            if(accountNumber==0)
               Sleep(SLEEP);
            else
               break;
           }
        return accountNumber>0;
       }