Retrieving Account Number in EA after starting MT4 from command line using Configuration at Startup

 
Hi,

Can anyone help with retrieving an account number using the AccountNumber() function in an Expert Advisor that was launched using the Configuration at Startup feature? It looks like the EA gets initialized before account login is complete and when I use the AccountNumber() function from OnTick() or OnInit() it returns 16384 which is the default value.

If I kick off the tester again manually by pressing the "Start" button, it works fine. Adding delays does not help, it seems to keep the account number the EA was initialized with for the duration of the test.

I have seen the advice below from @William Roeder but calling the function from OnTick() return the same result.

Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:

Terminal starts.
Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
OnInit is called.
For indicators OnCalculate is called with any existing history.
Human may have to enter password, connection to server begins.
New history is received, OnCalculate called again.
New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

Code below:

bool IsAuthenticated = false;

void Login(string FunctionName)
   {
      if (AccountName() == "")
      {
         Print("Account number unavailable. Login failed");
      }
      else
      {
         IsAuthenticated = true;
         Print("Authentication successful from " + FunctionName);
      }
   }

int OnInit()
  {
//---
   Login("OnInit");      
   

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if (!IsAuthenticated) 
   {
      Print("Logged in?: " + string(AccountInfoInteger( ACCOUNT_LOGIN )));   
      Print("Account number: " + string(AccountNumber()));
      Login("OnTick"); 
   }
  }
 
In future please post in the correct section
I will move this topic to the MQL4 and Metatrader 4 section.
 
Keith Watford:
In future please post in the correct section
I will move this topic to the MQL4 and Metatrader 4 section.

@KeithWatford Hello Can you provide the link to the solution of the above question?