Skip to content

Order & Deal Event

Order & Deal Event is a report of order action. When you place order, cancel order and update order, it will return an OrderState. OrderState is order info. If you don't want to receive any report, you can set subscribe_trade to False when you login.

In

api.login?

Out

Signature:
    api.login(
        person_id: str,
        passwd: str,
        hashed: bool = False,
        fetch_contract: bool = True,
        contracts_timeout: int = 0,
        contracts_cb: Callable[[], NoneType] = None,
        subscribe_trade: bool = True,
    ) -> None
Docstring:
    login to trading server

Stocks

Place Order

contract = api.Contracts.Stocks.TSE.TSE2890
order = api.Order(price=12,
                  quantity=10,
                  action=sj.constant.Action.Buy,
                  price_type=sj.constant.StockPriceType.LMT,
                  order_type=sj.constant.TFTOrderType.ROD,
                  custom_field="test",
                  account=api.stock_account
                  )
trade = api.place_order(contract, order)

Order Event

OrderState.TFTOrder {
    'operation': {
        'op_type': 'New',
        'op_code': '00',
        'op_msg': ''
    },
    'order': {
        'id': 'c21b876d',
        'seqno': '429832',
        'ordno': 'W2892',
        'action': 'Buy',
        'price': 12.0,
        'quantity': 10,
        'order_cond': 'Cash',
        'order_lot': 'Common',
        'custom_field': 'test',
        'order_type': 'ROD',
        'price_type': 'LMT'
    },
    'status': {
        'id': 'c21b876d',
        'exchange_ts': 1583828972,
        'modified_price': 0,
        'cancel_quantity': 0,
        'web_id': '137'
    },
    'contract': {
        'security_type': 'STK',
        'exchange': 'TSE',
        'code': '2890',
        'symbol': '',
        'name': '',
        'currency': 'TWD'
    }
}

TFTOrder

op_type (str): {
            "New": new order, 
            "Cancel": cancel order, 
            "UpdatePrice": update price, 
            "UpdateQty": update quantity
        }
op_code (str): {"00": success, others: fail}
op_msg (str): error message
id (str): same as the trade_id in TFTDeal
seqno (str): sequence number
ordno (str): order number
action (str): buy/sell
price (float or int): order price
quantity (int): order quantity
order_cond (str): {Cash: 現股, MarginTrading: 融資, ShortSelling: 融券}
order_lot (str): {Common: 整股, Fixing: 定盤, Odd: 盤後零股, IntradayOdd: 盤中零股}
custom_field (str): memo field
order_type (str): the type of order {ROD, IOC, FOK}
price_type (str): pricing type of order {LMT: 限價, MKT: 市價, MKP: 範圍市價}
id (str): same as the trade_id in TFTDeal
exchange_ts (int): exchange time
modified_price (float or int): modified price
cancel_quantity (int): cancel quantity
web_id (str): web id
security_type (str): category
exchange (str): exchange
code (str): code id
symbol (str): symbol
name (str): name
currency (str): currency

Deal Event

OrderState.TFTDeal {
    'trade_id': '12ab3456', 
    'exchange_seq': '123456', 
    'broker_id': 'your_broker_id', 
    'account_id': 'your_account_id', 
    'action': <Action.Buy: 'Buy'>, 
    'code': '2890', 
    'order_cond': <StockOrderCond.Cash: 'Cash'>, 
    'order_lot': <TFTStockOrderLot.Common: 'Common'>,
    'price': 12, 
    'quantity': 10,
    'web_id': '137',
    'custom_field': 'test',
    'ts': 1583828972
}

TFTDeal

trade_id (str): same as the id in TFTOrder
exchange_seq (str): exchange sequence number
broker_id (str): broker id
account_id (str): account
action (str): buy/sell
code (str): code id
order_cond (str): {Cash: 現股, MarginTrading: 融資, ShortSelling: 融券}
order_lot (str): {Common: 整股, Fixing: 定盤, Odd: 盤後零股, IntradayOdd: 盤中零股}
price (float or int): deal price
quantity (int): deal quantity
web_id (str): web id
custom_field (str): memo field
ts (int): deal timestamp    

Futures and Option

Place Order

contract = api.Contracts.Futures.TXF['TXF202107']
order = api.Order(
    action='Sell',
    price=17650,
    quantity=4,
    price_type='LMT',
    order_type='ROD', 
    octype=sj.constant.FuturesOCType.Auto,
    account=api.futopt_account
)
trade = api.place_order(contract, order)

Order Event

OrderState.FOrder {
    "operation":{
        "op_type":"New",
        "op_code":"00",
        "op_msg":""
    },
    "order":{
        "id":"aa78ef1f",
        "seqno":"956344",
        "ordno":"ky00N",
        "action":"Sell",
        "price":17650.0,
        "quantity":4,
        "order_cond":None,
        "order_type":"ROD",
        "price_type":"LMT",
        "market_type":"Day",
        "oc_type":"New",
        "subaccount":""
    },
    "status":{
        "id":"aa78ef1f",
        "exchange_ts":1625800170,
        "modified_price":0.0,
        "cancel_quantity":0,
        "web_id": "P"
    },
    "contract":{
        "security_type":"FUT",
        "code":"TXF",
        "exchange":"TIM",
        "delivery_month":"202107",
        "strike_price":0.0,
        "option_right":"Future"
    }
}

FOrder

op_type (str): {
            "New": new order, 
            "Cancel": cancel order, 
            "UpdatePrice": update price, 
            "UpdateQty": update quantity
        }
op_code (str): {"00": success, others: fail}
op_msg (str): error message
id (str): same as the trade_id in FDeal
seqno (str): sequence number
ordno (str): order number
action (str): buy/sell
price (float or int): order price
quantity (int): order quantity
order_cond (str): {Cash: 現股, MarginTrading: 融資, ShortSelling: 融券}
order_type (str): the type of order {ROD, IOC, FOK}
price_type (str): pricing type of order {LMT: 限價, MKT: 市價, MKP: 範圍市價}
market_type (str): {I:日盤, P:夜盤}
oc_type(str): {"0": 新倉, "1": 平倉, "7": 代沖銷, "": 自動}
subaccount(str): exchange
id (str): same as the trade_id in FDeal
exchange_ts (int): exchange time
modified_price (float or int): modified price
cancel_quantity (int): cancel quantity
web_id (str): web id
security_type (str): category
code (str): code id
exchange (str): exchange
delivery_month (str): delivery month
strike_price (float): name
option_right (str): Call/Put

Deal Event

OrderState.FDeal {
    "trade_id":"aa78ef1f",
    "seqno":"956344",
    "ordno":"ky00N11O",
    "exchange_seq":"a0000060",
    "broker_id":"F002000",
    "account_id":"9104000",
    "action":"Sell",
    "code":"TXF",
    "price":17650.0,
    "quantity":4,
    "subaccount":"",
    "security_type":"FUT",
    "delivery_month":"202107",
    "strike_price":0.0,
    "option_right":"Future",
    "market_type":"Day",
    "ts":1625800369
}

FDeal

trade_id (str): same as the id in FOrder
seqno (str): sequence number
ordno (str): The first 5 characters is the same as ordno in FOrder. The last 3 characters represent the deal sequence number.
exchange_seq (str): exchange sequence number
broker_id (str): broker id
account_id (str): account
action (str): buy/sell
code (str): code
price (float or int): deal price
quantity (int): deal quantity
subaccount (str): exchange
security_type (str): category
delivery_month (str): delivery month
strike_price (float): strike price
option_right (str): CALL/PUT
market_type (str): market type
ts (int): deal timestamp    

Set order callback

You can use set_order_callback to use the return information. The example prints my_place_callback before receiving the event.

In

def place_cb(stat, msg):
    print('my_place_callback')
    print(stat, msg)

api.set_order_callback(place_cb)
contract = api.Contracts.Stocks.TSE.TSE2890
order = api.Order(price=12,
                  quantity=10,
                  action=sj.constant.Action.Buy,
                  price_type=sj.constant.StockPriceType.LMT,
                  order_type=sj.constant.TFTOrderType.ROD,
                  custom_field="test",
                  account=api.stock_account
                  )
trade = api.place_order(contract, order)

Order Event

my_place_callback
OrderState.TFTOrder {
    'operation': {
        'op_type': 'New',
        'op_code': '00',
        'op_msg': ''
    },
    'order': {
        'id': 'c21b876d',
        'seqno': '429832',
        'ordno': 'W2892',
        'action': 'Buy',
        'price': 12.0,
        'quantity': 10,
        'order_cond': 'Cash',
        'order_lot': 'Common',
        'custom_field': 'test',
        'order_type': 'ROD',
        'price_type': 'LMT'
    },
    'status': {
        'id': 'c21b876d',
        'exchange_ts': 1583828972,
        'modified_price': 0,
        'cancel_quantity': 0,
        'web_id': '137'
    },
    'contract': {
        'security_type': 'STK',
        'exchange': 'TSE',
        'code': '2890',
        'symbol': '',
        'name': '',
        'currency': 'TWD'
    }
}

Deal Event

my_place_callback
OrderState.TFTDeal {
    'trade_id': '12ab3456', 
    'exchange_seq': '123456', 
    'broker_id': 'your_broker_id', 
    'account_id': 'your_account_id', 
    'action': <Action.Buy: 'Buy'>, 
    'code': '2890', 
    'order_cond': <StockOrderCond.Cash: 'Cash'>, 
    'order_lot': <TFTStockOrderLot.Common: 'Common'>,
    'price': 12, 
    'quantity': 10, 
    'web_id': '137',
    'custom_field': 'test',
    'ts': 1583828972
}