期貨
委託與成交回報是在進行下單、更改單及單的狀態改變時的報告。若不想收到任何回報可在登入的時候將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
下單¶
下單會收到一筆委託回報,op_type
會顯示這筆單的行為,如同下方新單會顯示New。當成交後會收到一筆成交回報。
In
contract = api.Contracts.Futures.TXF['TXF202107']
order = api.Order(
action='Sell',
price=17760,
quantity=1,
price_type='LMT',
order_type='ROD',
octype=sj.constant.FuturesOCType.Auto,
account=api.futopt_account
)
trade = api.place_order(contract, order)
Out
OrderState.FOrder {
'operation': {
'op_type': 'New',
'op_code': '00',
'op_msg': ''
},
'order': {
'id': '02c347f7',
'seqno': '956201',
'ordno': 'kY00H',
'action': 'Sell',
'price': 17760.0,
'quantity': 1,
'order_cond': None,
'order_type': 'ROD',
'price_type': 'LMT',
'market_type': 'Night',
'oc_type': 'New',
'subaccount': ''
},
'status': {
'id': '02c347f7',
'exchange_ts': 1625729890,
'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'
}
}
op_type
: operation typeop_code
: "00": success, others: failop_msg
: "" if succsess else it will show error message
Set order callback¶
In: set order callback
def place_cb(stat, msg):
print('__my_place_callback__')
print(stat, msg)
api.set_order_callback(place_cb)
In: place order
contract = api.Contracts.Futures.TXF['TXF202107']
order = api.Order(
action='Sell',
price=17760,
quantity=1,
price_type='LMT',
order_type='ROD',
octype=sj.constant.FuturesOCType.Auto,
account=api.futopt_account
)
trade = api.place_order(contract, order)
Out
__my_place_callback__
OrderState.FOrder {
'operation': {
'op_type': 'New',
'op_code': '00',
'op_msg': ''
},
'order': {
'id': '02c347f7',
'seqno': '956201',
'ordno': 'kY00H',
'action': 'Sell',
'price': 17760.0,
'quantity': 1,
'order_cond': None,
'order_type': 'ROD',
'price_type': 'LMT',
'market_type': 'Night',
'oc_type': 'New',
'subaccount': ''
},
'status': {
'id': '02c347f7',
'exchange_ts': 1625729890,
'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'
}
}
Cancel Order¶
In
api.update_status(api.futopt_account)
api.cancel_order(trade)
Out
OrderState.FOrder {
"operation":{
"op_type":"Cancel",
"op_code":"00",
"op_msg":""
},
"order":{
"id":"02c347f7",
"seqno":"956201",
"ordno":"kY00H",
"action":"Sell",
"price":17760.0,
"quantity":1,
"order_cond":None,
"order_type":"ROD",
"price_type":"CXL",
"market_type":"Night",
"oc_type":"New",
"subaccount":""
},
"status":{
"id":"02c347f7",
"exchange_ts":1625730789,
"modified_price":0.0,
"cancel_quantity":1,
"web_id": "P"
},
"contract":{
"security_type":"FUT",
"code":"TXF",
"exchange":"TIM",
"delivery_month":"202107",
"strike_price":0.0,
"option_right":"Future"
}
}
Update Price¶
In: place order
contract = api.Contracts.Futures.TXF['TXF202107']
order = api.Order(
action='Sell',
price=17860,
quantity=4,
price_type='LMT',
order_type='ROD',
octype=sj.constant.FuturesOCType.Auto,
account=api.futopt_account
)
trade = api.place_order(contract, order)
Out: place order
OrderState.FOrder {
"operation":{
"op_type":"New",
"op_code":"00",
"op_msg":""
},
"order":{
"id":"e031f0d4",
"seqno":"956295",
"ordno":"ky00K",
"action":"Sell",
"price":17860.0,
"quantity":4,
"order_cond":None,
"order_type":"ROD",
"price_type":"LMT",
"market_type":"Day",
"oc_type":"New",
"subaccount":""
},
"status":{
"id":"e031f0d4",
"exchange_ts":1625798848,
"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"
}
}
In: update price
api.update_status(api.futopt_account)
api.update_order(trade=trade, price=18990)
Out: update price
OrderState.FOrder {
"operation":{
"op_type":"UpdatePrice",
"op_code":"00",
"op_msg":""
},
"order":{
"id":"e031f0d4",
"seqno":"956295",
"ordno":"ky00K",
"action":"Sell",
"price":18990.0,
"quantity":4,
"order_cond":None,
"order_type":"ROD",
"price_type":"UPL",
"market_type":"Day",
"oc_type":"New",
"subaccount":""
},
"status":{
"id":"e031f0d4",
"exchange_ts":1625798970,
"modified_price":18990.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"
}
}
Update Quantity¶
In: place order
contract = api.Contracts.Futures.TXF['TXF202107']
order = api.Order(
action='Sell',
price=17860,
quantity=4,
price_type='LMT',
order_type='ROD',
octype=sj.constant.FuturesOCType.Auto,
account=api.futopt_account
)
trade = api.place_order(contract, order)
Out: place order
OrderState.FOrder {
"operation":{
"op_type":"New",
"op_code":"00",
"op_msg":""
},
"order":{
"id":"3960ad5d",
"seqno":"956297",
"ordno":"ky00L",
"action":"Sell",
"price":17860.0,
"quantity":4,
"order_cond":None,
"order_type":"ROD",
"price_type":"LMT",
"market_type":"Day",
"oc_type":"New",
"subaccount":""
},
"status":{
"id":"3960ad5d",
"exchange_ts":1625799066,
"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"
}
}
In: update quantity
api.update_status(api.futopt_account)
api.update_order(trade=trade, qty=3)
Out: update quantity
OrderState.FOrder {
"operation":{
"op_type":"UpdateQty",
"op_code":"00",
"op_msg":""
},
"order":{
"id":"3960ad5d",
"seqno":"956297",
"ordno":"ky00L",
"action":"Sell",
"price":17860.0,
"quantity":3,
"order_cond":None,
"order_type":"ROD",
"price_type":"UPD",
"market_type":"Day",
"oc_type":"New",
"subaccount":""
},
"status":{
"id":"3960ad5d",
"exchange_ts":1625799182,
"modified_price":0.0,
"cancel_quantity":3,
"web_id": "P"
},
"contract":{
"security_type":"FUT",
"code":"TXF",
"exchange":"TIM",
"delivery_month":"202107",
"strike_price":0.0,
"option_right":"Future"
}
}
Deal Event¶
Deal event occurs when the order is full/partially filled. Note that you "may" recieve the deal event sooner than the order event due to message priority in exchange.
Filled¶
In: 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)
Out: place 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"
}
}
Out: Deal
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
}
id
in FOrder is the same astrade_id
in FDeal.seqno
in FOrder is the same asseqno
in FDeal.- The first 5 characters of
ordno
in FDeal is the same asordno
in FOrder. The last 3 characters ofordno
in FDeal represent the deal sequence number.
Filling¶
In: 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)
Out: place order
OrderState.FOrder {
"operation":{
"op_type":"New",
"op_code":"00",
"op_msg":""
},
"order":{
"id":"f2924e46",
"seqno":"956357",
"ordno":"ky00b",
"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":"f2924e46",
"exchange_ts":1625800900,
"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"
}
}
Out: Deal: partially filled: 1
OrderState.FDeal {
"trade_id":"f2924e46",
"seqno":"956357",
"ordno":"ky00b219",
"exchange_seq":"a0000081",
"broker_id":"F002000",
"account_id":"9104000",
"action":"Sell",
"code":"TXF",
"price":17650.0,
"quantity":2,
"subaccount":"",
"security_type":"FUT",
"delivery_month":"202107",
"strike_price":0.0,
"option_right":"Future",
"market_type":"Day",
"ts":1625800944
}
Out: Deal: partially filled: 2
OrderState.FDeal {
"trade_id":"f2924e46",
"seqno":"956357",
"ordno":"ky00b21A",
"exchange_seq":"a0000082",
"broker_id":"F002000",
"account_id":"9104000",
"action":"Sell",
"code":"TXF",
"price":17650.0,
"quantity":2,
"subaccount":"",
"security_type":"FUT",
"delivery_month":"202107",
"strike_price":0.0,
"option_right":"Future",
"market_type":"Day",
"ts":1625801009
}
id
in FOrder is the same astrade_id
in FDeal(partially filled: 1 and 2).seqno
in FOrder is the same asseqno
in FDeal(partially filled: 1 and 2).- The first 5 characters of
ordno
in FDeal(partially filled: 1 and 2) is the same asordno
in FOrder. The last 3 characters ofordno
in FDeal represent the deal sequence number, which means they are different for each deal events.