Close or Delete Pending Order When Buy Order or Sell Order Executed - MT4

 

Dear all,

I am creating my system to open two pending orders - BuyStop and SellStop

Next, I want it to Close or Delete one of the Stop order when Buy or Sell is executed.

Example:

Case 1: BuyStop and SellStop are Placed

BuyStop is executed - Buy. Then, SellStop should be closed.

Case 2: BuyStop and SellStop are placed

SellStop is executed - Sell. Then, BuyStop should be closed. 

Anyone knows how to code this in MT4?

 

Thanks.  

 
MR.: I am creating my system to open two pending orders - BuyStop and SellStop
  1. There is no need to create pending orders in code.

    1. The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)

      Don't worry about it unless you're scalping M1 or trading news.

    2. Humans can't watch the screen 24/7, so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and just open an order.


  2. Where is your attempt and what is your problem?

    Not tested, not compiled, just typed.

    bool isOpen=false; int pendingTicket=EMPTY;
    for(int iPos=OrdersTotal(); --iPos >= 0;) if(
       OrderSelect(iPos, SelectByPos)
    && OrderMagicNumber() == extMN
    && OrderSymbol()      == _Symbol
    ){
       if(OrderType <= OP_Sell) isOpen        = true;
       else                     pendingTicket = OrderTicket();
    }
    if(isOpen && pendingTicket != EMPTY) OrderDelete(pendingTicket);
    
    Not tested, not compiled, just typed.
 
Your topic has been moved to the section: MQL4 e MetaTrader 4 — In the future, please consider which section is most appropriate for your query.
MQL5 forum: Expert Advisors and Automated Trading
MQL5 forum: Expert Advisors and Automated Trading
  • www.mql5.com
How to create an Expert Advisor (a trading robot) for Forex trading