- 26行中的证券账户改成你的账户编码
- 17行改成你本机userdata_mini目录所在的路径
- 41行是股票代码
- 45行是股票价格
- 仅供学习参考,据博文内容进行买卖操作以及程序bug造成损失,盈亏自负,与博主无关
from itertools import starmap
import time
import pandas as pd
from xtquant.xttrader import XtQuantTrader
from xtquant.xttype import StockAccount
from xtquant import xtconstant
from Scallback import *
# import Scallback
#显示设置
pd.set_option('max_rows',None) #显示最多行数
pd.set_option('max_columns',None) #显示最多列数
pd.set_option('expand_frame_repr',False) #当列太多时显示不清楚
pd.set_option('display.unicode.east_asian_width',True) #设置输出右对齐
#path 为mini qmt客户端的userdata_mini路径
path=r'F:\gszqqmt\userdata_mini'
#session_id 为会话编号,策略使用方对于不同的Python策略需要使用不同的会话编号
session_id=int(time.time())
#创建交易API实例
xt_trader=XtQuantTrader(path,session_id)
#print(xt_trader)
acc=StockAccount('这里输入你的账号')
#创建交易回调对象,并声明接收回调
# callback=MyXtQuantTraderCallback()
callback=MyXtQuantTraderCallback()
xt_trader.register_callback(callback)
#启动交易线程
xt_trader.start()
#建立交易连接,返回0表示成功
connect_result=xt_trader.connect()
if connect_result != 0:
import sys
sys.exit('连接QMTmin失败,程序将退出 %d' % connect_result)
if __name__=='__main__':
stock_code='600050.SH' #证券代码
order_type=xtconstant.STOCK_BUY #xtconstant.STOCK_BUY:证券买入 xtconstang.STOCK_SELL: 证券卖出
price_type=xtconstant.FIX_PRICE #xtconstant.LATEST_PRICE:最新价 xtconstant.FIX_PRICE:限价
order_volume=200 # order_volume: 委托数量,股票以'股' 为单位,债券以'张'为单位
price=7.29 #price:报价价格,如果price_type 为指定价,那price须指定价格,否则为0.
strategy_name='strategy_name' #strategy_name:策略名称
order_remark='remark' # order_remark : 委托备注
#同步下单报单
seq=xt_trader.order_stock(acc,stock_code,order_type,order_volume,price_type,price,
'strategy_name','remark')
print("下单编号:",seq)
# 根据订单号查询委托
orders=xt_trader.query_stock_orders(acc,seq)
以下文件命名为 Scallback.py与上面的主脚本放到同一目录下.
from xtquant.xttrader import XtQuantTraderCallback
import time, datetime, traceback, sys
class MyXtQuantTraderCallback(XtQuantTraderCallback):
def on_disconnected(self):
"""
连接状态回调
:return:
"""
print(datetime.datetime.now(),"连接断开connection lost")
def on_account_status(self, status):
"""
账号状态信息推送
:param response: XtAccountStatus对象
:return:
"""
print("账号状态信息推送on_account_status")
print(status.account_id,status.account_type,status.status)
print(datetime.datetime.now(), sys._getframe().f_code.co_name)
def on_stock_asset(self,asset):
"""
资金变动推送
:param asset: XtAsset对象
:return:
"""
print("资金变动推送on asset callback")
print(asset.account_id,asset.cash,asset.total_asset)
def on_stock_order(self, order):
"""
委托回报推送
:param order: XtOrder对象
:return:
"""
print(datetime.datetime.now(), '委托回调', order.order_remark)
print(order.stock_code, order.order_status, order.order_sysid)
def on_stock_trade(self, trade):
"""
成交变动推送
:param trade: XtTrade对象
:return:
"""
print(datetime.datetime.now(), '成交回调', trade.order_remark)
print(trade.account_id,trade.stock_code,trade.order_id)
def on_stock_position(self, position):
"""
持仓变动推送
:param position: XtPosition对象
:return:
"""
print("on position callback")
print(position.stock_code, position.volume)
def on_order_error(self,order_error):
"""
委托失败推送
:param order_error: XtOrderError 对象
:return:
"""
print("委托失败单号【",order_error.order_id,"】\n","失败错误码:",order_error.error_id,"\n","失败具体说明:", order_error.error_msg)
def on_cancel_error(self, cancel_error):
"""
撤单失败信息推送
:param cancel_error: XtCancelError 对象
:return:
"""
print("on cancel_error callback")
print(cancel_error.order_id, cancel_error.error_id, cancel_error.error_msg)
def on_order_stock_async_response(self, response):
"""
异步下单回报推送
:param response: XtOrderResponse 对象
:return:
"""
print(f"异步委托回调 {response.order_remark}")
print(response.account_id, response.order_id, response.seq)
def on_cancel_order_stock_async_response(self, response):
"""
:param response: XtCancelOrderResponse 对象
:return:
"""
print(datetime.datetime.now(), sys._getframe().f_code.co_name)
print(response.account_id, response.order_id, response.seq)