MT4 logout/login callback?

 

<Deleted - Please Only Post in English on This Forum>

--

Hello! There is a script that works in the background (while true), and when switching accounts in the terminal continues to work ... and I need to restart it!

Question: how do I catch logout or login? Or how resource-intensive is the AccountNumber () function to use it in while? Can there be a predefined variable for it by the analogy _Digits, _Point, etc ...?

 

Hello please see:

TERMINAL_CONNECTED

Connection to a trade server

TerminalInfoInteger


https://www.mql5.com/en/docs/check/terminalinfointeger
Documentation on MQL5: Checkup / TerminalInfoInteger
Documentation on MQL5: Checkup / TerminalInfoInteger
  • www.mql5.com
Checkup / TerminalInfoInteger - Reference on algorithmic/automated trading language for MetaTrader 5
 
Marco vd Heijden:

Hello please see:

TERMINAL_CONNECTED

Connection to a trade server

TerminalInfoInteger


https://www.mql5.com/en/docs/check/terminalinfointeger

I think this is another quite unnecessary request unless it is used in OnTimer or in OnInit.

If you get a tick in OnCalculate(), OnTick() you are connected (you get true) if you are not connected you don't get a tick and neither function start to work - so you'll never ever get a false!!

And there is nothing provided by mt4 (and mt5 I guess) to determine how long a 'no-connection' time span has been in order to decide to request missed data and re-calculate you values.

Mt4 and Mt5 are offering TerminalInfoInteger(TERMINAL_PING_LAST). But unfortunately this runs in a separate thread so you can catch a no-connection time span only randomly(!) - which would be different if mt4 and mt5 would offer not only the last ping but the previous ping as well - but as I heard: We have no intention...

 

Of course you can also run a watchdog timer with a x sec timeout but markets are sometimes quiet with very little ticks and your logic can falsely assume that the connection was lost so it's a good thing to actually check the connection.

 
Matvey Alekseev:

<Deleted - Please Only Post in English on This Forum>


Hello! There is a script that works in the background (while true), and when switching accounts in the terminal continues to work ... and I need to restart it! Question: how do I catch logout or login? Or how resource-intensive is the AccountNumber () function to use it in while? Can there be a predefined variable for it by the analogy _Digits, _Point, etc ...?

  1. Perhaps you should use the Russian forum Русский
  2. You can't use AccountNumber in a while. If you don't exit all loops, the code will be forcefully terminated after three seconds. You can test IsStopped and exit any loops.
  3. When switching accounts, the EA will go through a Deinit/Init cycle. You can't even look at the account number in Init because there may be no connection to the server yet.  When you get the next tick, the account has been logged in. Only then are you good to go. You can not run anything through the switch or catch the logout/login.
  4. Any script that was running is gone.
IsStopped - Checkup - MQL4 Reference
IsStopped - Checkup - MQL4 Reference
  • docs.mql4.com
IsStopped - Checkup - MQL4 Reference
 

Thanks for all the answers! I found a solution!

         int account_number = AccountNumber();
         
         while(!IsStopped())
         {
            if(AccountNumber()!=account_number)
            {
               Print("Account was changed... please restart.");
               break;
            }
	    
	    //service work...
         } 

Reason: