🚀 AI 一键生成 qmt 策略代码
立即体验

如何在 qmt 平台实现期权 Gamma Scalping 动态 Delta 对冲策略?(附 Python 完整源码)

qmt | 发布时间: 2026/7/24
以下内容由 EasyQuant 生成。

问题描述

请根据以下 SEO 主题生成完整页面内容:

Title: qmt 期权对冲:Gamma Scalping 动态 Delta 中性策略 Python 实现

Question: 如何在 qmt 中实现期权 Gamma Scalping 动态对冲?

解决方案

如何在 QMT 平台实现期权 Gamma Scalping 动态 Delta 对冲策略

Gamma Scalping 是一种经典的期权对冲交易策略。其核心思想是买入具有高 Gamma 的期权(通常为平值期权),使持仓头寸保持 Delta 中性。当标的资产价格发生剧烈波动时,Delta 会随之改变,交易员通过频繁买卖标的资产(如 ETF 或期货)调整标的头寸,重新拉回 Delta 中性,从而赚取标的资产波动带来的“波动率收益”。

在 QMT (迅投量化交易平台) 中,我们可以结合内置的 BSM 期权定价函数与底层交易接口,快速构建自动化 Gamma Scalping 策略。


策略逻辑与工作流程

  1. 初始化:定义期权合约与标的资产(例如 510300.SH 及其对应的认购/认沽期权),设定交易账号与对冲阈值(Delta 偏离阈值)。
  2. 计算期权 Delta:在 handlebar 或定时器回调中,获取期权及标的最新行情,调用 QMT 内置的 ContextInfo.bsm_priceContextInfo.bsm_iv 等函数计算期权理论 Delta。
  3. 计算组合总 Delta:将持有的期权头寸 Delta 与标的资产(ETF/期货)的持仓数量相加,得到组合的总 Delta。
  4. 动态对冲:当总 Delta 的绝对值超过设定的阈值时,使用 passorderorder_shares 自动买卖标的资产,使总 Delta 重新归零(回到中性)。

Python 完整源码示例

#coding:gbk
import math

def init(ContextInfo):
    # 设定标的资产与期权合约
    ContextInfo.underlying = '510300.SH'      # 标的 ETF
    ContextInfo.option_code = '10003280.SHO'  # 期权合约代码
    ContextInfo.accID = '6000000058'          # 资金账号
    
    # 设置股票池与主推账号
    ContextInfo.set_universe([ContextInfo.underlying, ContextInfo.option_code])
    ContextInfo.set_account(ContextInfo.accID)
    
    # 策略参数设置
    ContextInfo.option_qty = 10               # 持有期权张数(买入长头寸)
    ContextInfo.delta_threshold = 500         # Delta 对冲阈值(当量股数偏离超过500股时对冲)
    ContextInfo.risk_free = 0.025             # 无风险利率 2.5%
    ContextInfo.dividend = 0.0                # 分红率
    
    print("Gamma Scalping 策略初始化完成")

def calculate_option_delta(ContextInfo, opt_code, target_price):
    """
    根据 BSM 模型估算期权的 Delta (简易数值微分或解析法)
    """
    # 获取期权合约详情(行权价、到期日等)
    opt_info = ContextInfo.get_option_detail_data(opt_code)
    if not opt_info:
        return 0.0
    
    strike = opt_info['OptExercisePrice']
    opt_type = 'C' if opt_info['optType'] == 'CALL' else 'P'
    
    # 计算剩余天数 (简单示例,可精确到交易日)
    # 假设获取隐含波动率,若无法获取则给默认值 0.2
    iv = ContextInfo.get_option_iv(opt_code)
    if math.isnan(iv) or iv <= 0:
        iv = 0.20
        
    # 使用有限差分法估算 Delta: [P(S + dS) - P(S - dS)] / (2 * dS)
    dS = 0.001
    price_up = ContextInfo.bsm_price(opt_type, target_price + dS, strike, ContextInfo.risk_free, iv, 15, ContextInfo.dividend)
    price_dn = ContextInfo.bsm_price(opt_type, target_price - dS, strike, ContextInfo.risk_free, iv, 15, ContextInfo.dividend)
    
    if math.isnan(price_up) or math.isnan(price_dn):
        return 0.0
        
    delta = (price_up - price_dn) / (2 * dS)
    return delta

def handlebar(ContextInfo):
    # 仅在最后一根 Bar 执行对冲判定(盘中实时 tick 触发)
    if not ContextInfo.is_last_bar():
        return
    
    # 1. 获取标的与期权的最新价格
    full_tick = ContextInfo.get_full_tick([ContextInfo.underlying, ContextInfo.option_code])
    if ContextInfo.underlying not in full_tick:
        return
        
    target_price = full_tick[ContextInfo.underlying]['lastPrice']
    if target_price <= 0:
        return
        
    # 2. 计算单张期权 Delta
    single_delta = calculate_option_delta(ContextInfo, ContextInfo.option_code, target_price)
    
    # 期权合约乘数(通常为 10000)
    opt_info = ContextInfo.get_option_detail_data(ContextInfo.option_code)
    multiplier = opt_info.get('VolumeMultiple', 10000) if opt_info else 10000
    
    # 期权持仓总 Delta(相当于标的股数)
    total_option_delta_shares = single_delta * ContextInfo.option_qty * multiplier
    
    # 3. 获取标的当前持仓
    underlying_pos_shares = 0
    positions = get_trade_detail_data(ContextInfo.accID, 'STOCK', 'POSITION')
    for pos in positions:
        if pos.m_strInstrumentID == ContextInfo.underlying.split('.')[0]:
            underlying_pos_shares = pos.m_nCanUseVolume
            break
            
    # 4. 计算组合净 Delta (期权 Delta + 标的股票持仓股数)
    net_delta_shares = total_option_delta_shares + underlying_pos_shares
    
    print(f"标的现价: {target_price:.3f} | 单张期权Delta: {single_delta:.4f} | 组合净Delta(股): {net_delta_shares:.2f}")
    
    # 5. Delta 中性对冲逻辑
    # 如果净 Delta 超出设定的正向/负向阈值,则买卖标的资产调整
    if abs(net_delta_shares) > ContextInfo.delta_threshold:
        hedge_shares = -int(net_delta_shares) # 需要调整的标的股数
        
        # A股一手为100股,进行向下取整对齐
        if hedge_shares > 0:
            hedge_shares = (hedge_shares // 100) * 100
        else:
            hedge_shares = -((-hedge_shares // 100) * 100)
            
        if hedge_shares != 0:
            print(f"[触发Delta对冲] 发送标的委托量: {hedge_shares} 股")
            # 使用 order_shares 接口下单 (正数买入,负数卖出)
            order_shares(ContextInfo.underlying, hedge_shares, 'FIX', target_price, ContextInfo, ContextInfo.accID)

注意事项与优化建议

  1. 交易成本与滑点控制:Gamma Scalping 需要频繁对冲标的资产,交易佣金和买卖价差是策略盈利的主要侵蚀因素。建议设置合理的 delta_threshold,避免微小波动触发无意义的频繁对冲。
  2. 精确计算 Greeks:示例中使用了有限差分法估算 Delta,实盘中可直接通过获取 Level-2 或行情接口自带的希腊字母指标,也可使用更精细的 Black-Scholes 解析公式。
  3. 波动率环境:Gamma Scalping 本质上是做多实际波动率(Realized Volatility)。在标的剧烈震荡(高实际波动率)且隐含波动率较低时策略表现最佳。