EA kept running after ExpertRemove

 

I wrote an EA for my wife. On reaching a specified price it closes a portion of the lots and moves the stop loss.

i.e.

```

input double new_stop_loss;

...

// modify the SL

ensure(OrderModify(order_ticket_number, OrderOpenPrice(), new_stop_loss, OrderTakeProfit(), OrderExpiration(), Red), "Unable to modify SL");

ensure(OrderClose(order_ticket_number, tp_1_close_lots, Bid, 3, Red), "Unable to close order");

 

void ensure(bool predicate, string msg) {

    if (!predicate) {

        die(msg);

    }

}


void die(string msg) {

    MessageBox("Exiting: " + msg + ": " + IntegerToString(GetLastError()));

    ExpertRemove();

}

```

As you can see, the EA attempts to change the stop. But if it fails it will exit using `ExpertRemove`.

Yesterday she ran the EA with a SL value that equaled the existing SL. As such, when the price was hit overnight, OrderModify died because the values hadn't changed. However, hours later when we hit "OK" on the dialog box, the script continued to run! It exited half the trade at a completely different price than intended!

Why is it that the call to `ExitRemove` in `die` did not immediately exit the EA? 

 
Why is it that the call to `ExitRemove` in `die` did not immediately exit the EA? 

Per the documentation:

The Expert Advisor is not stopped immediately as you call ExpertRemove(); just a flag to stop the EA operation is set. That is, any next event won't be processed, OnDeinit() will be called and the Expert Advisor will be unloaded and removed from the chart.

 
Esfan: ensure(OrderModify(order_ticket_number, OrderOpenPrice(), new_stop_loss, OrderTakeProfit(), OrderExpiration(), Red), "Unable to modify SL");
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your (original) post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. Your code doesn't appear to select a ticket, therefor OrderOpenPrice, OrderTakeProfit, and OrderExpiration are undefined.
  3. You don't show the code that would call those functions. We are not mindreaders.
  4. You don't show the declaration of order_ticket_number. We are not mindreaders.
Reason: