Where is the best place to put this code?
Now I put in the OnTick() function but if disconnected(internet) calling OnTick() does not occur!
Which function mql4 call every 1 second?
Only during Trade operations will it make sense to call that function as an extra verification step!
During the rest of the time, the fact that the OnTick() event is being called, already means that it IS connected, so you don't have to check that function!
I want to start and initialize EA from the beginning when disconnected(internet) and re-connected.
So, Which function can I use and where?
In case you are programming a script, I would take
Otherwise the Event for starting the terminal would be
The OnInit() function is the Init event handler. It must be of void or int type, with no parameters:
The Init event is generated immediately after an Expert Advisor or an indicator is downloaded; this event is not generated for scripts. The OnInit() function is used for initialization. If OnInit() has the int type of the return value, the non-zero return code means unsuccessful initialization, and it generates the Deinit event with the code of deinitialization reason REASON_INITFAILED.
To optimize input parameters of an Expert Advisor, it is recommended to use values of the ENUM_INIT_RETCODE enumeration as the return code. These values are used for organizing the course of optimization, including the selection of the most appropriate testing agents. During initialization of an Expert Advisor before the start of testing you can request information about the configuration and resources of an agent (the number of cores, amount of free memory, etc.) using the TerminalInfoInteger() function. Based on the information obtained, you can either allow to use this testing agent, or reject using it during the optimization of this Expert Advisor.
Successful initialization, testing of the Expert Advisor can be continued.
This code means the same as a null value — the Expert Advisor has been successfully initialized in the tester.
Initialization failed; there is no point in continuing testing because of fatal errors. For example, failed to create an indicator that is required for the work of the Expert Advisor.
This return value means the same as a value other than zero - initialization of the Expert Advisor in the tester failed.
This value means the incorrect set of input parameters. The result string containing this return code is highlighted in red in the general optimization table.
Testing for the given set of parameters of the Expert Advisor will not be executed, the agent is free to receive a new task.
Upon receiving this value, the strategy tester will reliably not pass this task to other agents for retry.
No errors during initialization, but for some reason the agent is not suitable for testing. For example, not enough memory, no OpenCL support, etc.
After the return of this code, the agent will not receive tasks until the end of this optimization.
The OnInit() function of the void type always denotes successful initialization.
You might call IsConnected() in event handlers for the timer and chart events (OnTimer() and OnChartEvent())
Those events can trigger even if there is no tick.
Of course, this depends on your application needs.
I need to stop the EA whenever it is disconnected and after the connection is restarted from OnInit().
If the interrupt occurs, the EA will fail.
which system function call whenever no internet connection?
Remember, it takes more than 30 seconds before the network times out and the disconnection is even recognized. Unless the terminal can't reconnect, you're not even going to see it.
To me IsConnected() is one of the most stupid function of mt4 - in mt5 TerminalInfoInteger(TERMINAL_CONNECTED).
It is completely meaningless in OnTick() as this function is called only if the terminal is connected so an EA just cannot know whether the terminal is connected (just no to ticks) or not.
And even after the connection was re-established there is no e.g. error-code or a hint in a different version e.g. date time of the last re-connection and the duration of the disconnection.
So to catch disconnection you have to use OnTimer() and do you own checks while a good system should provide such very important information.
I once wrote this to the service desk and suggested that the terminal should just provide an access to the date/time of last ping and the previous ping, that would allow an EA to determine a posteriori somehow a disconnection (assuming that the terminal checks the connection via pings): We have not intention...