# Modify or Cancel Orders

# modify_order

modify_order(modify_order_op, order_id, qty, price, adjust_limit=0, trd_env=TrdEnv.REAL, acc_id=0, acc_index=0, aux_price=None, trail_type=None, trail_value=None, trail_spread=None)

  • Description

    Modify the price and quantity of orders, cancel orders, delete orders, enable or disable orders, etc.
    For OpenHKCCTradeContextIt it is invalid to change orders, except that cancelling orders is supported.

  • Parameters

    Parameter Type Description
    modify_order_op ModifyOrderOp Modify order operation type.
    order_id str Order ID.
    qty float The quantity after the order is changed.
    price float The price after the order is changed.
    adjust_limit float Price adjustment range.
    trd_env TrdEnv Trading environment.
    acc_id int Trading account ID.
    acc_index int The account number in the trading account list.
    aux_price float Trigger price.
    trail_type TrailType Trailing type.
    trail_value float Trailing amount/ratio.
    trail_spread float Specify spread.
  • Return

    Field Type Description
    ret RET_CODE Interface result.
    data pd.DataFrame If ret == RET_OK, modification information is returned.
    str If ret != RET_OK, error description is returned.
    • Modification information format as follows:
      Field Type Description
      trd_env TrdEnv Trading environment.
      order_id str Order ID.
  • Example

from futu import *
pwd_unlock = '123456'
trd_ctx = OpenSecTradeContext(filter_trdmarket=TrdMarket.HK, host='127.0.0.1', port=11111, security_firm=SecurityFirm.FUTUSECURITIES)
ret, data = trd_ctx.unlock_trade(pwd_unlock)  # If you use a live trading account to modify or cancel an order, you need to unlock the account first. The example here is to cancel an order on a paper trading account, and unlocking is not necessary.
if ret == RET_OK:
    order_id = "8851102695472794941"
    ret, data = trd_ctx.modify_order(ModifyOrderOp.CANCEL, order_id, 0, 0)
    if ret == RET_OK:
        print(data)
        print(data['order_id'][0])  # Get the order ID of the modified order
        print(data['order_id'].values.tolist())  # Convert to list
    else:
        print('modify_order error: ', data)
else:
    print('unlock_trade failed: ', data)
trd_ctx.close()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  • Output
    trd_env             order_id
0    REAL      8851102695472794941
8851102695472794941
['8851102695472794941']
1
2
3
4

# cancal_all_order

cancel_all_order(trd_env=TrdEnv.REAL, acc_id=0, acc_index=0, trdmarket=TrdMarket.NONE)

  • Description

    Cancel all orders. Paper trading and HKCC trading accounts do not support all cancellations.

  • Parameters

    Parameter Type Description
    trd_env TrdEnv Trading environment.
    acc_id int Trading account ID.
    acc_index int The account number in the trading account list.
    trdmarket TrdMarket Transaction market selection.
  • Return

    Field Type Description
    ret int Returned value. On success, ret == RET_OK. On error, ret != RET_OK.
    data str If ret == RET_OK, modification information is returned.
    If ret != RET_OK, error description is returned.
    • Modification information format as follows:
      Field Type Description
      trd_env TrdEnv Trading environment
      order_id str Order number
  • Example

from futu import *
pwd_unlock = '123456'
trd_ctx = OpenHKTradeContext(host='127.0.0.1', port=11111, security_firm=SecurityFirm.FUTUSECURITIES)
ret, data = trd_ctx.unlock_trade(pwd_unlock)  # If you use a live trading account to modify or cancel an order, you need to unlock the account first. The example here is to cancel all orders on a paper trading account, and unlocking is not necessary.
if ret == RET_OK:
    ret, data = trd_ctx.cancel_all_order()
    if ret == RET_OK:
        print(data)
    else:
        print('cancel_all_order error: ', data)
else:
    print('unlock_trade failed: ', data)
trd_ctx.close()
1
2
3
4
5
6
7
8
9
10
11
12
13
  • Output
success
1

Interface Limitations

  • A maximum of 20 requests per 30 seconds, and the time interval between two consecutive requests should not be less than 0.04 seconds.
  • When using live trading accounts, you need to unlock trade before calling Modify or Cancel Orders interface, but when using paper trading accounts, you do not need to unlock trade.

Tip

  • For the execution of modify the order, to learn more about the required parameters for each order type, please click here.
  • If you want to modify the quantity of the order, the parameter qty should be equal to the total quantity of the expected filled.
    For example:
    The quantity of an order is N shares, with n shares filled. For the unfilled (N-n) shares, if you want to cancel x shares, the parameter modify_order_op should be NORMAL, qty should be (N-x). order_quantity
  • If you want to cancel an order, the parameter modify_order_op should be CANCEL.
    For example:
    The quantity of an order is N shares, with n shares filled. If you want to cancel the unfilled (N-n) shares, modify_order_op should be CANCEL, and qty and price will be ignored.