- 返回
- 系统生成的订单编号,成功委托后的订单编号为大于0的正整数,如果为-1表示委托失败
我用以下代码进行测试,callback偶尔会有委托失败的信息回来,但是大部分情况是成交还是委托失败还是撤单都没有信息返回。
而且委托失败了也不会返回-1
import random
from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback
import xtquant.xtconstant as xtconstant
# miniQMT安装路径
mini_qmt_path = r'F:\QMT\QMT\国金证券QMT交易端\userdata_mini'
# mini_qmt_path = r'F:\QMT_ceshi\国金QMT交易端模拟\userdata_mini'
# 创建session_id,随机的整数
session_id = int(random.randint(100000, 999999))
# 创建交易对象
xt_trader = XtQuantTrader(mini_qmt_path, session_id)
# 启动交易对象
class MyCallback(XtQuantTraderCallback):
def on_stock_order(self, order):
"""委托状态推送(如报单、撤单、成交等)"""
print(f"[{datetime.datetime.now()}] 委托更新: {order.stock_code}, 状态={order.order_status}")
def on_stock_trade(self, trade):
"""成交推送"""
print(f"[{datetime.datetime.now()}] 成交: {trade.stock_code}, 数量={trade.traded_volume}")
def on_order_error(self, order_error):
"""委托失败推送"""
print(f"[{datetime.datetime.now()}] 委托失败: 错误码={order_error.error_id}, 原因={order_error.error_msg}")
def on_cancel_error(self, cancel_error):
"""撤单失败推送"""
print(f"撤单失败: 单号={cancel_error.order_id}, 错误码={cancel_error.error_id}")
def on_disconnected(self):
"""连接断开"""
print("交易连接已断开,需重新连接!")
xt_trader.register_callback(MyCallback)
xt_trader.start()
# 连接客户端
connect_result = xt_trader.connect()
from xtquant.xttype import StockAccount
# QMT账号
# account_id_s = '8886008161'
account_id_f = '809209213'
# account_id_s = '39956818'
# account_id_f = '196975'
# 创建账号对象
# account_s = StockAccount(account_id)
account_f = StockAccount(account_id_f, 'FUTURE')
# 获取持仓信息
# list[昨空仓, 今空仓, 昨多仓, 今多仓]
positions = xt_trader.query_position_statistics(account_f)
# 获取主力
main_symbol = xtdata.get_main_contract('ru00.SF')
orders = xt_trader.query_stock_orders(account_f, True)
from xtquant.xttrader import XtQuantTraderCallback
from xtquant import xtconstant
import datetime
def Buy_To_Cover_History(account=None, main_symbol=None, order_type=xtconstant.FUTURE_CLOSE_SHORT_HISTORY,
order_volume=0, price_type=xtconstant.LATEST_PRICE, price=0,
strategy_name='strategy1', order_remark='order_test', xt_trader=None):
seq = xt_trader.order_stock(account=account,
stock_code=main_symbol,
order_type=order_type,
order_volume=order_volume,
price_type=price_type,
price=price,
strategy_name=strategy_name,
order_remark=order_remark)
return seq
s2 = Buy_To_Cover_History(account=account_f, main_symbol=main_symbol, order_volume=1, price_type=xtconstant.LATEST_PRICE, price=0,
xt_trader=xt_trader)
|