第10章 研究环境 (Research)¶
本章介绍 Lean 的研究环境,包括 QuantBook、Jupyter Notebook 和本地研究工作流。
10.1 研究环境概述¶
10.1.1 什么是研究环境?¶
研究环境是一个交互式的 Jupyter Notebook 环境,用于: - 数据探索 - 策略原型开发 - 指标验证 - 可视化分析
10.1.2 启动研究环境¶
# 打开现有项目(项目名是必需的位置参数)
lean research "MyProject"
# 默认会自动在浏览器打开 Jupyter Lab;如不想自动打开加 --no-open
lean research "MyProject" --no-open
10.2 QuantBook 基础¶
10.2.1 创建 QuantBook¶
from QuantConnect import *
from QuantConnect.Data import *
# 创建 QuantBook
qb = QuantBook()
print(f"QuantBook created at: {qb.Time}")
10.2.2 添加证券¶
# 添加股票
spy = qb.AddEquity("SPY", Resolution.Hour)
aapl = qb.AddEquity("AAPL", Resolution.Daily)
# 添加外汇
eurusd = qb.AddForex("EURUSD", Resolution.Minute)
# 添加加密货币
btc = qb.AddCrypto("BTCUSD", Resolution.Daily)
# 添加期货
es = qb.AddFuture("ES", Resolution.Minute)
# 添加期权
spy_option = qb.AddOption("SPY", Resolution.Minute)
print(f"Added securities: {spy}, {aapl}, {eurusd}")
10.3 历史数据¶
10.3.1 获取历史数据¶
# 获取单个证券的历史数据
history = qb.History(spy, TimeSpan.FromDays(30), Resolution.Daily)
print(f"History shape: {history.shape}")
print(history.head())
10.3.2 多证券历史数据¶
# 获取多个证券的历史数据
symbols = [spy, aapl]
history = qb.History(symbols, TimeSpan.FromDays(30), Resolution.Daily)
# 查看数据
print(history.head())
10.3.3 指定时间范围¶
from datetime import datetime
# 指定起止时间
start = datetime(2023, 1, 1)
end = datetime(2023, 12, 31)
history = qb.History(spy, start, end, Resolution.Daily)
10.4 指标计算¶
10.4.1 内置指标¶
# SMA
sma_20 = qb.SMA(spy, 20)
sma_50 = qb.SMA(spy, 50)
# EMA
ema_12 = qb.EMA(spy, 12)
# RSI
rsi = qb.RSI(spy, 14)
# MACD
macd = qb.MACD(spy, 12, 26, 9)
# 布林带
bb = qb.BB(spy, 20, 2)
# ATR
atr = qb.ATR(spy, 14)
10.4.2 手动计算指标¶
import pandas as pd
# 获取历史数据
history = qb.History(spy, TimeSpan.FromDays(100), Resolution.Daily)
# 计算收盘价的 SMA
closes = history['close']
sma_20_manual = closes.rolling(window=20).mean()
# 计算标准差
std_20 = closes.rolling(window=20).std()
# 计算布林带
upper_band = sma_20_manual + 2 * std_20
lower_band = sma_20_manual - 2 * std_20
# 显示结果
print(f"SMA20: {sma_20_manual.iloc[-1]:.2f}")
print(f"Upper BB: {upper_band.iloc[-1]:.2f}")
print(f"Lower BB: {lower_band.iloc[-1]:.2f}")
10.5 数据分析示例¶
10.5.1 收益率分析¶
# 获取历史数据
history = qb.History(spy, TimeSpan.FromDays(252), Resolution.Daily)
# 计算日收益率
history['returns'] = history['close'].pct_change()
# 统计分析
print(f"Mean daily return: {history['returns'].mean():.4f}")
print(f"Std daily return: {history['returns'].std():.4f}")
print(f"Sharpe ratio: {history['returns'].mean() / history['returns'].std() * (252**0.5):.2f}")
# 累计收益
history['cumulative_returns'] = (1 + history['returns']).cumprod()
print(f"Total return: {history['cumulative_returns'].iloc[-1] - 1:.2%}")
10.5.2 回撤分析¶
# 计算回撤
history['running_max'] = history['close'].cummax()
history['drawdown'] = (history['close'] - history['running_max']) / history['running_max']
# 最大回撤
max_drawdown = history['drawdown'].min()
print(f"Max drawdown: {max_drawdown:.2%}")
# 回撤持续时间
drawdown_periods = history[history['drawdown'] == 0].index
print(f"Number of drawdown periods: {len(drawdown_periods)}")
10.5.3 相关性分析¶
# 获取多个证券的历史数据
symbols = [spy, aapl]
history = qb.History(symbols, TimeSpan.FromDays(252), Resolution.Daily)
# 计算收益率
returns = history['close'].unstack(level=0).pct_change()
# 相关系数矩阵
correlation = returns.corr()
print(correlation)
10.6 策略原型开发¶
10.6.1 均值回归策略原型¶
# 获取数据
history = qb.History(spy, TimeSpan.FromDays(252), Resolution.Daily)
# 计算指标
history['sma_20'] = history['close'].rolling(20).mean()
history['sma_50'] = history['close'].rolling(50).mean()
# 注意:qb.RSI(spy, 14) 返回的是指标对象(最新值取 .Current.Value),
# 不是可逐 bar 索引的序列;要得到 RSI 列,用 pandas 按 Wilder 平滑自行计算
delta = history['close'].diff()
gain = delta.clip(lower=0).ewm(alpha=1/14, adjust=False).mean()
loss = (-delta.clip(upper=0)).ewm(alpha=1/14, adjust=False).mean()
history['rsi'] = 100 - 100 / (1 + gain / loss)
# 生成信号
history['signal'] = 0
# 买入信号:价格低于 SMA20 且 RSI < 30
history.loc[
(history['close'] < history['sma_20']) &
(history['rsi'] < 30),
'signal'
] = 1
# 卖出信号:价格高于 SMA20 且 RSI > 70
history.loc[
(history['close'] > history['sma_20']) &
(history['rsi'] > 70),
'signal'
] = -1
# 计算策略收益
history['strategy_returns'] = history['close'].pct_change() * history['signal'].shift(1)
# 累计收益
history['strategy_cum_returns'] = (1 + history['strategy_returns']).cumprod()
# 对比买入持有
history['buy_hold_returns'] = (1 + history['close'].pct_change()).cumprod()
# 显示结果
import matplotlib.pyplot as plt
plt.figure(figsize=(12, 6))
plt.plot(history.index, history['strategy_cum_returns'], label='Strategy')
plt.plot(history.index, history['buy_hold_returns'], label='Buy & Hold')
plt.legend()
plt.title('Strategy vs Buy & Hold')
plt.show()
10.6.2 动量策略原型¶
# 获取数据
history = qb.History(spy, TimeSpan.FromDays(252), Resolution.Daily)
# 计算动量(过去3个月收益率)
history['momentum_3m'] = history['close'].pct_change(63)
# 计算波动率
history['volatility_20'] = history['close'].pct_change().rolling(20).std()
# 生成信号
# 动量 > 0 且波动率 < 0.02 时买入
history['signal'] = 0
history.loc[
(history['momentum_3m'] > 0) &
(history['volatility_20'] < 0.02),
'signal'
] = 1
# 计算收益
history['strategy_returns'] = history['close'].pct_change() * history['signal'].shift(1)
# 累计收益
history['cum_returns'] = (1 + history['strategy_returns']).cumprod()
print(f"Strategy Return: {history['cum_returns'].iloc[-1] - 1:.2%}")
10.7 完整研究 Notebook 示例¶
# %%
# Lean 研究环境示例
# 这是一个完整的研究工作流示例
# %% 导入库
from QuantConnect import *
from QuantConnect.Data import *
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# %% 创建 QuantBook
qb = QuantBook()
# %% 添加证券
spy = qb.AddEquity("SPY", Resolution.Daily)
aapl = qb.AddEquity("AAPL", Resolution.Daily)
print(f"Added {spy} and {aapl}")
# %% 获取历史数据
print("Fetching historical data...")
history = qb.History([spy, aapl], TimeSpan.FromDays(252), Resolution.Daily)
print(f"Data shape: {history.shape}")
print(history.head())
# %% 数据预处理
# 重置索引并处理
prices = history['close'].unstack(level=0)
returns = prices.pct_change().dropna()
print(f"\nReturns statistics:")
print(returns.describe())
# %% 计算技术指标
print("\nCalculating indicators...")
# SMA
sma_20 = prices[spy].rolling(20).mean()
sma_50 = prices[spy].rolling(50).mean()
# RSI 使用 QuantBook
rsi_spy = qb.RSI(spy, 14)
rsi_aapl = qb.RSI(aapl, 14)
# %% 可视化价格和均线
plt.figure(figsize=(14, 7))
plt.plot(prices.index, prices[spy], label='SPY', linewidth=2)
plt.plot(sma_20.index, sma_20, label='SMA20', linewidth=1.5, alpha=0.7)
plt.plot(sma_50.index, sma_50, label='SMA50', linewidth=1.5, alpha=0.7)
plt.title('SPY Price with Moving Averages')
plt.xlabel('Date')
plt.ylabel('Price ($)')
plt.legend()
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
# %% 收益率分布
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
returns[spy].hist(bins=50, alpha=0.7, label='SPY')
returns[aapl].hist(bins=50, alpha=0.7, label='AAPL')
plt.title('Return Distribution')
plt.xlabel('Daily Return')
plt.ylabel('Frequency')
plt.legend()
# %% QQ 图
from scipy import stats
plt.subplot(1, 2, 2)
stats.probplot(returns[spy].dropna(), dist="norm", plot=plt)
plt.title('SPY Returns Q-Q Plot')
plt.tight_layout()
plt.show()
# %% 滚动夏普比率
window = 60
rolling_sharpe = (
returns[spy].rolling(window).mean() /
returns[spy].rolling(window).std() *
np.sqrt(252)
)
plt.figure(figsize=(14, 5))
plt.plot(rolling_sharpe.index, rolling_sharpe, label=f'{window}-Day Rolling Sharpe')
plt.axhline(y=1, color='r', linestyle='--', label='Sharpe = 1')
plt.title(f'Rolling Sharpe Ratio ({window} days)')
plt.xlabel('Date')
plt.ylabel('Sharpe Ratio')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
# %% 相关性分析
correlation_matrix = returns.corr()
print("\nCorrelation Matrix:")
print(correlation_matrix)
# %% 滚动相关性
rolling_corr = returns[spy].rolling(60).corr(returns[aapl])
plt.figure(figsize=(14, 5))
plt.plot(rolling_corr.index, rolling_corr)
plt.title('60-Day Rolling Correlation: SPY vs AAPL')
plt.xlabel('Date')
plt.ylabel('Correlation')
plt.axhline(y=0, color='k', linestyle='-', alpha=0.3)
plt.grid(True, alpha=0.3)
plt.show()
# %% 简单策略回测
# 双均线交叉策略
signals = pd.DataFrame(index=prices.index)
signals['price'] = prices[spy]
signals['sma_fast'] = prices[spy].rolling(20).mean()
signals['sma_slow'] = prices[spy].rolling(50).mean()
# 生成信号
signals['signal'] = 0
signals.loc[signals['sma_fast'] > signals['sma_slow'], 'signal'] = 1
signals.loc[signals['sma_fast'] < signals['sma_slow'], 'signal'] = -1
# 计算收益
signals['returns'] = signals['price'].pct_change()
signals['strategy_returns'] = signals['returns'] * signals['signal'].shift(1)
# 累计收益
signals['cum_returns'] = (1 + signals['strategy_returns']).cumprod()
signals['buy_hold'] = (1 + signals['returns']).cumprod()
# %% 策略表现
final_strategy_return = signals['cum_returns'].iloc[-1] - 1
final_buy_hold_return = signals['buy_hold'].iloc[-1] - 1
print(f"\n=== Strategy Performance ===")
print(f"Strategy Return: {final_strategy_return:.2%}")
print(f"Buy & Hold Return: {final_buy_hold_return:.2%}")
print(f"Excess Return: {final_strategy_return - final_buy_hold_return:.2%}")
# %% 可视化策略表现
plt.figure(figsize=(14, 7))
plt.plot(signals.index, signals['cum_returns'], label='MA Crossover Strategy', linewidth=2)
plt.plot(signals.index, signals['buy_hold'], label='Buy & Hold', linewidth=2)
plt.title('Strategy vs Buy & Hold')
plt.xlabel('Date')
plt.ylabel('Cumulative Returns')
plt.legend()
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
# %% 风险指标
# 最大回撤
running_max = signals['cum_returns'].cummax()
drawdown = (signals['cum_returns'] - running_max) / running_max
max_drawdown = drawdown.min()
# 夏普比率
mean_return = signals['strategy_returns'].mean()
std_return = signals['strategy_returns'].std()
sharpe_ratio = mean_return / std_return * np.sqrt(252) if std_return > 0 else 0
# 胜率
win_rate = (signals['strategy_returns'] > 0).sum() / (signals['strategy_returns'] != 0).sum()
print(f"\n=== Risk Metrics ===")
print(f"Max Drawdown: {max_drawdown:.2%}")
print(f"Sharpe Ratio: {sharpe_ratio:.2f}")
print(f"Win Rate: {win_rate:.2%}")
print("\n=== Research Complete ===")
10.8 研究到生产的转换¶
10.8.1 将研究代码转为策略¶
# 在研究环境(Research)中
qb = QuantBook()
spy = qb.AddEquity("SPY", Resolution.Daily)
history = qb.History(spy, TimeSpan.FromDays(252), Resolution.Daily)
# 分析代码...
# 验证策略逻辑...
# 转换为生产策略(Algorithm)
from AlgorithmImports import *
class MyStrategy(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 1, 1)
self.SetEndDate(2023, 12, 31)
self.SetCash(100000)
self.spy = self.AddEquity("SPY", Resolution.Daily).Symbol
# 使用研究中验证的指标参数
self.sma_fast = self.SMA(self.spy, 20)
self.sma_slow = self.SMA(self.spy, 50)
self.SetWarmUp(TimeSpan.FromDays(60))
def OnData(self, data):
if not self.sma_fast.IsReady:
return
if self.sma_fast.Current.Value > self.sma_slow.Current.Value:
if not self.Portfolio[self.spy].IsLong:
self.SetHoldings(self.spy, 1)
else:
if self.Portfolio[self.spy].IsLong:
self.Liquidate(self.spy)
10.8.2 最佳实践¶
- 研究阶段
- 使用 QuantBook 探索数据
- 验证策略假设
-
分析历史表现
-
开发阶段
- 将研究代码转为 QCAlgorithm
- 添加风险管理
-
完善日志和错误处理
-
测试阶段
- 进行样本外测试
- 参数敏感性分析
- 压力测试