返回列表 发布新帖

算法研究--QMT策略怎么样迁移到Ptrade

8 0

今天我在把QMT的策略迁移到ptrade,发现2个系统的差别还是很大的,今天就简单的分享一下经验,对比一下框架函数,怎么样快速把QMt策略迁移到ptrade,ptrade实盘代码和回测的代码有的函数使用还是有区别的,不注意就会触发未来函数,

大QMt的写法,比较复杂,对比下来ptrade稍微简单一点

7088f00df05417ede06654cbae535b50.png

ptrade实盘的写法

7b891cc9f770b1a75ba0fef4e1ee1708.png

回测的ptrade写法,还是有很大的差别

2e4389475ebcc33072f890a1d41676e2.png

下面是简单的对比qmt和ptrade系统的差别,方便快速迁移策略

一、策略框架与事件函数

1a97a7e6b8d8ada19f0be7903c086225.png

二、行情数据获取函数

f670568e7c19d69b62ee53b8d306430f.png

三、交易下单函数

0f6be867ff0149d3b76d5cbe60a0ce5c.png

四、账户与交易查询函数

35e08fef36bf9c06fe99695083f32b22.png

五、设置与配置函数

d507b2d4c5d56511a95b02b1a7dc0803.png

六、辅助与信息函数

ca8388e8625ffbe861fd1de634c1b6b5.png

七、期货专用函数

baf1b11b647906b72bce11e5e04955c1.png

八、核心差异总结(迁移必看)

9bb985dd9e6645f59bbec8d4621ecd73.png

快速迁移的核心口诀:
框架先改:init → initialize,handlebar → run_daily
后缀全换:.SZ → .SZ,.SH → .SS
行情重写:get_market_data_ex → get_history
下单简化:passorder(11个参数) → order(3个参数)
查询改名:get_holdings → get_position,get_trade_detail_data → context.portfolio
日志替换:print → log.info
特有功能处理:算法交易、绘图等大QMT特有功能,在PTrade中需自行实现或放弃

不懂的问我就可以,专业的量化技术支持,加我备注入群可以加入量化研究群

3b95a0b55be4b888e2fbdf1e6b49916d.jpg

下面是官方简单的例子,方便怎么样去迁移对比区别参考,qmt例子

#coding:gbk
'''
大QMT 最简单双均线策略(使用run_time定时器优化版)
'''
def init(C):
    C.fast = ''      # 快线周期
    C.slow = ''     # 慢线周期
    C.stock = '1.SZ'  # 标的代码(带后缀)
    C.accountid = '55009640'  # 资金账号
    # 设置定时器:每天14:55:00运行一次 main_logic 函数
    # 格式:'55nSecond' 表示每55秒运行一次,这里用 '1nDay' 表示每天运行一次
    # startTime 设为 '2024-01-01 14:55:00' 表示第一次在14:55触发
    C.run_time("main_logic", "1nDay", "2024-01-01 14:55:00")
    print("定时器已设置,将在每天14:55运行策略")
# 这个是定时器回调函数,入参是 ContextInfo 对象
def main_logic(C):
    # 获取收盘价数据
    df = C.get_market_data_ex(
        ['close'], 
        [C.stock], 
        period='1d', 
        count=C.slow + 5, 
        subscribe=False
    )
    close_list = list(df[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
    # 查询持仓
    positions = get_holdings(C.accountid, 'STOCK')
    vol = 0
    if C.stock in positions:
        vol = positions[C.stock]['CurrentAmount']
    # 交易逻辑
    # 金叉买入
    if prev_fast_ma <= prev_slow_ma and fast_ma > slow_ma:
        if vol == 0:
            passorder(23, 1101, C.accountid, C.stock, 5, -1, 100, 'SimpleMA', 2, '', C)
            print(f'{C.stock} 金叉买入100股')
    # 死叉卖出
    elif prev_fast_ma >= prev_slow_ma and fast_ma < slow_ma:
        if vol > 0:
            passorder(24, 1101, C.accountid, C.stock, 5, -1, vol, 'SimpleMA', 2, '', C)
            print(f'{C.stock} 死叉卖出{vol}股')

ptrade简单例子

'''
PTrade 最简单双均线策略(修正版)
'''
# 初始化函数
def initialize(context):
    context.fast = ''          # 快线周期
    context.slow = ''         # 慢线周期
    context.stock = '0000.SZ'  # 标的代码
 
    run_daily(context, main_logic, time='14:55')
# 自定义策略函数(注意:此函数必须接受 context 作为参数)
def main_logic(context):
    # 获取历史收盘价
    df = get_history(context.slow + 5, '1d', 'close', [context.stock])
    if df is None or len(df) < context.slow + 1:
        return
    close_list = df[context.stock].values
    # 计算均线
    fast_ma = sum(close_list[-context.fast:]) / context.fast
    slow_ma = sum(close_list[-context.slow:]) / context.slow
    prev_fast_ma = sum(close_list[-context.fast-1:-1]) / context.fast
    prev_slow_ma = sum(close_list[-context.slow-1:-1]) / context.slow
    # 查询持仓
    position = get_position(context.stock)
    vol = 0 if position is None else position.total_amount
    # 交易逻辑
    # 金叉买入
    if prev_fast_ma <= prev_slow_ma and fast_ma > slow_ma:
        if vol == 0:
            order(context.stock, 100)
            log.info(f'{context.stock} 金叉买入100股')
    # 死叉卖出
    elif prev_fast_ma >= prev_slow_ma and fast_ma < slow_ma:
        if vol > 0:
            order(context.stock, -vol)
            log.info(f'{context.stock} 死叉卖出{vol}股')

回复

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

客服专线

400-080-8112

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