返回列表 发布新帖

miniQMT保姆级新手必学教程 量化程序化证券交易极简部署 数据交易接口配置简单轻松上手 开发python策略demo含源代码

637 0
发表于 2024-1-10 17:00:18 | 显示全部楼层 阅读模式

QMT软件样式

首先,我们需要下载并安装QMT终端,然后才能对软件进行Python库的下载,这样才能更方便地使用这个软件。miniQMT是QMT内部的极简版,可以摆脱很多限制实现策略代码本地运行,对于个人量化投资者来说这是一个低门槛享受机构投资者量化交易的软件。

python库安装

miniQMT配置流程

①请先联系开通指定证券账户后, 再开通QMT交易权限,会在电子邮箱收到下载地址。

②然后在设置》系统设置》模型设置》点击下载“python库下载”

③下载完成后找到 QMT文件安装目录下//bin.x64//lib//site-packags//xtquant

④复制到本地python环境下的//site-packags文件夹

⑤同时必须登录极简版QMT(XtMiniQmt.exe) 就可以本地调用python的miniQMT接口了。

⑥运行导入行情模块xtdata和交易模块xttrader, 实现本地程序化交易,开户咨询微信miniqmt

miniqmt

miniQMT的python环境运行

将下面的代码复制到您的IDE工具,更改代码中的path路径和xXtMiniQmt.exe启动路径,以及StockAccount账号,就可以开启量化之旅了,祝您投资顺利!

miniqmt

本地运行代码

# -*- coding: utf-8 -*-
from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback
from xtquant.xttype import StockAccount
from xtquant import xtconstant


class MyXtQuantTraderCallback(XtQuantTraderCallback):
    def on_disconnected(self):
        print("连接断开")

    def on_stock_order(self, order):
        print("委托回报")
        print(order.stock_code, order.order_status, order.order_sysid)

    def on_stock_asset(self, asset):
        print("资金变动")
        print(asset.account_id, asset.cash, asset.total_asset)

    def on_stock_trade(self, trade):
        print("成交变动")
        print(trade.account_id, trade.stock_code, trade.order_id)

    def on_stock_position(self, position):
        print("持仓变动")
        print(position.stock_code, position.volume)

    def on_order_error(self, order_error):
        print("委托失败")
        print(order_error.order_id, order_error.error_id, order_error.error_msg)

    def on_cancel_error(self, cancel_error):
        print("撤单失败")
        print(cancel_error.order_id, cancel_error.error_id, cancel_error.error_msg)

    def on_order_stock_async_response(self, response):
        print("异步下单回")
        print(response.account_id, response.order_id, response.seq)

    def on_account_status(self, status):
        print("账户状态")
        print(status.account_id, status.account_type, status.status)


if __name__ == "__main__":
    print("开始运行")
    # path为安装目录下userdata_mini路径,"D:\\QMT\\"需要根据自己的路径修改
    path = 'D:\\QMT\\userdata_mini'

    # 打开可执行文件XtMiniQmt.exe "D:\\QMT\\"需要根据自己的路径修改
    import subprocess
    process = subprocess.Popen('D:\\QMT\\bin.x64\\XtMiniQmt.exe')

    session_id = 123456
    xt_trader = XtQuantTrader(path, session_id)
    callback = MyXtQuantTraderCallback()
    xt_trader.register_callback(callback)
    xt_trader.start()
    connect_result = xt_trader.connect()
    if connect_result != 0:
        import sys
        sys.exit('xtquant链接失败,请确认xtminiqmt登录后再重试,程序即将退出 %d' % connect_result)
    # 创建资金账号为"55002303"的证券账号对象,需要改为自己的账号
    acc = StockAccount('55002303')
    subscribe_result = xt_trader.subscribe(acc)
    if subscribe_result != 0:
        print('账号订阅失败 %d' % subscribe_result)
    print(subscribe_result)
    stock_code = '600000.SH'

    order_go = xt_trader.order_stock(acc, stock_code, xtconstant.STOCK_BUY, 200, xtconstant.FIX_PRICE, 10.5,
                                                'strategy_name', 'remark')
    print(order_go)
    cxl_order = xt_trader.cancel_order_stock(acc, order_go)
    print(cxl_order)
    async_seq = xt_trader.order_stock(acc, stock_code, xtconstant.STOCK_BUY, 200, xtconstant.FIX_PRICE, 10.5,
                                      'strategy_name', 'remark')
    print(async_seq)

    asset = xt_trader.query_stock_asset(acc)
    if asset:
        print("资产:")
        print("可用资金 {0}".format(asset.cash))
    order = xt_trader.query_stock_order(acc, order_go)
    if order:
        print("委托:")
        print("委托 {0}".format(order.order_id))
    orders = xt_trader.query_stock_orders(acc)
    print("所有委托:", len(orders))
    if len(orders) != 0:
        print("最终委托:")
        print("{0} {1} {2}".format(orders[-1].stock_code, orders[-1].order_volume, orders[-1].price))
    trades = xt_trader.query_stock_trades(acc)
    print("所有交易:", len(trades))
    if len(trades) != 0:
        print("最终成交:")
        print("{0} {1} {2}".format(trades[-1].stock_code, trades[-1].traded_volume, trades[-1].traded_price))
    positions = xt_trader.query_stock_positions(acc)
    print("持仓:", len(positions))
    if len(positions) != 0:
        print("所有持仓:")
        print("{0} {1} {2}".format(positions[-1].account_id, positions[-1].stock_code, positions[-1].volume))
    position = xt_trader.query_stock_position(acc, stock_code)
    if position:
        print("持仓:")
        print("{0} {1} {2}".format(position.account_id, position.stock_code, position.volume))
    xt_trader.run_forever()  

申请QMT测试或者实盘

添加微信

客服专线

400-080-8112

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