3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
Gamma Scalping 是一种经典的期权对冲交易策略。其核心思想是买入具有高 Gamma 的期权(通常为平值期权),使持仓头寸保持 Delta 中性。当标的资产价格发生剧烈波动时,Delta 会随之改变,交易员通过频繁买卖标的资产(如 ETF 或期货)调整标的头寸,重新拉回 Delta 中性,从而赚取标的资产波动带来的“波动率收益”。
在 QMT (迅投量化交易平台) 中,我们可以结合内置的 BSM 期权定价函数与底层交易接口,快速构建自动化 Gamma Scalping 策略。
510300.SH 及其对应的认购/认沽期权),设定交易账号与对冲阈值(Delta 偏离阈值)。handlebar 或定时器回调中,获取期权及标的最新行情,调用 QMT 内置的 ContextInfo.bsm_price 或 ContextInfo.bsm_iv 等函数计算期权理论 Delta。passorder 或 order_shares 自动买卖标的资产,使总 Delta 重新归零(回到中性)。#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)
delta_threshold,避免微小波动触发无意义的频繁对冲。