QMT网格交易策略代码讲解——在设定区间内自动低买高卖,震荡市的经典打法
发布时间:2小时前阅读:12
一、网格交易 vs 其他策略:本质核心区别
✅ 趋势跟踪策略:赚取价格持续单向运行的趋势收益;
✅ 配对交易:赚取两只关联标的价差偏离后回归常态的修复收益;
• 单边下跌:价格跌破网格下限持续走低,策略持续加仓,最终大概率满仓被套。
二、全套QMT网格交易代码框架演示
本策略无需依赖任何复杂技术指标,仅依托标的实时价格完成全部买卖判断,代码结构简洁清晰、注释完整、可直接实盘复用。
import numpy as np
def init(ContextInfo):
# ===== 交易标的 =====
ContextInfo.stock = '510300.SH'
ContextInfo.set_universe([ContextInfo.stock])
# ===== 网格核心参数 =====
ContextInfo.grid_low = 3.70 # 网格区间下限
ContextInfo.grid_high = 4.30 # 网格区间上限
ContextInfo.grid_step = 0.02 # 网格间距(固定0.02元/格)
ContextInfo.per_amount = 10000 # 单格交易金额(元)
# ===== 双层风控参数 =====
ContextInfo.max_position_pct = 0.8 # 最大持仓比例(严控80%以内)
ContextInfo.stop_loss_pct = 0.05 # 破网止损阈值(下限跌破5%无条件清仓)
# ===== 运行状态记录 =====
ContextInfo.last_grid_level = None # 上一根K线网格层级
ContextInfo.total_levels = int((ContextInfo.grid_high - ContextInfo.grid_low) / ContextInfo.grid_step)
def handlebar(ContextInfo):
stock = ContextInfo.stock
# ===== 1. 获取实时最新价格 =====
tick = ContextInfo.get_full_tick([stock])
current_price = tick[stock]['lastPrice']
if current_price <= 0:
return
# ===== 2. 实时映射当前网格层级 =====
# 下限对应0层,上限对应最高层,区间内按步长折算层级
if current_price <= ContextInfo.grid_low:
current_level = 0
elif current_price >= ContextInfo.grid_high:
current_level = ContextInfo.total_levels
else:
current_level = int((current_price - ContextInfo.grid_low) / ContextInfo.grid_step)
# ===== 3. 首次启动策略:自动建立初始底仓 =====
if ContextInfo.last_grid_level is None:
ContextInfo.last_grid_level = current_level
# 取网格中间层级作为底仓基准
mid_level = ContextInfo.total_levels // 2
if current_level <= mid_level:
target_value = ContextInfo.per_amount * (mid_level - current_level + 1)
order_target_value(stock, target_value, ContextInfo)
return
# ===== 4. 破网止损 + 仓位上限双重风控 =====
pos = ContextInfo.get_position(stock)
if pos > 0:
total_asset = ContextInfo.available_cash + pos * current_price
current_pct = (pos * current_price) / total_asset
# 跌破下限5%:破网止损,无条件清仓
if current_price < ContextInfo.grid_low * (1 - ContextInfo.stop_loss_pct):
order_target_value(stock, 0, ContextInfo)
ContextInfo.last_grid_level = None
return
# 持仓超80%:暂停所有买入,杜绝满仓被套
if current_pct >= ContextInfo.max_position_pct and current_level < ContextInfo.last_grid_level:
return
# ===== 5. 判定网格线穿越状态(无穿越则不操作)=====
if current_level == ContextInfo.last_grid_level:
return
# ===== 6. 价格上行穿越网格:自动减仓卖出 =====
if current_level > ContextInfo.last_grid_level:
levels_crossed = current_level - ContextInfo.last_grid_level
sell_amount = ContextInfo.per_amount * levels_crossed
current_position_value = pos * current_price
sell_amount = min(sell_amount, current_position_value)
if sell_amount > 0:
order_target_value(stock, current_position_value - sell_amount, ContextInfo)
# ===== 7. 价格下行穿越网格:自动加仓买入 =====
elif current_level < ContextInfo.last_grid_level:
levels_crossed = ContextInfo.last_grid_level - current_level
buy_amount = ContextInfo.per_amount * levels_crossed
buy_amount = min(buy_amount, ContextInfo.available_cash)
# 二次校验仓位上限,避免超限
total_asset = ContextInfo.available_cash + pos * current_price
after_buy_pct = (pos * current_price + buy_amount) / total_asset if total_asset > 0 else 1
if after_buy_pct <= ContextInfo.max_position_pct and buy_amount > 0:
order_target_value(stock, pos * current_price + buy_amount, ContextInfo)
# ===== 8. 更新当前网格层级,循环监控 =====
ContextInfo.last_grid_level = current_level1、价格网格层级映射逻辑
if current_price <= ContextInfo.grid_low:
current_level = 0
elif current_price >= ContextInfo.grid_high:
current_level = ContextInfo.total_levels
else:
current_level = int((current_price - ContextInfo.grid_low) / ContextInfo.grid_step)
以本次参数为例:下限3.70元、上限4.30元、步长0.02元,区间总计30格。3.70元对应0层、4.30元对应30层、3.72元对应1层、4.10元对应20层。2、首次启动底仓建立逻辑
mid_level = ContextInfo.total_levels // 2
if current_level <= mid_level:
target_value = ContextInfo.per_amount * (mid_level - current_level + 1)
order_target_value(stock, target_value, ContextInfo)
系统默认以网格中间层级为分界,当前价格处于区间下半段时,自动布局底仓,且价格越靠近下限,底仓仓位越重;价格处于上半段则暂不建仓,等待价格回落再布局,规避高位开局风险。
核心代码片段:
逻辑解析:网格策略不看绝对价格点位,核心触发条件是网格层级发生变动。4、最大仓位保护风控逻辑
if current_pct >= ContextInfo.max_position_pct and current_level < ContextInfo.last_grid_level:
return
逻辑解析:网格策略最致命的风险就是单边下跌无限加仓、最终满仓被套,本段代码是核心风控防线。
当账户持仓占比达到80%上限时,系统自动暂停所有买入操作。宁可错过低位加仓的反弹机会,也坚决杜绝重仓深套风险,从根源控制最大回撤。
核心代码片段:
逻辑解析:网格策略成立的前提是「价格在预设区间震荡」。四、QMT代码网格 vs PTrade图形网格:核心优劣对比⚖️
无需编写代码,可视化面板直接填写参数即可启动运行,零门槛上手,适合纯小白快速使用。
支持无限自定义优化,可叠加各类进阶逻辑,突破固定工具限制:
2、叠加ATR指标,动态自适应网格间距,波动大放宽格值、波动小收紧格值;
简单来说:PTrade是固定模板工具,QMT是可定制的策略底座。
1、两大核心固有风险
• 向下破网(深套风险):价格跌破下限持续阴跌,策略持续加仓,若无风控极易满仓被套。
✅ 最适配标的:沪深300、中证500等宽基ETF,流动性充足、无退市风险、长期有底部支撑,震荡属性稳定;
❌ 禁止使用标的:个股!个股易受基本面利空、黑天鹅影响持续单边下跌,网格越跌越买的逻辑会持续放大亏损,风险极高。
股票/量化开户找我呀!股票佣金万0.854(满足条件)!无门槛国债逆回购一折 (百万分之一)!ETF佣金万0.5!免费量化使用量化软件QMT+ptrade! 优惠多多!
【立即咨询开通】
温馨提示:投资有风险,选择需谨慎。
我是新手,想请问网格交易条件单低买高卖如何自动下单呢?
-
REITs认购&交易权限怎么开?和股票基金有3处不同
2026-08-17 15:40
-
新三板是什么?权限怎么开?三大条件缺一不可
2026-08-17 15:40
-
港股通一笔交易贵在哪?汇率结算+3项费用全拆解
2026-08-17 15:40


问一问

+微信
分享该文章
