Opposite pending order does not remove position

 

Hello,

I am trying to close a PENDING BUY STOP order by placing a PENDING SELL STOP order. I am using Python. Both orders get executed (deals done appear in the Journal tab) and the position gets removed from the Exposure tab but long and short trades still appear in the Trade tab and mt5.positions_get() also show them. The same thing does not happen if I use DEAL instead of PENDING orders.

How to ensure that positions get cleaned after executing the opposite pending order??

This is a snippet of the code:

request1 = { "action": mt5.TRADE_ACTION_PENDING, "symbol": symbol, "volume": lot, "type": mt5.ORDER_TYPE_BUY_STOP, "price": buy_price, "magic": 234000, "comment": "python script open", "type_time": mt5.ORDER_TIME_GTC, "type_filling": mt5.ORDER_FILLING_RETURN,

}

result1 = mt5.order_send(request1)

request1_close = { "action": mt5.TRADE_ACTION_PENDING, "symbol": symbol, "volume": lot, "type": mt5.ORDER_TYPE_SELL_STOP, "price": sell_price, "magic": 234000, "position": result1.order, "comment": "python script open", "type_time": mt5.ORDER_TIME_GTC, "type_filling": mt5.ORDER_FILLING_RETURN, }

result1_close = mt5.order_send(request1_close)

Thanks

 
Eduardo Perez:

Hi Eduardo,

to share code use the 'Code' button. I have corrected this for this time.

 

 
Eduardo Perez: I am trying to close a PENDING BUY STOP order by placing a PENDING SELL STOP order. I am using Python. Both orders get executed (deals done appear in the Journal tab) and the position gets removed from the Exposure tab but long and short trades still appear in the Trade tab and mt5.positions_get() also show them. The same thing does not happen if I use DEAL instead of PENDING orders.

How to ensure that positions get cleaned after executing the opposite pending order??

  1. You don't close pending orders, you delete them.
  2. Placing a new order does not automatically delete another. You have to code that separately.
 
Miguel Angel Vico Alba #:

Hi Eduardo,

to share code use the 'Code' button. I have corrected this for this time. 

Thanks Miguel Angel, new to the forum
 
Eduardo Perez #:

Don't worry.

I advise you to read the rules to avoid misunderstandings in the future. It will only take you 1 minute.

https://www.mql5.com/en/forum/443428

Read Fernando's answer carefully. He has hit the nail on the head (once again). <3

 
Eduardo Perez: I am trying to close a PENDING BUY STOP order
  1. You can't close pending orders, they are not open. You delete them.

  2. 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.

 
Fernando Carreiro #:
  1. You don't close pending orders, you delete them.
  2. Placing a new order does not automatically delete another. You have to code that separately.

Hello Fernando,

Thank you for your help. My problem is not related to order removal but to position removal after order (buy+close) completion. If I complete a long order and then a short order of the same instrument and same lot size the resulting position should be 0 and this is not happening with PENDING orders.

Sharing some screenshots:

Files:
 
William Roeder #:
  1. You can't close pending orders, you delete them.

  2. 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.

Thanks William, I will use DEAL orders if there is no other option but this is very strange to me. Using pending STOP/STOP LIMIT is standard good practice with others plataforms/brokers for ensuring control over filling price. 
Reason: