"""
研究环境 (Research) 示例

本文件包含可以在 Jupyter Notebook 中运行的代码示例。
使用 `lean research` 启动研究环境后，可以逐步执行这些代码块。

安装依赖：
pip install pandas numpy matplotlib scipy
"""

# %% [markdown]
# # Lean 研究环境完整示例
#
# 本 Notebook 演示如何使用 QuantBook 进行策略研究和验证。

# %% 导入必要的库
from QuantConnect import *
from QuantConnect.Data import *
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime, timedelta

# 设置 matplotlib 显示中文（可选）
plt.rcParams['figure.figsize'] = (14, 7)
plt.rcParams['font.size'] = 10

print("Libraries imported successfully!")

# %% [markdown]
# ## 1. 创建 QuantBook 并添加证券

# %% 创建 QuantBook
qb = QuantBook()
print(f"QuantBook created at: {qb.Time}")

# %% 添加股票
spy = qb.AddEquity("SPY", Resolution.Daily)
aapl = qb.AddEquity("AAPL", Resolution.Daily)
msft = qb.AddEquity("MSFT", Resolution.Daily)

print(f"Added securities: SPY, AAPL, MSFT")

# %% [markdown]
# ## 2. 获取历史数据

# %% 获取单个证券的历史数据
print("Fetching historical data for SPY...")
spy_history = qb.History(spy, TimeSpan.FromDays(252), Resolution.Daily)
print(f"SPY History shape: {spy_history.shape}")
print(f"Date range: {spy_history.index[0]} to {spy_history.index[-1]}")
print(spy_history.head())

# %% 获取多个证券的历史数据
print("\nFetching historical data for multiple securities...")
symbols = [spy, aapl, msft]
multi_history = qb.History(symbols, TimeSpan.FromDays(252), Resolution.Daily)
print(f"Multi-security history shape: {multi_history.shape}")
print(multi_history.head())

# %% 获取指定时间段的数据
start_date = datetime(2023, 1, 1)
end_date = datetime(2023, 12, 31)

specific_history = qb.History(spy, start_date, end_date, Resolution.Daily)
print(f"\nData for 2023: {len(specific_history)} bars")

# %% [markdown]
# ## 3. 数据探索

# %% 提取收盘价
close_prices = multi_history['close'].unstack(level=0)
print("Close prices:")
print(close_prices.head())

# %% 基本统计
print("\n=== Price Statistics ===")
print(close_prices.describe())

# %% 收益率计算
returns = close_prices.pct_change().dropna()
print("\n=== Returns Statistics ===")
print(returns.describe())

# %% 年化收益率和波动率
annual_returns = returns.mean() * 252
annual_volatility = returns.std() * np.sqrt(252)

print("\n=== Annualized Metrics ===")
for symbol in close_prices.columns:
    print(f"{symbol.Value}:")
    print(f"  Annual Return: {annual_returns[symbol]:.2%}")
    print(f"  Annual Volatility: {annual_volatility[symbol]:.2%}")
    print(f"  Sharpe Ratio (assuming 0% rf): {annual_returns[symbol]/annual_volatility[symbol]:.2f}")

# %% [markdown]
# ## 4. 技术指标计算

# %% 使用 QuantBook 内置指标
print("Calculating technical indicators...")

# SMA
sma_20 = qb.SMA(spy, 20)
sma_50 = qb.SMA(spy, 50)
print(f"SMA20: {sma_20.Current.Value:.2f}")
print(f"SMA50: {sma_50.Current.Value:.2f}")

# EMA
ema_12 = qb.EMA(spy, 12)
ema_26 = qb.EMA(spy, 26)
print(f"\nEMA12: {ema_12.Current.Value:.2f}")
print(f"EMA26: {ema_26.Current.Value:.2f}")

# RSI
rsi_14 = qb.RSI(spy, 14)
print(f"\nRSI(14): {rsi_14.Current.Value:.2f}")

# MACD
macd = qb.MACD(spy, 12, 26, 9)
print(f"\nMACD:")
print(f"  MACD: {macd.Current.Value:.2f}")
print(f"  Signal: {macd.Signal.Current.Value:.2f}")
print(f"  Histogram: {macd.Histogram.Current.Value:.2f}")

# ATR
atr_14 = qb.ATR(spy, 14)
print(f"\nATR(14): {atr_14.Current.Value:.2f}")

# BB
bb_20_2 = qb.BB(spy, 20, 2)
print(f"\nBollinger Bands(20, 2):")
print(f"  Upper: {bb_20_2.UpperBand.Current.Value:.2f}")
print(f"  Middle: {bb_20_2.MiddleBand.Current.Value:.2f}")
print(f"  Lower: {bb_20_2.LowerBand.Current.Value:.2f}")
print(f"  BandWidth: {bb_20_2.BandWidth.Current.Value:.4f}")

# %% [markdown]
# ## 5. 手动计算技术指标

# %% 准备数据
spy_close = spy_history['close']

# %% 计算移动平均线
sma_20_manual = spy_close.rolling(20).mean()
sma_50_manual = spy_close.rolling(50).mean()

# %% 计算RSI
def calculate_rsi(prices, period=14):
    """手动计算 RSI"""
    delta = prices.diff()
    gain = (delta.where(delta > 0, 0)).rolling(period).mean()
    loss = (-delta.where(delta < 0, 0)).rolling(period).mean()
    rs = gain / loss
    rsi = 100 - (100 / (1 + rs))
    return rsi

rsi_manual = calculate_rsi(spy_close, 14)

print(f"Manual RSI: {rsi_manual.iloc[-1]:.2f}")
print(f"QuantBook RSI: {rsi_14.Current.Value:.2f}")

# %% [markdown]
# ## 6. 数据可视化

# %% 绘制价格和移动平均线
plt.figure(figsize=(14, 7))
plt.plot(spy_history.index, spy_history['close'], label='SPY Close', linewidth=2)
plt.plot(spy_history.index, sma_20_manual, label='SMA20', linewidth=1.5, alpha=0.7)
plt.plot(spy_history.index, sma_50_manual, 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=(14, 5))

# 收益率直方图（pct_change 后的列是各标的代码，不再是 'close'，
# 直接对整个 DataFrame 作图）
plt.subplot(1, 2, 1)
returns.hist(bins=50, alpha=0.7, edgecolor='black')
plt.title('Daily Returns Distribution')
plt.xlabel('Daily Return')
plt.ylabel('Frequency')
plt.axvline(returns.mean().mean(), color='r', linestyle='--', label='Mean')
plt.legend()

# %% QQ图
from scipy import stats
plt.subplot(1, 2, 2)
# SPY 的收益率列用 Symbol 对象索引（spy.Symbol），而非 'close'
stats.probplot(returns[spy.Symbol].dropna(), dist="norm", plot=plt)
plt.title('SPY Returns Q-Q Plot (Normality Check)')
plt.tight_layout()
plt.show()

# %% 绘制相关性热图
plt.figure(figsize=(10, 8))
correlation_matrix = returns.corr()
plt.imshow(correlation_matrix, cmap='coolwarm', aspect='auto', vmin=-1, vmax=1)
plt.colorbar(label='Correlation')
plt.xticks(range(len(correlation_matrix)), [s.Value for s in correlation_matrix.columns], rotation=45)
plt.yticks(range(len(correlation_matrix)), [s.Value for s in correlation_matrix.index])
plt.title('Correlation Matrix')

# 添加相关系数标注
for i in range(len(correlation_matrix)):
    for j in range(len(correlation_matrix)):
        text = plt.text(j, i, f'{correlation_matrix.iloc[i, j]:.2f}',
                       ha="center", va="center", color="black")
plt.tight_layout()
plt.show()

# %% [markdown]
# ## 7. 策略原型开发

# %% 创建策略数据框架
strategy_data = pd.DataFrame(index=spy_history.index)
strategy_data['close'] = spy_history['close']
strategy_data['sma_fast'] = spy_history['close'].rolling(20).mean()
strategy_data['sma_slow'] = spy_history['close'].rolling(50).mean()
strategy_data['rsi'] = rsi_manual

# %% 生成交易信号
strategy_data['signal'] = 0

# 买入条件：快线上穿慢线 且 RSI 不超买
buy_signals = (
    (strategy_data['sma_fast'] > strategy_data['sma_slow']) &
    (strategy_data['rsi'] < 70)
)

# 卖出条件：快线下穿慢线 或 RSI 超买
sell_signals = (
    (strategy_data['sma_fast'] < strategy_data['sma_slow']) |
    (strategy_data['rsi'] > 70)
)

strategy_data.loc[buy_signals, 'signal'] = 1
strategy_data.loc[sell_signals, 'signal'] = -1

print(f"Buy signals: {buy_signals.sum()}")
print(f"Sell signals: {sell_signals.sum()}")

# %% 计算策略收益
strategy_data['returns'] = strategy_data['close'].pct_change()
strategy_data['strategy_returns'] = strategy_data['returns'] * strategy_data['signal'].shift(1)

# 累计收益
strategy_data['cum_returns'] = (1 + strategy_data['strategy_returns']).cumprod()
strategy_data['buy_hold'] = (1 + strategy_data['returns']).cumprod()

# %% 可视化策略表现
plt.figure(figsize=(14, 7))
plt.plot(strategy_data.index, strategy_data['cum_returns'],
         label='Strategy Returns', linewidth=2)
