python with mt5 Delete a Pending Order

 

Hello Everyone, Im facing trouble in deleting an active pending order from mt5 through python. I keep getting an error out of this code. Can someone please help ???

import MetaTrader5 as mt5

from MetaTrader5 import *

if not mt5.initialize():

print("initialize() failed, error code =", mt5.last_error())

quit()

symbol = 'BTCUSD'

lot = 0.02

price = mt5.symbol_info_tick(symbol).bid

deviation = 20

 close_request={ "action": mt5.TRADE_ACTION_REMOVE,

"symbol": 'BTCUSD',

 "volume": lot,

"type": mt5.ORDER_TYPE_BUY_STOP,

"position": 114296760,

"price": price,

"deviation": deviation,

"magic": 0,

"comment": "python script op",

"type_time": mt5.ORDER_TIME_GTC, # good till cancelled

"type_filling": 1,

}

# send a close request

result=mt5.order_send(close_request)

print(" result",result)

 
Please edit your post and use the code button (Alt+S) when pasting code.
EDIT your original post, please do not just post the code correctly in a new post.
 
Hisham Ashour:

Hello Everyone, Im facing trouble in deleting an active pending order from mt5 through python. I keep getting an error out of this code. Can someone please help ???

import MetaTrader5 as mt5

from MetaTrader5 import *

if not mt5.initialize():

print("initialize() failed, error code =", mt5.last_error())

quit()

symbol = 'BTCUSD'

lot = 0.02

price = mt5.symbol_info_tick(symbol).bid

deviation = 20

 close_request={ "action": mt5.TRADE_ACTION_REMOVE,

"symbol": 'BTCUSD',

 "volume": lot,

"type": mt5.ORDER_TYPE_BUY_STOP,

"position": 114296760,

"price": price,

"deviation": deviation,

"magic": 0,

"comment": "python script op",

"type_time": mt5.ORDER_TIME_GTC, # good till cancelled

"type_filling": 1,

}

# send a close request

result=mt5.order_send(close_request)

print(" result",result)

you might try below at your python codes

1. change 

"position": 114296760,

to

"order": (with pending order's order #),

2. remove "price" and "deviation"  and "type" ("type_time" might NOT be required too) - those parameters are NOT required for pending order removal at MT5-python.

3. ensure "type_filling" is the right one.  try either mt5.ORDER_FILLING_IOC or mt5.ORDER_FILLING_FOK





 
Manage MT5 orders using Python
Manage MT5 orders using Python
  • Kritthanit Malathong
  • kritthanit-m.medium.com
Post id: 36, 22/04/2022 1. Connect Python to MT5 2. Open Position Order type: BUYLIMIT, BUYSTOP, SELLLIMIT, SELLSTOP ==> Call “Order” Order type: BUY, SELL ==> Call “Position” 3. Modify position 4. Read data from position 5...
 

Hello, 


I tried the code from the link and it didn t close any orders. Can you help?

Deleting pending orders:

def delete_pending(mt5, ticket):
    close_request = {
        "action": mt5.TRADE_ACTION_REMOVE,
        "order": ticket,
        "type_time": mt5.ORDER_TIME_GTC,
        "type_filling": mt5.ORDER_FILLING_IOC,
    }
    result = mt5.order_send(close_request)

    if result.retcode != mt5.TRADE_RETCODE_DONE:
        result_dict = result._asdict()
        print(result_dict)
    else:
        print('Delete complete...')