3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在 QMT(迅投量化交易系统)中,ContextInfo.bsm_price 是基于 Black-Scholes-Merton (BSM) 模型用于计算欧式期权理论价格的核心 API 接口。本文将详细解析该函数的参数说明、返回值格式以及具体的代码实现示例。
ContextInfo.bsm_price(optionType, objectPrices, strikePrice, riskFree, sigma, days, dividend)
| 参数名 | 类型 | 说明 |
|---|---|---|
optionType |
str |
期权类型:'C' 表示认购期权(Call),'P' 表示认沽期权(Put) |
objectPrices |
float 或 list |
期权标的资产的价格,可以是单个浮点数或价格列表 |
strikePrice |
float |
期权的行权价格 |
riskFree |
float |
无风险收益率(如 3% 填 0.03) |
sigma |
float |
标的资产的年化波动率(如 23% 填 0.23) |
days |
int/float |
距离期权到期的剩余天数 |
dividend |
float |
标的资产的分红率(如 0) |
objectPrices 为单个 float 时,返回标的对应价格下的期权理论价格 (float)。objectPrices 为 list 时,返回对应价格序列的期权理论价格列表 (list)。nan。以下示例展示了如何在 QMT 策略的 handlebar 阶段使用 bsm_price 计算单个平值期权价格,以及绘制标的价格变动下的期权理论价格曲线:
# encoding:gbk
import numpy as np
def init(ContextInfo):
pass
def handlebar(ContextInfo):
# 仅在最后一根 Bar 运行打印,避免重复输出
if not ContextInfo.is_last_bar():
return
# 基础参数设置
option_type = 'C' # 认购期权
strike_price = 3.5 # 行权价 3.5 元
risk_free = 0.03 # 无风险利率 3%
sigma = 0.23 # 波动率 23%
days = 15 # 剩余天数 15 天
dividend = 0 # 分红率 0
# 示例 1:计算标的现价为 3.51 元时的平值期权理论价格
single_price = ContextInfo.bsm_price(option_type, 3.51, strike_price, risk_free, sigma, days, dividend)
print(f"当标的价格为 3.51 元时,期权理论价格为: {single_price}")
# 示例 2:计算标的价格从 3.0 元到 4.0 元变动过程中的期权理论价格序列
object_prices = list(np.arange(3.0, 4.0, 0.1))
prices_list = ContextInfo.bsm_price(option_type, object_prices, strike_price, risk_free, sigma, days, dividend)
print("标的价格与对应的期权理论价格:")
for spot, price in zip(object_prices, prices_list):
print(f"标的价: {spot:.2f} -> 期权理论价: {price}")
ContextInfo.get_option_iv() 获取实时隐含波动率,或使用 ContextInfo.get_risk_free_rate() 动态传入十年期国债收益率作为无风险利率。