closing metatrader with EA

 
Hi

I want to make an EA that closes all orders and exits metatrader if certain criteria have been reached.
I know how to close the orders, but I can't seem to find how the EA could close the whole metatrader program.

Anyone knows?

thx in advance
 
 
7bit:
taskkill [...] might be the solution.

That route has two problems: (a) if there's more than one copy of MT4 running then all of them get closed; and (b) termination of the process may leave temporary files lying around, or even cause damage to things like history files.

A cleaner alternative is to send a WM_CLOSE message to MT4's main window. (This relies on the fact that MT4 does not ask for user confirmation before closing.) Could use GetAncestor() instead of repeated calls to GetParent().

#import "user32.dll"
   int PostMessageA(int hWnd, int msg, int wparam, int lparam);
   int GetParent(int hWnd);
#import
   
#define WM_CLOSE 16


int start()
{
   PostMessageA(GetParent(GetParent(GetParent(WindowHandle(Symbol(), Period())))), WM_CLOSE, 0, 0);
}   
 
Archel:
I want to make an EA that closes all orders and exits metatrader if certain criteria have been reached.

...but there are many arguments in favour of leaving MT4 open and just stopping the EA trading, rather than terminating MT4.

 
hmm
jjc:

...but there are many arguments in favour of leaving MT4 open and just stopping the EA trading, rather than terminating MT4.


It's an independant EA that finishes the trades of other EA's, so MT4 doesn't necessary have to be terminated.
But I can't seem to find how to close down other EA's either. ^^

About the solutions above, I don't really understand them. Didn't know that code like getparent could be used, cause it isn't written down in mql documentation.
Do I just have to copy the code then?

eg

if (criteria for closing down...)

#import "user32.dll"
   int PostMessageA(int hWnd, int msg, int wparam, int lparam);
   int GetParent(int hWnd);
#import
   
#define WM_CLOSE 16


int start()
{
   PostMessageA(GetParent(GetParent(GetParent(WindowHandle(Symbol(), Period())))), WM_CLOSE, 0, 0);
}   
 
Archel:
It's an independant EA that finishes the trades of other EA's, so MT4 doesn't necessary have to be terminated.

Ah. That is indeed about the only good reason I can think of for an EA shutting down MT4. There is code on this forum for stopping all EAs from running without terminating MT4 but, personally, in this position I'd tend to be happier doing a complete closure of MT4.

Archel wrote >>
About the solutions above, I don't really understand them. Didn't know that code like getparent could be used, cause it isn't written down in mql documentation.
Do I just have to copy the code then?

The code is using Win32 API functions. See https://docs.mql4.com/basis/preprosessor/import

The import and define bits need to sit outside functions, typically at the very start of your code. Broadly speaking, you'd do the following:

#import "user32.dll"
   int PostMessageA(int hWnd, int msg, int wparam, int lparam);
   int GetParent(int hWnd);
#import
   
#define WM_CLOSE 16

[... anything else before start() ...]

int start()
{
   [... normal trading/monitoring here ...]

   if (criteria for closing down...) {
       PostMessageA(GetParent(GetParent(GetParent(WindowHandle(Symbol(), Period())))), WM_CLOSE, 0, 0);
   }

   [... more normal trading/monitoring here ...]

   return (0);
}   
 
Archel, an alternative solution is to have the expert remove itself from the chart -> https://www.mql5.com/en/forum/122839 (Terminal remains open).
 
Hmmm, I'm so bad at this. ^^

So, what I want, is an independent EA that closes all open trades (made by other EA's) and terminates the MT4 terminal when a criteria is reached.
This criteria is when my equity would be less than 95% than the equity at the beginning. (when I attached the independant EA to the chart).

Now, I have this, with the help from above (for the closing of the terminal) and from another topic (for the closing of trades):


double balans,x;

//---------------
int init()
{balans = AccountEquity();}
//---------------
//---------------
void CloseAll()
{
  for(int i=OrdersTotal()-1;i>=0;i--)
 {
    OrderSelect(i, SELECT_BY_POS);
    bool result = false;
        if ( OrderType() == OP_BUY) 
         result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
        if ( OrderType() == OP_SELL)  
         result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
        if ( OrderType()== OP_BUYSTOP)  
         result = OrderDelete( OrderTicket() );
        if ( OrderType()== OP_SELLSTOP) 
         result = OrderDelete( OrderTicket() );
 }
  return;
}
//---------------

#import "user32.dll"
   int PostMessageA(int hWnd, int msg, int wparam, int lparam);
   int GetParent(int hWnd);
#import
   
#define WM_CLOSE 16

//---------------
int start()
{x= AccountEquity();

if (x< (0.95*balans))
{CloseAll();
PostMessageA(GetParent(GetParent(GetParent(WindowHandle(Symbol(), Period())))), WM_CLOSE, 0, 0);}

return (0);

}

Could you say what is wrong, cause it doesn't yet work.
(I tested it by using the criteria "if (open[0] > open[1])" instead of the "0.95...."

grts
 
Archel:
Hmmm, I'm so bad at this. ^^

So, what I want, is an independent EA that closes all open trades (made by other EA's) and terminates the MT4 terminal when a criteria is reached.

I would recommend you disable all other EA's first. If u don't and u start closing their orders, it might trigger them to open other orders, and u might end up in a loop (a costly loop). So first u must disable all other experts.

Regarding your code - it has no error handling capabilities. It would work fine in the Tester, but you are very likely to run into serious problems in Live trading.

 
hmm

maybe I can prevent the EA from trading by writing the criteria in the EA itself...
It ain't what I wanted, but it's basically the same.

So instead of:

if ( ....)
ordersend();


I write:

if (.... && x>balans* 0,95)
ordersend();
 

Hello folks


Is there a mql4 robot, that checks if a negative amount (daily loss) is reached based upon the history?


Then it closes immediately MT4 client.


Example: IF daily_loss == 32 USD THEN close MT4 Client


Thx a lot in advance!

Reason: