9. Cancel Order

Cancel open unfilled or partially filled orders

Objective

Cancel the unfilled portion of open orders (PENDING, PARTIALLY_FILLED).

Flow

Open Orders → Trading API (Cancel Order) → Order Book State

Cancellable Statuses

CANCELLABLE_STATUSES = {
    OrderStatus.PENDING_APPROVAL, OrderStatus.READY,
    OrderStatus.SENT, OrderStatus.QUEUED, OrderStatus.PARTIAL_FILLED,
}

Sample Code — Sync

python/sample_09_cancel_order.py
from ssi_sdk import Auth, Trading, Config
from ssi_sdk.enums import OrderStatus
from auth_helper import ensure_auth
from helpers import enum_value

config = Config(
    client_id="<your_client_id>",
    api_key="<your_api_key>",
    api_secret="<your_api_secret>",
    private_key="<your_private_key>",
)
ACCOUNT_NO = "<your_account_no>"

with Auth(config) as auth:
    ensure_auth(auth, otp="<your_otp>")
    with Trading(auth) as trading:
        orders = trading.portfolio.get_today_orders(ACCOUNT_NO)
        open_orders = [o for o in orders if o.status in CANCELLABLE_STATUSES]

        if open_orders:
            target = open_orders[0]
            result = trading.trading.cancel_order_by_order_id(
                account_no=ACCOUNT_NO, order_id=target.order_id,
            )
            print(f"  Cancel result: {result}")

            # Confirm the status after cancelling
            for order in trading.portfolio.get_today_orders(ACCOUNT_NO):
                if order.order_id == target.order_id:
                    print(f"  Status: {enum_value(order.status)} | Cancelled: {order.cancel_quantity}")
                    break

Sample Code — Async

python/sample_09_cancel_order_async.py
import asyncio
from ssi_sdk import AsyncAuth, AsyncTrading, Config
from ssi_sdk.enums import OrderStatus
from auth_helper import ensure_auth_async

config = Config(...)
ACCOUNT_NO = "<your_account_no>"

async def main():
    async with AsyncAuth(config) as auth:
        await ensure_auth_async(auth, otp="<your_otp>")
        async with AsyncTrading(auth) as trading:
            orders = await trading.portfolio.get_today_orders(ACCOUNT_NO)
            open_orders = [o for o in orders if o.status in CANCELLABLE_STATUSES]

            # Cancel all open orders in parallel
            cancel_tasks = [
                trading.trading.cancel_order_by_order_id(
                    account_no=ACCOUNT_NO, order_id=o.order_id,
                )
                for o in open_orders
            ]
            results = await asyncio.gather(*cancel_tasks, return_exceptions=True)

asyncio.run(main())

Notes

  • Only orders in PENDING_APPROVAL, READY, SENT, QUEUED, PARTIAL_FILLED status can be cancelled.
  • The async version can cancel multiple orders in parallel using asyncio.gather.
  • After cancellation, available cash is released back.

On this page