功能概述
本代码实现了基于LSTM神经网络的指数期权价格预测与隐含波动率曲面建模系统。通过整合标的资产价格序列、宏观经济指标和市场情绪数据,构建多维特征输入矩阵,采用分位数回归损失函数优化模型输出,最终生成包含Delta、Gamma、Vega等希腊字母的风险参数矩阵。该系统适用于做市商报价、波动率套利及风险对冲场景,需注意模型过拟合、特征工程偏差及极端行情下的泛化能力风险。
核心组件架构
数据预处理模块
python
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
class OptionDataProcessor:
def __init__(self, lookback_window=60):
self.lookback = lookback_window
self.scalers = {}
def create_vol_surface(self, df):
"""构建波动率曲面特征"""
moneyness = df['strike'] / df['underlying_price']
df['moneyness'] = np.log(moneyness)
df['time_to_maturity'] = (df['maturity'] - df['date']).dt.days / 365
return df.pivot(index='moneyness', columns='time_to_maturity', values='implied_vol')
def sequence_generator(self, X, y, sequence_length):
"""生成时序训练样本"""
X_seq, y_seq = [], []
for i in range(len(X)-sequence_length+1):
X_seq.append(X[i:(i+sequence_length)])
y_seq.append(y[i+sequence_length-1])
return np.array(X_seq), np.array(y_seq)
波动率建模单元
python
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout, Lambda
class VolatilityModel:
def __init__(self, input_shape):
self.model = self._build_architecture(input_shape)
def _build_architecture(self, input_shape):
"""构建LSTM-Attention混合架构"""
model = Sequential([
LSTM(128, return_sequences=True, input_shape=input_shape),
Dropout(0.2),
LSTM(64, return_sequences=True),
Dropout(0.2),
Lambda(lambda x: tf.reduce_mean(x, axis=1)),
Dense(64, activation='relu'),
Dense(32, activation='relu'),
Dense(1)
])
model.compile(optimizer='adam', loss='mse')
return model
def train_with_quantile_loss(self, X_train, y_train, quantile=0.5):
"""分位数回归训练"""
def quantile_loss(y_true, y_pred):
return tf.keras.metrics.quantile_loss(y_true, y_pred, quantile)
self.model.compile(optimizer='adam', loss=quantile_loss)
策略执行引擎
python
class OptionsTradingStrategy:
def __init__(self, vol_model, risk_limits):
self.vol_model = vol_model
self.risk_limits = risk_limits
self.position_book = {}
def calculate_greeks(self, option_chain, forecast_vol):
"""计算风险参数"""
df = option_chain.copy()
df['theoretical_price'] = self.black_scholes_price(
df['underlying_price'], df['strike'], df['maturity'],
risk_free_rate=0.05, volatility=forecast_vol
)
# 数值方法计算希腊字母
df['delta'] = self._compute_delta(df)
df['gamma'] = self._compute_gamma(df)
return df[['delta', 'gamma', 'vega']]
def black_scholes_price(self, S, K, T, r, sigma):
"""改进版BS定价公式"""
d1 = (np.log(S/K) + (r + 0.5*sigma**2)*T) / (sigma*np.sqrt(T))
d2 = d1 - sigma*np.sqrt(T)
return S * norm.cdf(d1) - K * np.exp(-r*T) * norm.cdf(d2)
关键技术实现
多维度特征工程
在波动率建模中,除传统量价指标外,引入以下创新特征:
- 期限结构斜率:不同到期日合约的波动率差值
- 偏度因子:OTM看涨/看跌期权隐含波动率比率
- 微观结构噪声:买卖价差与已实现波动率比值
- 宏观敏感度:国债收益率曲线形态变化指标
python
def generate_features(market_data):
features = pd.DataFrame(index=market_data.index)
features['realized_vol_20d'] = market_data['close'].pct_change().rolling(20).std()
features['skew_factor'] = (market_data['bid_ask_spread_call'] /
market_data['bid_ask_spread_put'])
features['term_structure'] = (market_data['vol_30d'] - market_data['vol_90d'])
features['macro_sensitivity'] = market_data['yield_curve_slope'] * 0.75
return features.dropna()
混合损失函数设计
为解决波动率预测中的不对称误差问题,采用复合损失函数:
- Huber损失:处理异常值鲁棒性
- Quantile损失:捕捉波动率分布尾部特性
- MSDE损失:保证预测路径平滑性
python
def composite_loss(y_true, y_pred, alpha=0.3, beta=0.4, gamma=0.3):
"""三重加权损失函数"""
huber = tf.keras.losses.Huber(delta=1.0)
quantile = tf.keras.losses.QuantileLoss(quantile=0.5)
msde = tf.keras.losses.MeanSquaredError()
return (alpha * huber(y_true, y_pred) +
beta * quantile(y_true, y_pred) +
gamma * msde(y_true, y_pred))
动态头寸管理算法
根据实时风险暴露调整持仓规模,关键约束条件包括:
- 单腿Delta敞口不超过账户净值的5%
- Gamma暴露峰值控制在2%以内
- Vega敏感性维持在±15%区间
python
def dynamic_position_adjustment(self, current_position, risk_metrics):
"""动态调仓逻辑"""
max_delta_exposure = self.risk_limits['max_delta'] * self.account_value
max_gamma_peak = self.risk_limits['max_gamma'] * self.account_value
# 计算目标持仓比例
target_ratio = min(
current_position.delta / max_delta_exposure,
current_position.gamma_peak / max_gamma_peak
)
# 执行阶梯式调仓
adjustment_steps = np.linspace(0, 1, 5)
for step in adjustment_steps:
new_position = current_position * (1 - step) + target_ratio * step
self.execute_order(new_position - current_position)
实证分析框架
回测系统设计
采用事件驱动型回测架构,关键组件包括:
- Tick级数据模拟器:生成包含微结构噪声的市场数据流
- 订单簿重建引擎:模拟真实交易对盘口的影响
- 滑点成本模型:基于VOLATERAL算法估算实际成交价格
python
class BacktestEngine:
def __init__(self, initial_capital=1_000_000):
self.cash = initial_capital
self.positions = {}
self.trade_log = pd.DataFrame(columns=[
'timestamp', 'symbol', 'direction', 'qty', 'price', 'fee'
])
def run_monte_carlo(self, n_simulations=1000):
"""蒙特卡洛路径模拟"""
paths = []
for _ in range(n_simulations):
path = self._generate_price_path()
self._execute_trading_logic(path)
paths.append(self.calculate_pnl())
return pd.Series(paths)
def _generate_price_path(self):
"""几何布朗运动路径生成"""
mu = 0.05 / 252
sigma = 0.2 / np.sqrt(252)
return np.exp(np.cumsum(np.random.normal(mu, sigma, size=252)))