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

如何使用 QMT 的 ContextInfo.bsm_price 函数计算欧式期权理论价格?

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

问题描述

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

Title: QMT 期权理论定价:`bsm_price` 接口计算欧式期权价格

Question: 如何使用 ContextInfo.bsm_price 函数计算期权的理论价格?

解决方案

如何使用 QMT 的 ContextInfo.bsm_price 函数计算期权理论价格

在 QMT(迅投量化交易系统)中,ContextInfo.bsm_price 是基于 Black-Scholes-Merton (BSM) 模型用于计算欧式期权理论价格的核心 API 接口。本文将详细解析该函数的参数说明、返回值格式以及具体的代码实现示例。


1. 接口函数说明

用法

ContextInfo.bsm_price(optionType, objectPrices, strikePrice, riskFree, sigma, days, dividend)

参数详解

参数名 类型 说明
optionType str 期权类型:'C' 表示认购期权(Call),'P' 表示认沽期权(Put)
objectPrices floatlist 期权标的资产的价格,可以是单个浮点数或价格列表
strikePrice float 期权的行权价格
riskFree float 无风险收益率(如 3% 填 0.03
sigma float 标的资产的年化波动率(如 23% 填 0.23
days int/float 距离期权到期的剩余天数
dividend float 标的资产的分红率(如 0

返回值

  • objectPrices 为单个 float 时,返回标的对应价格下的期权理论价格 (float)。
  • objectPriceslist 时,返回对应价格序列的期权理论价格列表 (list)。
  • 计算结果保留 4 位小数(最小值 0.0001),输入非法参数时返回 nan

2. 代码示例

以下示例展示了如何在 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}")

3. 注意事项

  1. 适用于欧式期权:BSM 模型主要针对欧式期权(如 50ETF 期权、300ETF 期权等)进行定价,美式期权由于存在提前行权特征,定价可能存在微小偏差。
  2. 配合实时波动率:在实盘套利或做市策略中,建议配合 ContextInfo.get_option_iv() 获取实时隐含波动率,或使用 ContextInfo.get_risk_free_rate() 动态传入十年期国债收益率作为无风险利率。