python回测平台接口_Acqusta TQuant
TradeApi是個(gè)抽象的接口,用戶需要從其他的接口中得到DataApi的實(shí)例。在不同的系統(tǒng)中,如回測(cè)平臺(tái)、策略平臺(tái)等,有不同的實(shí)現(xiàn)和創(chuàng)建方法,但是接口都會(huì)保持一致,方便用戶平滑切換系統(tǒng)。
如從TQuantApi中得到TradeApi實(shí)例:import tquant as tq
tqapi = tq.TQuantApi('ipc://tqc_10001')
dapi = tqapi.data_api()
tapi = tqapi.trade_api()
接口原型#class TradeApi:
def set_on_order_status(self, callback): pass
def set_on_order_trade(self, callback): pass
def set_on_account_status(self, callback): pass
def account_status(self): pass
def query_balance(self, account_id): pass
def query_trades(self, account_id): pass
def query_orders(self, account_id): pass
def query_positions(self, account_id): pass
def place_order(self, account_id, code, price, size, action, order_id=0): pass
def cancel_order(self, account_id, code, entrust_no="", order_id=0): pass
def query(self, account_id, command, params=""): pass
除了設(shè)置回調(diào)函數(shù)外,其他的函數(shù)都返回兩個(gè)值。
查詢交易帳號(hào)連接狀態(tài)#def account_status(self): pass
參數(shù)無(wú)
返回值accounts AccountInfo的數(shù)組
err_msg 錯(cuò)誤原因,當(dāng) accounts is None時(shí)有意義。
該方法用于檢查交易帳號(hào)連接狀態(tài),返回值是一個(gè)數(shù)組。
例子In [8]: accounts, msg = tapi.account_status()
In [9]: accounts
Out[9]:
[{'account': '123456',
'account_id': 'stk',
'account_type': '',
'broker': '證券公司',
'msg': '',
'status': 'Connected'}]
查詢資金#def query_balance(self, account_id): pass
參數(shù)account_id 帳號(hào)編號(hào)
返回值balance dict類型,字段參考《TradeApi概述》的Balance結(jié)構(gòu)。
err_msg 錯(cuò)誤原因,當(dāng) balance is None時(shí)有意義。
例子In [34]: bal, _ = tapi.query_balance('stk')
In [35]: bal
Out[35]:
{'account_id': 'stk',
'close_pnl': 0.0,
'enable_balance': 57588.93,
'float_pnl': 0.0,
'fund_account': '',
'init_balance': 0.0,
'margin': 0.0}
查詢持倉(cāng)#def query_positions(self, account_id): pass
參數(shù)account_id 帳號(hào)編號(hào)
返回值positions DataFrame類型,字段參考《TradeApi概述》的Position結(jié)構(gòu)。
err_msg 錯(cuò)誤原因,當(dāng) positions is None時(shí)有意義。
例子In [37]: positions,_ = tapi.query_positions('stk')
In [38]: positions
Out[38]:
account_id close_pnl code commission cost cost_price \
0 stk 0.0 518880.SH 0.0 0.0 2.755
1 stk 0.0 600410.SH 0.0 0.0 10.458
2 stk 0.0 600900.SH 0.0 0.0 15.287
3 stk 0.0 601018.SH 0.0 0.0 5.454
4 stk 0.0 131990.SZ 0.0 0.0 0.000
5 stk 0.0 150210.SZ 0.0 0.0 0.762
current_size enable_size float_pnl frozen_size init_size last_price \
0 5100 5100 -129.19 0 0 2.730
1 1400 1400 -1717.57 0 0 9.230
2 900 900 631.95 0 0 15.990
3 2500 2500 -782.54 0 0 5.140
4 0 0 0.00 0 0 100.000
5 10000 10000 1490.50 0 0 0.911
margin name side today_size
0 0.0 黃金ETF Long 0
1 0.0 華勝天成 Long 0
2 0.0 長(zhǎng)江電力 Long 0
3 0.0 寧波港 Long 0
4 0.0 標(biāo)準(zhǔn)券 Long 0
5 0.0 國(guó)企改B Long 0
查詢訂單#def query_orders(self, account_id): pass
參數(shù)account_id 帳號(hào)編號(hào)
返回值orders DataFrame類型,字段參考《TradeApi概述》的Order結(jié)構(gòu)。
err_msg 錯(cuò)誤原因,當(dāng) orders is None時(shí)有意義。
例子In [37]: orders,_ = tapi.query_orders('stk')
In [38]: orders
Out[38]:
查詢成交#def query_trades(self, account_id): pass
參數(shù)account_id 帳號(hào)編號(hào)
返回值trades DataFrame類型,字段參考《TradeApi概述》的Order結(jié)構(gòu)。
err_msg 錯(cuò)誤原因,當(dāng) trades is None時(shí)有意義。
例子In [37]: trades,_ = tapi.query_trades('stk')
In [38]: trades
Out[38]:
下單#def place_order(self, account_id, code, price, size, action, order_id=0): pass
參數(shù)account_id 字符串類型,帳號(hào)編號(hào)。
code 代碼,字符串類型,例子: 000001.SH。
price 委托價(jià)格,double類型,單位:元。
size 委托數(shù)量,32位或64位整數(shù)類型,單位:股。
action 委托動(dòng)作,字符串,例子: "Buy"。
order_id 自定義訂單編號(hào),32位整數(shù)。
返回值order_info dict類型,包括 entrust_no和order_id或者其中之一。
err_msg 錯(cuò)誤原因,當(dāng) order_info is None時(shí)有意義。
同步下單接口
在股票交易接口中,提交訂單給柜臺(tái)后需要同步等待柜臺(tái)的處理結(jié)果,處理結(jié)果中包含委托編號(hào),這是個(gè)同步過(guò)程,因此order_info中可以包含entrust_no。
異步下單接口
對(duì)于ctp這種異步的交易接口,發(fā)送訂單給柜臺(tái)后,無(wú)需同步等待,柜臺(tái)會(huì)通過(guò)異步消息告訴客戶端處理結(jié)果。異步消息中包含委托編號(hào)和訂單編號(hào)。請(qǐng)求和響應(yīng)通過(guò)訂單編號(hào)關(guān)聯(lián)。TradeApi的異步過(guò)程和ctp的異步過(guò)程一樣。客戶端給tquant提交了下單請(qǐng)求后,會(huì)立即得到tquant自己的訂單編號(hào)(order_id)。然后在訂單狀態(tài)通知中通過(guò)order_id得到下單結(jié)果。
當(dāng)參數(shù)order_id是0時(shí),表示由tquant進(jìn)行編號(hào),否則由客戶端編號(hào),此時(shí)客戶端需要自己保證訂單幫的唯一性。通常用戶不用自己進(jìn)行編號(hào)。
撤單#def cancel_order(self, account_id, code, entrust_no="", order_id=0): pass
參數(shù)account_id 字符串類型,帳號(hào)編號(hào),不為空。
code 代碼,字符串類型,不為空,例子: 000001.SH。
entrust_no 委托編號(hào),字符串類型。
order_id 訂單編號(hào),整數(shù)類型。
返回值result boolean類型,成功或者失敗。
err_msg 當(dāng) result is None 或者 == False時(shí),包含錯(cuò)誤原因。
可以通過(guò)order_id或者entrust_no來(lái)撤單。
例子
通用查詢接口#def query(self, account_id, command, params=""): pass
參數(shù)account_id 字符串類型,帳號(hào)編號(hào),不為空。
command 字符串類型,查詢命令
params 字符串類型,查詢參數(shù)
返回值
result 字符串
err_msg 當(dāng) result is None時(shí),包含錯(cuò)誤原因。
通用查詢接口用于實(shí)現(xiàn)不能標(biāo)準(zhǔn)化的查詢指令。如查詢ctp交易接口中的代碼表。
例子txt, msg = tapi.query('simnow','ctp_codetable')
回調(diào)通知#def set_on_order_status(self, callback): pass
def set_on_order_trade(self, callback): pass
def set_on_account_status(self, callback): pass
TradeApi提供三個(gè)回調(diào)通知,訂單狀態(tài)、成交回報(bào)和交易帳號(hào)狀態(tài)變化通知。
三個(gè)回調(diào)函數(shù)的參數(shù)類型分別是 Order, Trade, 以及AccountStatus。字段定義見《TradeApi概述》。
例子:def on_order_status(order):
print "on_order", order
def on_order_trade(trade):
print "on_trade", trade
def on_account_status(account):
print "on_account", account
tapi.set_on_order_status (on_order_status)
tapi.set_on_order_trade (on_order_trade)
tapi.set_on_account_status (on_account_status)
總結(jié)
以上是生活随笔為你收集整理的python回测平台接口_Acqusta TQuant的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 手机QQ轻聊版,3.2.0升级3.3.1
- 下一篇: [Python]远程SSH库Parami