返回列表 发布新帖

量化研究--策略迁移算法1研究

5 0

今天我们简单的介绍一下,策略迁移的策略,目前大概有3种处理方式,直接迁移代码,对编程要求最高,本地的策略怎么样迁移到大QMT,大QMT策略怎么样迁移到本地,这个主要是在看见的兼容,大qmt有固定的交易框架,init,haedbar,run_time,3个核心的部分,其他的可有可无,init,做全局设计,参数的配置,haedbar做策略的启动,安固定的周期去运行策略,默认是3秒,run_time类似,haedbar,都是启动策略的,run_time比较灵活,扩展性好,支持定时和循环,下面是我做了简单的函数对比,方便参考迁移

1行情数据接口对照

d82eaef75ce6aa65b1e8492250b67f27.jpg

2交易接口对照

本地QMT:交易下单需先创建 连接对象,然后通过 连接对象.函数名() 调用。

大QMT

:交易下单直接调用全局函数 passorder

c742f8ec5a17f6dc948030c7e8822ff2.png

3策略框架与辅助函数对照

ddaea28cd9707dfad657d219e64b982e.png

4总结:迁移策略时需要调整的关键点

21b07d158aae05230c4553dcbfd2723e.png

大qmt简单的例子代码,来自官方例子,不懂的问我就可以

1ab311609842663b68723594475ee6ea.jpg

#coding:gbk
'''
大QMT双均线策略示例(在QMT内置编辑器运行)
金叉买入,死叉卖出
'''
def init(C):
    # 设置均线参数
    C.fast = 5      # 快线周期
    C.slow = 20     # 慢线周期
    C.stock = C.stockcode + '.' + C.market  # 当前主图品种
    C.accountid = "testS"  # 回测资金账号(可任意)
    print(f"大QMT策略初始化,标的:{C.stock}")
def handlebar(C):
    # 获取当前K线时间
    bar_time = timetag_to_datetime(C.get_bar_timetag(C.barpos), '%Y%m%d%H%M%S')


    # 获取足够的历史收盘价数据
    local_data = C.get_market_data_ex(
        ['close'], 
        [C.stock], 
        end_time = bar_time, 
        period = C.period, 
        count = C.slow + 5, 
        subscribe = False
    )


    close_list = list(local_data[C.stock].iloc[:, 0])
    if len(close_list) < C.slow + 1:
        return


    # 计算快慢均线
    fast_ma = sum(close_list[-C.fast:]) / C.fast
    slow_ma = sum(close_list[-C.slow:]) / C.slow
    prev_fast_ma = sum(close_list[-C.fast-1:-1]) / C.fast
    prev_slow_ma = sum(close_list[-C.slow-1:-1]) / C.slow


    # 获取当前持仓
    current_vol = 0
    holdings = get_holdings(C.accountid, "STOCK")
    if C.stock in holdings:
        current_vol = holdings[C.stock]['CurrentAmount']


    # 金叉买入
    if prev_fast_ma <= prev_slow_ma and fast_ma > slow_ma:
        if current_vol == 0:
            msg = f"金叉买入 {C.stock} 100股"
            passorder(23, 1101, C.accountid, C.stock, 5, -1, 100, '双均线策略', 2, msg, C)
            print(f"{bar_time} {msg}")


    # 死叉卖出
    elif prev_fast_ma >= prev_slow_ma and fast_ma < slow_ma:
        if current_vol > 0:
            msg = f"死叉卖出 {C.stock} 全部持仓"
            passorder(24, 1101, C.accountid, C.stock, 5, -1, current_vol, '双均线策略', 2, msg, C)
            print(f"{bar_time} {msg}")

回复

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

客服专线

400-080-8112

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