How do I close an Expert programmatically?

Back to topics list To post a new topic, please log in or register
avatar
115
Miroslav_Popov 2009.10.01 05:13
 

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!

 
avatar
8
spockman 2009.10.01 09:54 #
 

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); }
 
avatar
115
Miroslav_Popov 2009.10.01 13:14 #
 

** 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.

 
avatar
115
Miroslav_Popov 2009.10.01 13:33 #
 

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.
 
avatar
2025
gordon 2010.02.02 06:15 #
 
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);
 
avatar
115
Miroslav_Popov 2010.02.02 11:23 #
 

Excellent!


Thank you.

avatar
3134
qjol 2010.11.15 14:25 #
 
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 ?
avatar
13009
WHRoeder 2012.10.26 22:45 #
 
Of course not, it has to be running to call an API
avatar
3
airified 2015.07.26 09:42 #
 
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! 
avatar
13009
WHRoeder 2015.07.26 13:43 #
 
RTFM Event Handling Functions - MQL4 Documentation What part of INIT_PARAMETERS_INCORRECT was unclear?



Back to topics list  

To add comments, please log in or register