期权是金融市场中最灵活的衍生品之一,它能提供普通期货无法实现的收益结构。对于量化交易者来说,期权策略的引入可以大大丰富交易工具箱。
本文将介绍:
- 期权的基础概念
- 期权合约的基本要素
- 如何用TqSdk获取期权数据
- 简单期权策略入门
在众多期货量化工具中,**天勤量化(TqSdk)**是目前国内最受欢迎的开源期货量化框架之一:
| 特点 |
说明 |
| 完全免费 |
开源免费,无需付费即可获取实时行情 |
| 数据全面 |
支持国内所有期货交易所的实时行情和历史数据 |
| 上手简单 |
几行Python代码即可获取数据 |
| 期权支持 |
完整支持商品期权和股指期权 |
安装方法:
bash
复制代码
pip install tqsdk
| 概念 |
说明 |
| 期权 |
在未来某时间以约定价格买卖标的物的权利 |
| 权利金 |
购买期权支付的费用 |
| 行权价 |
约定的买卖价格 |
| 到期日 |
期权有效期的最后一天 |
| 分类维度 |
类型 |
说明 |
| 权利类型 |
看涨期权(Call) |
买入标的物的权利 |
|
看跌期权(Put) |
卖出标的物的权利 |
| 行权方式 |
欧式期权 |
只能在到期日行权 |
|
美式期权 |
到期前任意时间可行权 |
| 操作 |
方向 |
最大盈利 |
最大亏损 |
| 买入看涨 |
看多 |
无限 |
权利金 |
| 买入看跌 |
看空 |
行权价-权利金 |
权利金 |
| 卖出看涨 |
看空 |
权利金 |
无限 |
| 卖出看跌 |
看多 |
权利金 |
行权价-权利金 |
| 类型 |
说明 |
| 内在价值 |
立即行权获得的价值 |
| 时间价值 |
期权价格 - 内在价值 |
| 实值期权 |
内在价值 > 0 |
| 虚值期权 |
内在价值 = 0 |
| 平值期权 |
行权价 ≈ 标的价格 |
python
复制代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
功能:获取期权合约基础信息
说明:本代码仅供学习参考
"""
from tqsdk import TqApi, TqAuth
# ============ 初始化 ============
api = TqApi(auth=TqAuth("快期账户", "快期密码"))
# 铜期权示例
# 合约代码格式: 交易所.品种代码+合约月份+C/P+行权价
# C=看涨, P=看跌
OPTION_SYMBOL = "SHFE.cu2502C74000" # 铜2502看涨期权,行权价74000
# 获取期权行情
quote = api.get_quote(OPTION_SYMBOL)
# 获取标的合约行情
UNDERLYING = "SHFE.cu2502"
underlying_quote = api.get_quote(UNDERLYING)
api.wait_update()
print("=" * 60)
print("期权合约信息")
print("=" * 60)
print(f"期权合约: {OPTION_SYMBOL}")
print(f"标的合约: {UNDERLYING}")
print("-" * 60)
print(f"\n期权行情:")
print(f" 最新价: {quote.last_price}")
print(f" 买一价: {quote.bid_price1}")
print(f" 卖一价: {quote.ask_price1}")
print(f" 成交量: {quote.volume}")
print(f" 持仓量: {quote.open_interest}")
print(f"\n标的行情:")
print(f" 最新价: {underlying_quote.last_price}")
# 计算内在价值(看涨期权)
strike_price = 74000
intrinsic_value = max(0, underlying_quote.last_price - strike_price)
time_value = quote.last_price - intrinsic_value
print(f"\n价值分析:")
print(f" 行权价: {strike_price}")
print(f" 内在价值: {intrinsic_value:.0f}")
print(f" 时间价值: {time_value:.0f}")
if intrinsic_value > 0:
print(f" 状态: 实值期权")
elif underlying_quote.last_price == strike_price:
print(f" 状态: 平值期权")
else:
print(f" 状态: 虚值期权")
api.close()
python
复制代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
功能:获取期权链信息
说明:本代码仅供学习参考
"""
from tqsdk import TqApi, TqAuth
api = TqApi(auth=TqAuth("快期账户", "快期密码"))
# 标的合约
UNDERLYING = "SHFE.cu2502"
# 获取标的行情
underlying_quote = api.get_quote(UNDERLYING)
api.wait_update()
current_price = underlying_quote.last_price
print(f"标的合约: {UNDERLYING}")
print(f"当前价格: {current_price:.0f}")
# 定义行权价序列(示例)
strike_prices = [70000, 72000, 74000, 76000, 78000]
print("\n" + "=" * 70)
print("期权链")
print("=" * 70)
print(f"{'行权价':<10} {'看涨价格':<12} {'看涨内在值':<12} {'看跌价格':<12} {'看跌内在值':<12}")
print("-" * 70)
# 获取各行权价期权
for strike in strike_prices:
# 构造合约代码
call_symbol = f"SHFE.cu2502C{strike}"
put_symbol = f"SHFE.cu2502P{strike}"
try:
call_quote = api.get_quote(call_symbol)
put_quote = api.get_quote(put_symbol)
api.wait_update()
# 计算内在价值
call_intrinsic = max(0, current_price - strike)
put_intrinsic = max(0, strike - current_price)
# 标记平值期权
atm_mark = " *" if abs(current_price - strike) < 1000 else ""
print(f"{strike:<10}{atm_mark} {call_quote.last_price:<12.0f} {call_intrinsic:<12.0f} "
f"{put_quote.last_price:<12.0f} {put_intrinsic:<12.0f}")
except:
print(f"{strike:<10} 数据获取失败")
print("\n* 表示接近平值")
api.close()
python
复制代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
功能:买入看涨期权策略
说明:本代码仅供学习参考
"""
from tqsdk import TqApi, TqAuth, TqSim
# ============ 参数设置 ============
UNDERLYING = "SHFE.cu2502" # 标的合约
OPTION_SYMBOL = "SHFE.cu2502C74000" # 看涨期权
STRIKE_PRICE = 74000 # 行权价
LOTS = 1 # 手数
# ============ 初始化 ============
api = TqApi(TqSim(init_balance=100000), auth=TqAuth("快期账户", "快期密码"))
underlying_quote = api.get_quote(UNDERLYING)
option_quote = api.get_quote(OPTION_SYMBOL)
account = api.get_account()
print("=" * 60)
print("买入看涨期权策略")
print("=" * 60)
print(f"标的: {UNDERLYING}")
print(f"期权: {OPTION_SYMBOL}")
print(f"行权价: {STRIKE_PRICE}")
print("-" * 60)
# 等待行情
api.wait_update()
# 买入期权
entry_price = option_quote.last_price
order = api.insert_order(
symbol=OPTION_SYMBOL,
direction="BUY",
offset="OPEN",
volume=LOTS,
limit_price=option_quote.ask_price1
)
print(f"\n[开仓] 买入看涨期权")
print(f" 期权价格: {entry_price}")
print(f" 手数: {LOTS}")
print(f" 成本: {entry_price * 5 * LOTS}") # 假设合约乘数5
# 监控盈亏
while True:
api.wait_update()
if api.is_changing(option_quote):
current_price = option_quote.last_price
underlying_price = underlying_quote.last_price
# 计算浮盈
pnl = (current_price - entry_price) * 5 * LOTS
pnl_pct = (current_price - entry_price) / entry_price * 100
# 计算内在价值
intrinsic = max(0, underlying_price - STRIKE_PRICE)
print(f"\r标的={underlying_price:.0f} 期权={current_price:.0f} "
f"内在值={intrinsic:.0f} 浮盈={pnl:.0f}({pnl_pct:+.1f}%)", end="")
python
复制代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
功能:保护性看跌期权策略(持有期货+买入看跌)
说明:本代码仅供学习参考
"""
from tqsdk import TqApi, TqAuth, TqSim
# ============ 参数设置 ============
UNDERLYING = "SHFE.cu2502"
PUT_OPTION = "SHFE.cu2502P72000" # 看跌期权
STRIKE_PRICE = 72000
FUTURES_LOTS = 1
OPTION_LOTS = 1
# ============ 初始化 ============
api = TqApi(TqSim(init_balance=500000), auth=TqAuth("快期账户", "快期密码"))
future_quote = api.get_quote(UNDERLYING)
put_quote = api.get_quote(PUT_OPTION)
api.wait_update()
print("=" * 60)
print("保护性看跌期权策略")
print("=" * 60)
print("策略说明: 持有期货多头 + 买入看跌期权")
print("效果: 锁定最大亏损,保留上涨收益")
print("-" * 60)
# 买入期货
future_entry = future_quote.last_price
api.insert_order(UNDERLYING, "BUY", "OPEN", FUTURES_LOTS, future_quote.ask_price1)
print(f"\n[期货] 买入 {FUTURES_LOTS}手 @ {future_entry}")
# 买入看跌期权
put_entry = put_quote.last_price
api.insert_order(PUT_OPTION, "BUY", "OPEN", OPTION_LOTS, put_quote.ask_price1)
print(f"[期权] 买入看跌 {OPTION_LOTS}手 @ {put_entry}")
# 计算成本
total_cost = put_entry * 5 * OPTION_LOTS # 期权权利金
print(f"\n保护成本(权利金): {total_cost:.0f}")
print(f"最大亏损(行权价): {future_entry - STRIKE_PRICE + put_entry:.0f}/手")
# 监控组合
while True:
api.wait_update()
if api.is_changing(future_quote):
future_price = future_quote.last_price
put_price = put_quote.last_price
# 期货盈亏
future_pnl = (future_price - future_entry) * 5 * FUTURES_LOTS
# 期权盈亏
option_pnl = (put_price - put_entry) * 5 * OPTION_LOTS
# 组合盈亏
total_pnl = future_pnl + option_pnl
print(f"\r期货={future_price:.0f}(盈亏{future_pnl:+.0f}) "
f"期权={put_price:.0f}(盈亏{option_pnl:+.0f}) "
f"组合={total_pnl:+.0f}", end="")
| 策略 |
适用场景 |
最大盈利 |
最大亏损 |
| 买入看涨 |
强烈看多 |
无限 |
权利金 |
| 买入看跌 |
强烈看空 |
行权价-权利金 |
权利金 |
| 保护性看跌 |
持仓对冲 |
无限 |
有限 |
| 备兑看涨 |
增强收益 |
有限 |
较大 |
| 字母 |
含义 |
说明 |
| Delta(Δ) |
标的价格敏感度 |
标的涨1元,期权涨多少 |
| Gamma(Γ) |
Delta的变化率 |
Delta随标的价格的变化 |
| Theta(Θ) |
时间衰减 |
每过一天期权贬值多少 |
| Vega(ν) |
波动率敏感度 |
波动率变化1%,期权变多少 |
| 期权类型 |
Delta范围 |
说明 |
| 看涨期权 |
0到1 |
深度实值接近1 |
| 看跌期权 |
-1到0 |
深度实值接近-1 |
| 平值期权 |
约±0.5 |
涨跌各半 |
| 对比项 |
期权 |
期货 |
| 权利义务 |
买方有权利无义务 |
双方都有义务 |
| 保证金 |
买方只付权利金 |
双方都要保证金 |
| 最大亏损 |
买方最多亏权利金 |
理论上无限 |
| 盈利结构 |
非线性 |
线性 |
建议顺序:
- 买入看涨/看跌 - 最简单,风险有限
- 保护性策略 - 学习对冲
- 价差策略 - 控制成本
- 卖出策略 - 需要更多经验
| 建议 |
说明 |
| 从小仓位开始 |
先用1手学习 |
| 关注流动性 |
选择成交活跃的合约 |
| 注意时间价值 |
临近到期时间价值衰减加速 |
| 了解行权规则 |
不同交易所规则不同 |
| 做好风险管理 |
卖出期权需要特别注意 |
| 要点 |
内容 |
| 期权本质 |
权利的买卖 |
| 四种操作 |
买Call/卖Call/买Put/卖Put |
| 价值构成 |
内在价值 + 时间价值 |
| 入门策略 |
买入看涨/看跌最安全 |
| TqSdk支持 |
完整支持期权行情和交易 |
免责声明:本文仅供学习交流使用,不构成任何投资建议。期货和期权交易有风险,入市需谨慎。
更多资源: