How do I exit the EA?

 

I simply want to stop program execution like Exit or Halt in other languages. How do I do this in mql4? I've tried return(0) as suggested, but it doesn't stop the EA, at least not in the Strategy Tester.

 

MQL4 documentation says...

"For an expert to stop working,it must be deleted from the chart using "Expert Advisors - Delete" in the chart context menu. The status of the "Enable Expert Advisors" field influences the running of the expert."

 
Lou:

I simply want to stop program execution like Exit or Halt in other languages. How do I do this in mql4? I've tried return(0) as suggested, but it doesn't stop the EA, at least not in the Strategy Tester.

For Demo/Live you can have an EA detach itself from the chart (https://www.mql5.com/en/forum/120736). This will not work for the Tester and is not necessarily recommended for either case. The more universal method is to have the EA return immediately at every call to start(). The basic idea:

int start()
   {
   static bool exit = false;
   if (exit) return(0);
   
   // somewhere in the code we set exit to true...
   }
When exit is set to true, the expert is effectively disabled. Note that if the expert has no persistence layer and the Terminal restarts, the expert will be active (since exit will initialize to false).
 
Lou:

I simply want to stop program execution like Exit or Halt in other languages. How do I do this in mql4? I've tried return(0) as suggested, but it doesn't stop the EA, at least not in the Strategy Tester.


Lou,

you want no open new trades and close opened trades and remove pending orders? Or only no open new trades? In second case, you can put as criterion to open new orders. Example: you want stop the program if balance is lower than $ 9200 or if spread is higher than 35 pipets:

if (

criterion 1
&& criterion 2

...

&& criterion n
&& Ask - Bid < 35 * Point
&& AccountBalance () > 9200

) Order = SIGNAL_SELL; // Order = SIGNAL_BUY;


In the case of spread, after the value changes, the program return to work. I dont know if my suggestion is appropriate for your problem...

Reason: