How to re-init EA after internet connection is lost and reestablished? Many Thanks!

 

Hello, everyone,

I have met a problem when coding EA and I was wondering whether you could be kind enough to enlighten me.

The internet connection to the server here is sometimes lost and reestablished automatically.

The following is my understanding of EA working when internet is lost and reestablished.

Please point out if there is something wrong.

When internet connection is lost, the attached EA is still attached to the chart, just 'stopped' because no new tick data is arrived and the Start() function is not executed.

When internet connnection is reestablished, the attached EA is still attached to the chart and when a new tick data arrives the Start() function is executed.

I wish the EA would be able to execute deinit() when internet connnection is lost and execute init() again when internet is reestablished.

I was wondering whether you could be kind enough to help me out.

Thank you so much!!!

 
blackkettle:
I wish the EA would be able to execute deinit() when internet connnection is lost and execute init() again when internet is reestablished.
You must monitor the connection your self in a loop like
int start(){
    _start();   // Do real start.
    while( !(IsTesting() || IsStopped()) ){
        Sleep(1000); 
        if (IsConnected()){         static int connected = 1;
            if (connected != 1){    connected = 1;  _init();    }
            RefreshRates();         _start();   // Do real start
        }
        else if (connected != 0){   connected = 0;  _deinit();  }
    }   // while
}
int deinit(){ _deinit(); }
Note that when the disconnection happens outside of your local network it takes a minute or so before the network times out and isConnected changes.
 

The Obvious solution is using another Script which Always-Looping and checking connections along side Global_Variable_Set to inform the EA of lost connection. Since I already wrote out my example during WHRoeder's post, I'm going to submit it. But I'm not sure if GetLastError() will track the dis-connection for you. Does anyone know if it'll work in this case?

bool _1st_Run;
void init(){_1st_Function();}.
void start(){
    _1st_Function();
}
//~~~~~~~~~~~~~~~~~~~~
void _1st_Function(){
    if(GetLastError()==6){
        //ERR_NO_CONNECTION
        _1st_Run=false;
    }
    if(_1st_Run==false){
        _1st_Run=true;
        do_something;
    }
}
 
ubzen:

The Obvious solution is using another Script which Always-Looping and checking connections along side Global_Variable_Set to inform the EA of lost connection. Since I already wrote out my example during WHRoeder's post, I'm going to submit it. But I'm not sure if GetLastError() will track the dis-connection for you. Does anyone know if it'll work in this case?

Doubt it. The varaible GetLastError returns and clears won't be set unless and until you issue a server call that fails.
 
WHRoeder:
You must monitor the connection your self in a loop like Note that when the disconnection happens outside of your local network it takes a minute before the network times out and isConnected changes.
How is a loop better than simply waiting for new incoming ticks with new refreshed rates?
 
blogzr3:
How is a loop better than simply waiting for new incoming ticks with new refreshed rates?
That should have been directed at the OP - what additional info can you get from a disconnected state that you cannot get from simply testing it within start()?. Is a brief disconnection sufficient to trigger the situation, or do you need a more prolonged timeout?
 

Remember, it's not a brief disconnection. If you disable network at the pc, you'll get notification in about 7 seconds. If you disable at the modem for example to simulate a loss higher up, it will take more than a minute before the network times out.

As for the why question, perhaps the OP will enlighten us.

 
WHRoeder:
You must monitor the connection your self in a loop like Note that when the disconnection happens outside of your local network it takes a minute or so before the network times out and isConnected changes.

Thank you so much, WHRoeder!

I am so apperciative of your kindness and help!

Below is the code. Hope it would be helpful to others.

Thank you again!

//+------------------------------------------------------------------+
//|                                                   netconn_02.mq4 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   _init();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   _deinit();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   _start();   // Do real start.
    while( !(IsTesting() || IsStopped()) ){
        Sleep(1000); 
        if (IsConnected()){ static int connected = 1;
            if (connected != 1){ connected = 1;  _init();}
            RefreshRates();         _start();   // Do real start
        }
        else if (connected != 0){ connected = 0;  _deinit();}
    }   // while
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

int _init()
{
   Print("initiated!");
   return(0);
}

int _start()
{
   Print("started!");
   return(0);
}

int _deinit()
{
   Print("deinitiated!");
   return(0);
}
 
Click here, dabbler knows something https://www.mql5.com/en/forum/138987
 

Here an update for the friends of MQ5:

#include <Tools/Auxiliary.mqh>
#include <Trade/TerminalInfo.mqh>

CTerminalInfo terminalInfo;

void OnStart()
{
        checkConnectivity();
}

void checkConnectivity()
{
        static bool connectionLost = false;

        while( !(IsTester() || IsOptimization() || IsStopped()) )
        {
                Sleep(1000);
                
                if (terminalInfo.IsConnected())
                {
                        if (connectionLost)
                        {
                                connectionLost = false;
                                Print("Connection re-established.");
                        }
                }
                else
                {
                        if (!connectionLost)
                        {
                                connectionLost = true;
                                Print("Connection lost.");
                        }
                }
        }
}

Copy above code into a script and drag it into your chart to get updates on connection loss and when connection re-establishes.

The log messages will look like this then:

2018.10.02 20:23:21.976 ConnectivityChecker (Usa500,D1) Connection lost.

2018.10.02 20:23:22.976 ConnectivityChecker (Usa500,D1) Connection re-established.


Hope this helps! :)

 
Admiral Thrawn:

Here an update for the friends of MQ5:

Copy above code into a script and drag it into your chart to get updates on connection loss and when connection re-establishes.

The log messages will look like this then:

2018.10.02 20:23:21.976 ConnectivityChecker (Usa500,D1) Connection lost.

2018.10.02 20:23:22.976 ConnectivityChecker (Usa500,D1) Connection re-established.


Hope this helps! :)

Hi, could you help me I don´t have the file "Auxiliary.mqh"