定義全局環(huán)境導入xtquant庫,、aiohttp庫,、sanic庫,設(shè)置訪問路由為 from xtquant import xtdata, xttrader, xtconstant from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback from xtquant.xttype import StockAccount import aiohttp, random from sanic import Sanic, Blueprint, response api = Blueprint('xtquant', url_prefix='/xtquant/trade') @api.listener('before_server_start') async def before_server_start(app, loop): '''全局共享session''' global session, trader jar = aiohttp.CookieJar(unsafe=True) session = aiohttp.ClientSession(cookie_jar=jar, connector=aiohttp.TCPConnector(ssl=False)) trader = Trader() @api.listener('after_server_stop') async def after_server_stop(app, loop): '''關(guān)閉session''' await session.close() order_status = { xtconstant.ORDER_UNREPORTED: '未報', xtconstant.ORDER_WAIT_REPORTING: '待報', xtconstant.ORDER_REPORTED: '已報', xtconstant.ORDER_REPORTED_CANCEL: '已報待撤', xtconstant.ORDER_PARTSUCC_CANCEL: '部成待撤', xtconstant.ORDER_PART_CANCEL: '部撤', xtconstant.ORDER_CANCELED: '已撤', xtconstant.ORDER_PART_SUCC: '部成', xtconstant.ORDER_SUCCEEDED: '已成', xtconstant.ORDER_JUNK: '廢單', xtconstant.ORDER_UNKNOWN: '未知' } if __name__ == '__main__': app = Sanic(name='xtquant') app.config.RESPONSE_TIMEOUT = 600000 app.config.REQUEST_TIMEOUT = 600000 app.config.KEEP_ALIVE_TIMEOUT = 600 app.blueprint(api) app.run(host='0.0.0.0', port=7800, workers=1, auto_reload=True, debug=False) 定義交易賬戶全局信息類這里使用單例模式,,確保Trader中的RPC實例和賬戶信息唯一:
賬號登錄隨機設(shè)置一個session_id值,,并傳入極簡客戶端用戶目錄和賬號ID,、類型,連接客戶端: @api.route('/login', methods=['GET']) async def login(request): ''' 賬號登錄 ''' session_id = int(request.args.get('session_id', random.randint(20000, 60000))) qmt_dir = request.args.get('qmt_dir', 'C:\\gszq\\qmt\\userdata_mini') account_id = request.args.get('account_id', '') account_type = request.args.get('account_type', 'STOCK') # 賬號類型,,可選STOCK,、CREDIT connect_result = trader.set_trader(qmt_dir, session_id) trader.set_account(account_id, account_type=account_type) print('交易連接成功!') if connect_result == 0 else print('交易連接失??!') return response.json({'status': connect_result}) 查詢功能封裝查詢資產(chǎn)、當前持倉,、當日成交,、當日委托等:
交易功能封裝包含下單和撤單兩類,,下單成功會返回一個大于0的訂單號,,撤單只需傳入這個訂單號即可,,撤單成功返回0。
@api.route('/place_order', methods=['GET']) async def trade_place_order(request): ''' 下單 ''' stock_code = request.args.get('stock_code', '510300.SH') direction = xtconstant.STOCK_BUY if request.args.get('direction', 'buy') == 'buy' else xtconstant.STOCK_SELL volumn = int(request.args.get('volumn', '100')) price = float(request.args.get('price', '4.4')) order_id = trader.xt_trader.order_stock(trader.account, stock_code, direction, volumn, xtconstant.FIX_PRICE, price, 'strategy_name', 'remark') return response.json({'order_id': order_id}, ensure_ascii=False) @api.route('/cancel_order', methods=['GET']) async def trade_cancel_order(request): ''' 撤單 ''' order_id = int(request.args.get('order_id', '0')) cancel_order_result = trader.xt_trader.cancel_order_stock(trader.account, order_id) return response.json({'cancel_order_result': cancel_order_result}, ensure_ascii=False) 功能測試0x00 啟動服務器端
0x01 登錄 import requests print(requests.get('http://127.0.0.1:7800/xtquant/trade/login?account_id=******').json()) {'status': 0} 0x02 下單
0x03 撤單 print(requests.get('http://127.0.0.1:7800/xtquant/trade/cancel_order?order_id=403701775').json()) {'cancel_order_result': 0} 0x04 查詢訂單
注意:僅供參考,使用后果自負,,由此造成的損失概不負責,。為保證安全請設(shè)置訪問防火墻,禁止用于非法用途,! |
|