返回列表 发布新帖

场景_xtquant常用的三种下单方式

263 1
发表于 2024-4-26 17:50:04 | 显示全部楼层 阅读模式

一、问题

投资者在执行交易时通常会采用三种基本的下单方式:按股数下单、按金额下单以及按账户可用比例下单。然而,如何在xtquant上实现这些下单策略呢?

本帖将通过实际的代码示例,来展示如何利用xtquant来实现上述这些报价方式,以满足不同的交易需求。

二、解答

原生Python

import pandas as pd
import numpy as np
import datetime
from xtquant import xtdata,xttrader
from xtquant.xttype import StockAccount
from xtquant import xtconstant
from xtquant.xttrader import XtQuantTraderCallback
import sys
import time

class MyXtQuantTraderCallback(XtQuantTraderCallback):
    # 用于接收回调信息的类
    def on_stock_order(self, order):
        """
        委托回报推送
        :param order: XtOrder对象
        :return:
        """
        print(datetime.datetime.now(), '委托回调', order.order_remark,order.stock_code,order.order_volume,order.offset_flag)


    def on_stock_trade(self, trade):
        """
        成交变动推送
        :param trade: XtTrade对象
        :return:
        """
        print(datetime.datetime.now(), '成交回调', trade.order_remark,trade.stock_code,trade.traded_volume,trade.offset_flag)


    def on_order_error(self, order_error):
        """
        委托失败推送
        :param order_error:XtOrderError 对象
        :return:
        """
        # print("on order_error callback")
        # print(order_error.order_id, order_error.error_id, order_error.error_msg)
        print(f"委托报错回调 {order_error.order_remark} {order_error.error_msg}")
    def on_order_stock_async_response(self, response):
        """
        异步下单回报推送
        :param response: XtOrderResponse 对象
        :return:
        """

        print(datetime.datetime.now(),f"异步委托回调 {response.order_remark} {response.stock_code}")

callback = MyXtQuantTraderCallback()
# 填投研端的期货账号
# account = StockAccount("1000024",account_type = "FUTURE")
# 填写投研端的股票账号
account = StockAccount("2000567")
# 填投研端的userdata路径
xt_trade = xttrader.XtQuantTrader(r"C:\Program Files\迅投极速交易终端睿智融科版\userdata",int(time.time()))
# 注册接受回调
xt_trade.register_callback(callback) 
# 启动交易线程
xt_trade.start()
# 链接交易
connect_result = xt_trade.connect()
# 订阅账号信息,接受这个账号的回调,回调是账号维度的
subscribe_result = xt_trade.subscribe(account)
print(subscribe_result)

################### 提示 ######################
# 交易所支持的报价方式,只支持按指定价/指定股数下单,或市价/指定股数下单
# 所以下列如"按金额下单", "按账户比例下单", "对手价下单",实际上是把操作转换成按指定价/指定股数下单


###############################################

code = "600000.SH" # 交易标的

tick = xtdata.get_full_tick([code])[code]

last_price = tick["lastPrice"] # 最新价

ask_price = round(tick["askPrice"][0],3) # 买方对手价

# 按股数下单
lots = 200 # 下200股
res_id = xt_trade.order_stock_async(account, code, xtconstant.STOCK_BUY, lots, xtconstant.FIX_PRICE, last_price, "最新/固定手数", "最新/固定手数") # 
res_id = xt_trade.order_stock_async(account, code, xtconstant.STOCK_BUY, lots, xtconstant.FIX_PRICE, ask_price, "对手/固定手数", "对手/固定手数") # 
print("下单结果:", res_id)
# 按金额下单
amount = 10000 # 下单一万元
lots = amount / last_price // 100 * 100
res_id = xt_trade.order_stock_async(account, code, xtconstant.STOCK_BUY, lots, xtconstant.FIX_PRICE, last_price, "最新/固定金额", "最新/固定金额") # 
res_id = xt_trade.order_stock_async(account, code, xtconstant.STOCK_BUY, lots, xtconstant.FIX_PRICE, ask_price, "对手/固定金额", "对手/固定金额") # 
print("下单结果:", res_id)

# 按账户可用比例金额比例下单
account_info = xt_trade.query_stock_asset(account)
print("账号",account_info.account_id, "可用资金",account_info.cash)
buy_ratio = 0.001 # 按可用千分之一的金额下单
amount = account_info.cash * buy_ratio
lots = amount / last_price // 100 * 100
res_id = xt_trade.order_stock_async(account, code, xtconstant.STOCK_BUY, lots, xtconstant.FIX_PRICE, last_price, "最新/固定比例", "最新/固定比例") # 
res_id = xt_trade.order_stock_async(account, code, xtconstant.STOCK_BUY, lots, xtconstant.FIX_PRICE, ask_price, "对手/固定比例", "对手/固定比例") # 

xtdata.run()

不清楚的内容可添加下方助理微信咨询,有其他 QMT 小技巧想学习的吗?欢迎在下方留言,笔者将根据大家的留言持续更新哦!

欢迎和我一起加入迅投组建的 QMT 实战交流社群,交流群内有许多做量化交易的高手和大佬,具有良好的分享和互助氛围。且迅投官方会不定期为多次分享、乐于助人的群友申请送投研专业版的机会。

只需扫描下方的二维码,名额有限,限时加入。一起分享见解、交换信息、并共同进步,就像群友说的:“就算周末,晚上也有地方沟通交流!”

企业微信截图_17151655836859.png

评论1

心如止水
发表于 2024-4-29 09:57:23 | 显示全部楼层
还有其他想学习的 QMT 小技巧吗?欢迎大家下方留言~

回复

您需要登录后才可以回帖 登录 | 立即注册

主题

16

回帖

63

积分

0

客服专线

400-080-8112

用思考的速度交易,用真诚的态度合作,我们是认真的!
  • 关注公众号
  • 添加微信客服
Copyright © 2001-2024 迅投QMT社区 版权所有 All Rights Reserved. 蜀ICP备19002686号-2
关灯 快速发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表