CAccountInfo check if account is valid/open...

 

Hello All,

I am working with a friend on an EA; I design & write the code (what could go wrong? 😂) and he investigates trading scenarios. Our two minds are very different and up to a point it works.

However we have recently encountered a problem which I had handled badly in the code 😳 - and I am about to rectify that problem.

I use CAccountInfo to discover information about the account. All good. However it's only all good until it is not; it looks like the demo account has been expired by ICM and we are getting the following message in the log:

authorization on ICMarketsSC-Demo failed (Invalid account)

Great! but there doesn't seem to be a method/property in CAccoundInfo or in any other call I can find to check the validity/open status of an account.

To my great shame, because I hadn't checked a divisor the EA was being killed with a divide by zero failure. Yup, it's shameful, I am embarrassed.

So I want to be able to check the account validity/open status.

Google searches aren't proving helpful and I can't find what I want in the documentation; please can some kind individual put me out of my misery and show me the way.

With my embarrassed best regards, ESB.

 

An invalid account is no more connected, so you can check with :

TerminalInfoInteger(TERMINAL_CONNECTED)
Of course, no connection doesn't mean the account is invalid. I don't think there is a way to know that directly.
 
Alain Verleyen #:

An invalid account is no more connected, so you can check with :

Of course, no connection doesn't mean the account is invalid. I don't think there is a way to know that directly.

Thank you Alain,

I just found IsConnected() which looks like the same thing as TerminalInfoInteger(TERMINAL_CONNECTED); I'd like to have found an AccountIsValid() or AccountConnected() function - but hey! I suppose I'll have to make do.

I had a good look at all the AccountInfo functions and what they can do - they seem to have missed this feature, perhaps a feature request is in order? It's not an unreasonable thing to want to check.

Thank you for looking, I'll get back to fixing my bad coding 🙄.

With my best regards and thanks, ESB.

Documentation on MQL5: IsConnected / Standard Library
Documentation on MQL5: IsConnected / Standard Library
  • www.mql5.com
Gets the information about connection to trade server. Return Value true - the terminal is connected to a trade server, otherwise - false. Note...
 

I just added this if it's any use to anybody:

bool connected_can_trade(const bool show_message = false)
{
   if( !TerminalInfoInteger(TERMINAL_CONNECTED) ) {
      if ( show_message )
         printf("%s[%d], %s: Terminal %s is not connected!", __FILE__, __LINE__, __FUNCTION__, TerminalInfoString(TERMINAL_NAME));
      return false;
   }
   if( !AccountInfoInteger(ACCOUNT_TRADE_ALLOWED) ) {
      if ( show_message )
         printf("%s[%d], %s: Trading is forbidden for the account %s", __FILE__, __LINE__, __FUNCTION__, AccountInfoInteger(ACCOUNT_LOGIN));
      return false;
   }        
   return true;
}

I haven't run it yet and it might benefit from using ACCOUNT_TRADE_EXPERT instead of ACCOUNT_TRADE_ALLOWED.

WMBR, ESB.