Download MetaTrader 5

How do I close an Expert programmatically?

To add comments, please log in or register
Miroslav Popov
365
Miroslav Popov  

Hello,


I wont to close an expert from the init() function in certain conditions. Is there a command, which can do it?

It must be simple but I cannot figure out.


Thank you!

Spock Mann
16
Spock Mann  

Put a global value (bool) that you can set to true when your conditions are met. You jus thave to skip the program at the start() event, so it wont execute anymore.

You can also set the StopEA=true in the init(), so the start() will skip the first execution as well.

//---- global variables

bool StopEA=false;

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
 {
  if (StopEA == true) { return(0); } //This line will skip the execution of EA.
 }


//somewhere in a function your conditions are met, so you just have to set the StopEA to true.

...
if (<anyconditions>) { StopEA = true; return(0); }
Miroslav Popov
365
Miroslav Popov  

** You jus thave to skip the program at the start() event, so it wont execute anymore.**


That was the first thing I tried but I'm not sure the expert is stopped. It's on the chart keep smiling. Probably MT continues calling start() at every tick.

What I wonna is to remove it completely from the chart.

Miroslav Popov
365
Miroslav Popov  

My expert must run in one chart only. Also it needs additional conditions as DLL allowed. If these conditions are not met I wont to release the global variable and to stop the expert.

In addition I want to be sure that the expert releases the global variable at every stop. I do this in the deinit() function, but if MT crashed, deinit() wasn't called. My current code is here:


#define SERVER_SEMA_NAME "MT4-FST Expert"

bool IsServer;

int init()
{
    string message = "MT4-FST Expert started!";
    Comment(message);
    Print(message);

    if (CheckEnvironment())
        Server();
    
   return (0);
}

int start()
{
    return (0);
}

int deinit()
{
   Comment("");
   ReleaseServerSema();
   
   return (0);
}

///
/// Checks the working conditions.
///
bool CheckEnvironment()
{
    string message;
    
    // Checks if the script wasn't started on another chart.
    IsServer = GetServerSema();
    if (!IsServer)
    {
        message = "MT4-FST Expert is already running on another chart!";
        Comment(message);
        Print(message);
        return (false);
    }
    
    // Checks if DLL is allowed.
    if (IsDllsAllowed() == false)
    {
        message = "DLL call is not allowed. Experts cannot run.";
        Comment(message);
        Print(message);
        ReleaseServerSema();
        return (false);
    }
    
    ...
    ...
    
    // Everithing looks OK.
    return (true);
}

///
/// Checks the global variable in order to determine if
/// the server is running on another chart.
///
bool GetServerSema()
{
    if (GlobalVariableCheck(SERVER_SEMA_NAME))
    {   // Global variable exists.
        double value = GlobalVariableGet(SERVER_SEMA_NAME);
        if (value == 0)
        {   // Error in GlobalVariableGet.
            Print("Error in GlobalVariableGet: ", GetLastError());
            return (SaveServerSema());
        }
        else if (value == -1)
        {   // Server was stopped.
            return (SaveServerSema());
        }
        else if (value < TimeLocal() - GetTickCount() / 1000)
        {   // Global variable has been set before Windows was started.
            return (SaveServerSema());
        }
        else
        {   // Server is working on another chart.
            return (false);
        }
    }

    // Global variable doesn't exist.
    return (SaveServerSema());
}

///
/// Releases the global variable when server stops.
///
void ReleaseServerSema()
{
    if (IsServer)
        GlobalVariableSet(SERVER_SEMA_NAME, -1);
}

///
/// Saves a global variable when server starts.
///
bool SaveServerSema()
{
    if (GlobalVariableSet(SERVER_SEMA_NAME, TimeLocal()) != 0)
    {   // Global variable successfully set.
        return (true);
    }
    else
    {
        Print("Error in GlobalVariableSet: ", GetLastError());
        return (false);
    }
}


I wont to stop the Expert if the CheckEnvironment() returned false.
gordon
2326
gordon  
Miroslav_Popov:

That was the first thing I tried but I'm not sure the expert is stopped. It's on the chart keep smiling. Probably MT continues calling start() at every tick.

What I wonna is to remove it completely from the chart.

Yeah, it's calling the expert on every tick. U can force remove it from chart via a Win API call:

#include <WinUser32.mqh>       

// this will remove expert from chart
PostMessageA( WindowHandle( Symbol(), Period()), WM_COMMAND, 33050, 0);
Miroslav Popov
365
Miroslav Popov  

Excellent!


Thank you.

qjol
3243
qjol  
gordon:

Yeah, it's calling the expert on every tick. U can force remove it from chart via a Win API call:


nice, is there a way to get it back with an API ?
whroeder1
14779
whroeder1  
Of course not, it has to be running to call an API
airified
78
airified  
gordon:

Yeah, it's calling the expert on every tick. U can force remove it from chart via a Win API call:

Hey gordon...sorry for bumping up an old thread...can i check if the Win API call will work with the MT4 backtester as well? meaning once called, it will skip to the next set of parameters? ty! 
whroeder1
14779
whroeder1  
RTFM Event Handling Functions - MQL4 Documentation What part of INIT_PARAMETERS_INCORRECT was unclear?
To add comments, please log in or register