AccountNumber() Problem

 

Hi

I have a condition in OnInit to check the AccountNumber.

int OnInit()
  { 
   if(AccountNumber() != 525490)
     {
      Comment("################  The Account Number Is Not Authorized!! ################");
      return(INIT_FAILED);
     }
//--- 
   return(INIT_SUCCEEDED);
  }


It works correctly, but when I close the MT4 and re_open it, I have to drag the indicator to the chart again, If not, the indicator won't work.

I noticed that the OnInit function works before the account gets signed in, So, at that time AccountNumber() returns Zero and the condition returns false and the rest of my codes removes the indicator from the chart.

I also tried this:

int OnInit()
  {
//--- indicator buffers mapping
   do
     {
      Sleep(1000);
     }
   while(AccountNumber() == 0);
   if(AccountNumber() != 525490)
     {
      Comment("################  The Account Number Is Not Authorized!! ################");
      return(INIT_FAILED);
     }
//--- 
   return(INIT_SUCCEEDED);
  }

But it didn't help and make the MT4 freezing.

Please help me understand what can I do.


Regards

 
Don't check in OnInit. Check in OnCalculate and then remove it ChartIndicatorDelete - Chart Operations - MQL4 Reference

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

  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
 
William Roeder:
Don't check in OnInit. Check in OnCalculate and then remove it ChartIndicatorDelete - Chart Operations - MQL4 Reference

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

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

Thanks  William Roeder

Very helpful

Reason: