委託成交回報
委託與成交回報是在進行下單、更改單及單的狀態改變時的報告。若不想收到任何回報可在登入的時候將subscribe_trade
設定為 False,預設為皆會收到。
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
證券¶
下單
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)
委託回報
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": 新單, "Cancel": 刪單, "UpdatePrice": 改價, "UpdateQty": 改量}
op_code (str): {"00": 成功, others: 失敗}
op_msg (str): 錯誤訊息
id (str): 與成交回報的trade_id相同
seqno (str): 平台單號
ordno (str): 委託單號
action (str): 買賣別
price (float or int): 委託價
quantity (int): 委託量
order_cond (str): {Cash: 現股, MarginTrading: 融資, ShortSelling: 融券}
order_lot (str): {Common: 整股, Fixing: 定盤, Odd: 盤後零股, IntradayOdd: 盤中零股}
custom_field (str): 自訂欄位
order_type (str): 委託類別 {ROD, IOC, FOK}
price_type (str): {LMT: 限價, MKT: 市價, MKP: 範圍市價}
id (str): 與成交回報的trade_id相同
exchange_ts (int): 交易所時間
modified_price (float or int): 改價
cancel_quantity (int): 取消數量
web_id (str): 下單平台代碼
security_type (str): 商品類別
exchange (str): 交易所
code (str): 商品代碼
symbol (str): 符號
name (str): 商品名稱
currency (str): 幣別
成交回報
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): 與委託回報id相同
exchange_seq (str): 回報序號
broker_id (str): 分行代碼
account_id (str): 帳號
action (str): 買賣別
code (str): 商品代碼
order_cond (str): {Cash: 現股, MarginTrading: 融資, ShortSelling: 融券}
order_lot (str): {Common: 整股, Fixing: 定盤, Odd: 盤後零股, IntradayOdd: 盤中零股}
price (float or int): 成交價
quantity (int): 成交量
web_id (str): 平台代碼
custom_field (str): 自訂欄位
ts (int): 成交timestamp
期權¶
下單
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)
委託回報
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": 新單, "Cancel": 刪單, "UpdatePrice": 改價, "UpdateQty": 改量}
op_code (str): {"00": 成功, others: 失敗}
op_msg (str): 錯誤訊息
id (str): 與成交回報的trade_id相同
seqno (str): 平台單號
ordno (str): 委託單號
action (str): 買賣別
price (float or int): 委託價
quantity (int): 委託量
order_cond (str): {Cash: 現股, MarginTrading: 融資, ShortSelling: 融券}
order_type (str): 委託類別 {ROD, IOC, FOK}
price_type (str): {LMT: 限價, MKT: 市價, MKP: 範圍市價}
market_type (str): 市場別 {I:日盤, P:夜盤}
oc_type(str): {"0": 新倉, "1": 平倉, "7": 代沖銷, "": 自動}
subaccount(str): 交易所
id (str): 與成交回報的trade_id相同
exchange_ts (int): 交易所時間
modified_price (float or int): 改價
cancel_quantity (int): 取消數量
web_id (str): 下單平台代碼
security_type (str): 商品類別
code (str): 商品代碼
exchange (str): 交易所
delivery_month (str):
strike_price (float): 商品名稱
option_right (str): Call/Put
成交回報
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): 與委託回報id相同
seqno (str): 平台單號
ordno (str): 前五碼為同委託回報委託單號,後三碼為同筆委託成交交易序號。
exchange_seq (str): 回報序號
broker_id (str): 分行代碼
account_id (str): 帳號
action (str): 買賣別
code (str): 商品代碼
price (float or int): 成交價
quantity (int): 成交量
subaccount (str): 交易所
security_type (str): 商品類別
delivery_month (str): 商品月份
strike_price (float): 履約價
option_right (str): CALL/PUT
market_type (str): 市場別
ts (int): 成交timestamp
使用回報資訊¶
可以利用set_order_callback
使用回報資訊。如同範例為收到回報前印出my_place_callback。
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)
委託回報
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'
}
}
成交回報
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
}