Can an EA kill itself as part of the programming?

 

I want to program functionality into an EA so that it can effectively abort, for example if there are no open trades.


I tried return(1) rather than return(0) but that has no effect.

Is there a "die and don't get reborn on the next tick" type of feature possible, or would I have to fall into an infinite while loop instead.


In a typical example the code would check for open trades. If none present it would pop up a MessageBox saying "no trades open" and ideally quit at that point. It would be boring for such a messsage box to appear on the next tick.

 

start() is called every tick, so your EA runs on every tick while attached to the chart.

return(x) called from start -- the value of x is ignored, but the return does exit your code

If you want to see the Message Box one time only then write code to display it one time only.

 
Another option is to unload the EA with WinAPI call.
 
start() {
  static aborted=false;
  if (aborted == true) return;
  ...
  if (...) {
    aborted=true;
    alert(...); // display message once here
  }
  ...
}
 

WHRoeder wrote >>

  static aborted=false;
  if (aborted == true) return;

Does this compile?

 
WHRoeder:

Thanks, but you missed the point (and the argument on the return :-)

Yes I can do a return to exit the code once, but then the start will re-invoke it which is what I wanted to stop.


Strickly speaking phy the start is not called every tick, IMHO. The calls to start are thrown away until you do a return to exit from the code (otherwise the code would be re-entrant.


Irton, I like the idea of a WinAPI call to unload the EA. That is exactly what I want to do. Do you have such an API call in mind?

 
dabbler:

Thanks, but you missed the point (and the argument on the return :-)

Yes I can do a return to exit the code once, but then the start will re-invoke it which is what I wanted to stop.

On the next tick, start() will be called and the static will be set, so it will immediately return. That means

that it can effectively abort

 
Irtron:

Does this compile?

Give me a break. Yes it does compile (default to int,) but if you can't figure out what's missing, you don't belong coding.

 
dabbler:

Irton, I like the idea of a WinAPI call to unload the EA. That is exactly what I want to do. Do you have such an API call in mind?

I don't know of a way of removing the EA from the chart programmatically (though it may be possible to do so by simulating menu clicks). What definitely does work is closing the chart by posting WM_CLOSE to the parent of WindowHandle(Symbol(), Period()). But you'd have to be braver than me to do this on something trading real money.


Strickly speaking phy the start is not called every tick, IMHO. The calls to start are thrown away until you do a return to exit from the code (otherwise the code would be re-entrant.

True. Pedantic, but true. <g>


 
WHRoeder:

Give me a break. Yes it does compile (default to int,) but if you can't figure out what's missing, you don't belong coding.

Oh, please, do have a break.

Missing type specifier is not allowed in MQL4 unlike in other languages. So it definitely doesn't and fails with 'unexpected token' error as expected.


Will, why do you take this so personal? A simple question implies a simple answer. :)

 
dabbler:

Irton, I like the idea of a WinAPI call to unload the EA. That is exactly what I want to do. Do you have such an API call in mind?


void commitSuicide()

{ int h = WindowHandle(Symbol(), Period()); if (h != 0) PostMessageA(h, WM_COMMAND, 33050, 0); }


PS.

I wonder why SRC button stopped working? At least in googlechrome.