plt.plot(strategy_data.index, strategy_data['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()

# %% [markdown]
# ## 8. 策略性能分析

# %% 计算性能指标
def calculate_performance_metrics(returns_series, name="Strategy"):
    """计算策略性能指标"""
    # 总收益
    total_return = returns_series.iloc[-1] - 1

    # 年化收益
    annual_return = returns_series.mean() * 252

    # 年化波动率
    annual_volatility = returns_series.std() * np.sqrt(252)

    # 夏普比率（假设无风险利率为0）
    sharpe_ratio = annual_return / annual_volatility if annual_volatility > 0 else 0

    # 最大回撤
    cum_returns = (1 + returns_series).cumprod()
    running_max = cum_returns.cummax()
    drawdown = (cum_returns - running_max) / running_max
    max_drawdown = drawdown.min()

    # 胜率
    win_rate = (returns_series > 0).sum() / (returns_series != 0).sum()

    # 盈亏比
    winning_returns = returns_series[returns_series > 0]
    losing_returns = returns_series[returns_series < 0]
    avg_win = winning_returns.mean() if len(winning_returns) > 0 else 0
    avg_loss = losing_returns.mean() if len(losing_returns) > 0 else 0
    profit_loss_ratio = abs(avg_win / avg_loss) if avg_loss != 0 else 0

    # Sortino比率
    downside_returns = returns_series[returns_series < 0]
    downside_deviation = downside_returns.std() * np.sqrt(252)
    sortino_ratio = annual_return / downside_deviation if downside_deviation > 0 else 0

    # Calmar比率（年化收益/最大回撤）
    calmar_ratio = annual_return / abs(max_drawdown) if max_drawdown != 0 else 0

    metrics = {
        'Name': name,
        'Total Return': f'{total_return:.2%}',
        'Annual Return': f'{annual_return:.2%}',
        'Annual Volatility': f'{annual_volatility:.2%}',
        'Sharpe Ratio': f'{sharpe_ratio:.2f}',
        'Sortino Ratio': f'{sortino_ratio:.2f}',
        'Max Drawdown': f'{max_drawdown:.2%}',
        'Win Rate': f'{win_rate:.2%}',
        'Profit/Loss Ratio': f'{profit_loss_ratio:.2f}',
        'Calmar Ratio': f'{calmar_ratio:.2f}'
    }

    return metrics

# %% 计算策略指标
strategy_metrics = calculate_performance_metrics(strategy_data['strategy_returns'], "MA Crossover Strategy")
buy_hold_metrics = calculate_performance_metrics(strategy_data['returns'], "Buy & Hold")

# %% 显示性能对比
print("\n" + "="*60)
print("PERFORMANCE METRICS COMPARISON")
print("="*60)
print(f"{'Metric':<25} {'Strategy':<20} {'Buy & Hold':<20}")
print("-"*60)

for key in strategy_metrics.keys():
    if key != 'Name':
        print(f"{key:<25} {strategy_metrics[key]:<20} {buy_hold_metrics[key]:<20}")

print("="*60)

# %% [markdown]
# ## 9. 参数优化分析

# %% 参数优化：双均线策略
def backtest_ma_strategy(fast_period, slow_period, data):
    """
    回测双均线策略

    Args:
        fast_period: 快线周期
        slow_period: 慢线周期
        data: 价格数据

    Returns:
        dict: 性能指标
    """
    df = pd.DataFrame(index=data.index)
    df['close'] = data['close']
    df['sma_fast'] = data['close'].rolling(fast_period).mean()
    df['sma_slow'] = data['close'].rolling(slow_period).mean()

    # 生成信号
    df['signal'] = 0
    df.loc[df['sma_fast'] > df['sma_slow'], 'signal'] = 1
    df.loc[df['sma_fast'] < df['sma_slow'], 'signal'] = -1

    # 计算收益
    df['returns'] = df['close'].pct_change()
    df['strategy_returns'] = df['returns'] * df['signal'].shift(1)

    # 计算性能指标
    total_return = (1 + df['strategy_returns']).prod() - 1
    sharpe_ratio = df['strategy_returns'].mean() / df['strategy_returns'].std() * np.sqrt(252) if df['strategy_returns'].std() > 0 else 0

    cum_returns = (1 + df['strategy_returns']).cumprod()
    running_max = cum_returns.cummax()
    drawdown = (cum_returns - running_max) / running_max
    max_drawdown = drawdown.min()

    return {
        'fast': fast_period,
        'slow': slow_period,
        'total_return': total_return,
        'sharpe': sharpe_ratio,
        'max_drawdown': max_drawdown
    }

# %% 测试不同参数组合
fast_periods = [5, 10, 15, 20, 25]
slow_periods = [30, 40, 50, 60, 70]

results = []

for fast in fast_periods:
    for slow in slow_periods:
        if fast < slow:
            result = backtest_ma_strategy(fast, slow, spy_history)
            results.append(result)

# %% 转换为DataFrame分析
results_df = pd.DataFrame(results)

# %% 找到最优参数
best_sharpe = results_df.loc[results_df['sharpe'].idxmax()]
best_return = results_df.loc[results_df['total_return'].idxmax()]

print("\n=== Best Parameters by Sharpe Ratio ===")
print(f"Fast Period: {best_sharpe['fast']}, Slow Period: {best_sharpe['slow']}")
print(f"Sharpe Ratio: {best_sharpe['sharpe']:.2f}")
print(f"Total Return: {best_sharpe['total_return']:.2%}")
print(f"Max Drawdown: {best_sharpe['max_drawdown']:.2%}")

print("\n=== Best Parameters by Total Return ===")
print(f"Fast Period: {best_return['fast']}, Slow Period: {best_return['slow']}")
print(f"Sharpe Ratio: {best_return['sharpe']:.2f}")
print(f"Total Return: {best_return['total_return']:.2%}")
print(f"Max Drawdown: {best_return['max_drawdown']:.2%}")

# %% 可视化参数优化结果
pivot_sharpe = results_df.pivot(index='fast', columns='slow', values='sharpe')
pivot_return = results_df.pivot(index='fast', columns='slow', values='total_return')

fig, axes = plt.subplots(1, 2, figsize=(16, 6))

# Sharpe Ratio 热图
im1 = axes[0].imshow(pivot_sharpe.values, cmap='RdYlGn', aspect='auto')
axes[0].set_xticks(range(len(pivot_sharpe.columns)))
axes[0].set_yticks(range(len(pivot_sharpe.index)))
axes[0].set_xticklabels(pivot_sharpe.columns)
axes[0].set_yticklabels(pivot_sharpe.index)
axes[0].set_xlabel('Slow Period')
axes[0].set_ylabel('Fast Period')
axes[0].set_title('Sharpe Ratio')
fig.colorbar(im1, ax=axes[0])

# Total Return 热图
im2 = axes[1].imshow(pivot_return.values, cmap='RdYlGn', aspect='auto')
axes[1].set_xticks(range(len(pivot_return.columns)))
axes[1].set_yticks(range(len(pivot_return.index)))
axes[1].set_xticklabels(pivot_return.columns)
axes[1].set_yticklabels(pivot_return.index)
axes[1].set_xlabel('Slow Period')
axes[1].set_ylabel('Fast Period')
axes[1].set_title('Total Return')
fig.colorbar(im2, ax=axes[1])

plt.tight_layout()
plt.show()

# %% [markdown]
# ## 10. 策略转为生产代码

# %% 经过研究验证，我们选择最优参数
# 这里选择 Sharpe Ratio 最高的参数组合
OPTIMAL_FAST = int(best_sharpe['fast'])
OPTIMAL_SLOW = int(best_sharpe['slow'])

print(f"\nOptimal parameters from research:")
print(f"Fast MA: {OPTIMAL_FAST}, Slow MA: {OPTIMAL_SLOW}")

# %% 生成生产策略代码
strategy_code = f'''
from AlgorithmImports import *

class OptimizedMACrossStrategy(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.fast_ma = self.SMA(self.spy, {OPTIMAL_FAST}, Resolution.Daily)
        self.slow_ma = self.SMA(self.spy, {OPTIMAL_SLOW}, Resolution.Daily)

        self.SetWarmUp(TimeSpan.FromDays({OPTIMAL_SLOW}))

        self.Debug(f"Strategy initialized with Fast={OPTIMAL_FAST}, Slow={OPTIMAL_SLOW}")

    def OnData(self, data):
        if not self.fast_ma.IsReady:
            return

        fast = self.fast_ma.Current.Value
        slow = self.slow_ma.Current.Value

        holdings = self.Portfolio[self.spy]

        # 金叉买入
        if fast > slow and not holdings.IsLong:
            self.SetHoldings(self.spy, 1)
            self.Debug(f"BUY at {{self.Securities[self.spy].Price:.2f}}")

        # 死叉卖出
        elif fast < slow and holdings.IsLong:
            self.Liquidate(self.spy)
            self.Debug(f"SELL at {{self.Securities[self.spy].Price:.2f}}")
'''

print("\n" + "="*60)
print("PRODUCTION STRATEGY CODE")
print("="*60)
print(strategy_code)
print("="*60)

print("\n=== Research Complete ===")
print("You can now copy the strategy code above into your Lean project.")
