Is it possible to close MT4 from code?

 

I have an EA that talks to a server, but sometimes a communication error might happen, and the EA can have a hard time recovering after, so it would be better to restart the MT4 instance. Is there some way I can close the chart AND MT4 itself to enable that?


TIA!


Dennis Gundersen

 

Sure, that's possible. But you didn't give enough details. What do you mean by "communication error?". Are we talking failing a ping? Are we talking delay?

It's simple enough to make an EA that closes MetaTrader if pinging an IP fails, but if we are talking "communication error" as in the EA should receive news data and tick data but sometimes it misses one news event. Well, that's far more difficult.

void OnTick() {
Inputs(ip("Server IP", "8.8.8.8"), period("Check period in minutes", 60));
Vars(bRestartMt4(false), restartCount(0));

string Server = ip;
int Interval = period * 1000 * 60; // Convert to ms
    if (pingFailed) {
        Print("Ping failed. Restarting MT4...");
        SystemRestart();
    }
}

The better solution would be fix whatever the heck is causing this communication error, as restarting MetaTrader isn't really a solution. MetaTrader 4 is already kinda a piece of junk that was built in 2005, I'm fairly confident the process of restarting it over and over will cause more issues.

 
James McKnight #:

Sure, that's possible. But you didn't give enough details. What do you mean by "communication error?". Are we talking failing a ping? Are we talking delay?

It's simple enough to make an EA that closes MetaTrader if pinging an IP fails, but if we are talking "communication error" as in the EA should receive news data and tick data but sometimes it misses one news event. Well, that's far more difficult.

The better solution would be fix whatever the heck is causing this communication error, as restarting MetaTrader isn't really a solution. MetaTrader 4 is already kinda a piece of junk that was built in 2005, I'm fairly confident the process of restarting it over and over will cause more issues.

I've written a server console app using ZeroMQ to push string[] back and forth to MT4. The console app is pretty stable, but occasional handshake and dropped frame errors do occur when API calls are involved, (and as you said, MT4 is "special"). A typical example would be the first tick of a new week causing a timeout as the web app running the API needs time to spin back up. I'm working on bug fixes for this, but in the meantime, a sample "SystemRestart()" would be much appreciated.

Re

Dennis

 
sgude0: the EA can have a hard time recovering after, so it would be better to restart the MT4 instance. Is there some way I can close the chart AND MT4 itself to enable that?

Fix your broken EA.

EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static / global ticket variables will have been lost. You will have an open order / position but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover?

Use a OrderSelect / Position select loop on the first tick, or persistent storage (GV+flush or files) of ticket numbers required.

On a network disconnection, you might get ERR_NO_RESULT or ERR_TRADE_TIMEOUT. There is nothing to be done, but log the error, return and wait for a reconnection and a new tick. Then reevaluate. Was an order opened or is the condition still valid.

 
William Roeder #:

Fix your broken EA.

While that's a valid opinion, it's hardly a reply to my question. Being able to close down an app from code, is hardly a unique requirement.

Re

Dennis

 
James McKnight #:

Sure, that's possible. But you didn't give enough details. What do you mean by "communication error?". Are we talking failing a ping? Are we talking delay?

It's simple enough to make an EA that closes MetaTrader if pinging an IP fails, but if we are talking "communication error" as in the EA should receive news data and tick data but sometimes it misses one news event. Well, that's far more difficult.

what is that?  

    if (pingFailed) {
        Print("Ping failed. Restarting MT4...");
        SystemRestart();

there is no such function in MT4