"""
数据与历史查询完整示例

本示例演示了 Lean 中的各种数据访问方式：
1. 历史数据请求 (History API)
2. 实时数据访问 (OnData, Securities)
3. Coarse Universe 选择
4. Fine Universe 选择
5. 期权链处理
6. 自定义数据
"""

from AlgorithmImports import *
from datetime import timedelta, datetime

# ========== 自定义数据类型 ==========

class SentimentData(PythonData):
    """
    自定义情绪数据示例

    假设我们有一个包含市场情绪指标的 CSV 文件
    格式：date,sentiment_value
    """

    def GetSource(self, config, date, isLiveMode):
        """定义数据源"""
        return SubscriptionDataSource(
            "data/sentiment.csv",
            SubscriptionTransportMedium.LocalFile,
            FileFormat.Csv
        )

    def Reader(self, config, line, date, isLiveMode):
        """解析 CSV 行"""
        if not line.strip():
            return None

        # 跳过标题
        if line.startswith("date"):
            return None

        parts = line.split(',')
        if len(parts) < 2:
            return None

        sentiment = SentimentData()
        sentiment.Symbol = config.Symbol
        # 注意：Python 没有 datetime.Parse，解析字符串日期要用 datetime.strptime
        # （CSV 中 date 列形如 2023-01-31）
        sentiment.Time = datetime.strptime(parts[0], "%Y-%m-%d")
        sentiment.Value = float(parts[1])
        sentiment.EndTime = sentiment.Time + timedelta(days=1)

        return sentiment


# ========== 主策略类 ==========

class DataAndHistoryDemoAlgorithm(QCAlgorithm):
    """
    数据访问演示策略

    演示各种数据获取和处理方式
    """

    def Initialize(self):
        """初始化算法"""
        # ========== 基本设置 ==========
        self.SetStartDate(2020, 1, 1)
        self.SetEndDate(2023, 6, 30)
        self.SetCash(100000)

        # 记录初始资金
        # 注意：Portfolio 没有 StartingCash 属性，需要在 Initialize 里自行保存
        self.starting_cash = self.Portfolio.TotalPortfolioValue

        # ========== 添加证券 ==========
        self.spy = self.AddEquity("SPY", Resolution.Daily).Symbol
        self.benchmark = self.AddEquity("SPY", Resolution.Daily).Symbol

        # ========== 添加多个证券用于 Universe 演示 ==========
        self.AddEquity("AAPL", Resolution.Daily)
        self.AddEquity("MSFT", Resolution.Daily)
        self.AddEquity("GOOGL", Resolution.Daily)
        self.AddEquity("AMZN", Resolution.Daily)

        # ========== 设置 Universe 选择 ==========
        # 惯用写法：self.AddUniverse(粗筛选函数, 精筛选函数)
        # （Lean 中没有 FineFundamentalUniverse(...) 这种可实例化的筛选入口）
        self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)

        # ========== 添加自定义数据 ==========
        # 注意：需要实际的数据文件才能工作
        # self.AddData(SentimentData, "SENTIMENT", Resolution.Daily)

        # ========== 设置定时任务 ==========
        self.Schedule.On(
            self.DateRules.Every(DayOfWeek.Monday),
            self.TimeRules.AfterMarketOpen(self.spy, 30),
            self.WeeklyAnalysis
        )

        # ========== 自定义变量 ==========
        self.selected_symbols = []     # Universe 选中的股票
        self.last_analysis_time = None
        self.analysis_count = 0

        # ========== 指标 ==========
        self.sma_50 = self.SMA(self.spy, 50, Resolution.Daily)

        self.Debug("=" * 60)
        self.Debug("Data and History Demo - Initialized")
        self.Debug("=" * 60)


    def OnData(self, data):
        """
        主数据处理函数

        演示实时数据访问
        """
        # ========== 1. 通过 Securities 访问数据 ==========
        if self.Securities.ContainsKey(self.spy):
            spy_security = self.Securities[self.spy]
            current_price = spy_security.Price
            current_volume = spy_security.Volume

            # ========== 2. 买卖价信息 ==========
            bid_price = spy_security.BidPrice
            ask_price = spy_security.AskPrice
            bid_size = spy_security.BidSize
            ask_size = spy_security.AskSize

            # ========== 3. OHLCV 数据 ==========
            if data.ContainsKey(self.spy):
                bar = data[self.spy]
                open_price = bar.Open
                high_price = bar.High
                low_price = bar.Low
                close_price = bar.Close
                volume = bar.Volume


    def WeeklyAnalysis(self):
        """
        每周分析任务

        演示历史数据的使用
        """
        self.analysis_count += 1
        self.Debug(f"\n{'='*60}")
        self.Debug(f"[WEEKLY ANALYSIS #{self.analysis_count}] {self.Time.date()}")
        self.Debug(f"{'='*60}")

        # ========== 1. 获取 SPY 历史数据 ==========
        self.DemonstrateHistoryBasic()

        # ========== 2. 多证券历史数据 ==========
        self.DemonstrateHistoryMultiple()

        # ========== 3. 历史数据统计分析 ==========
        self.DemonstrateHistoryStatistics()

        # ========== 4. 使用历史数据计算指标 ==========
        self.DemonstrateManualIndicator()


    def DemonstrateHistoryBasic(self):
        """演示基本历史数据请求"""
        self.Debug("\n--- Basic History Request ---")

        # 获取 SPY 过去 30 天的日线数据
        history = self.History(self.spy, 30, Resolution.Daily)

        if history.empty:
            self.Debug("No history data available")
            return

        self.Debug(f"Retrieved {len(history)} bars")

        # 获取最新和最旧的收盘价
        latest_close = history.iloc[-1]['close']
        oldest_close = history.iloc[0]['close']

        self.Debug(f"Oldest close: ${oldest_close:.2f}")
        self.Debug(f"Latest close: ${latest_close:.2f}")
        self.Debug(f"30-day return: {(latest_close / oldest_close - 1) * 100:.2f}%")


    def DemonstrateHistoryMultiple(self):
        """演示多证券历史数据请求"""
        self.Debug("\n--- Multiple Symbols History ---")

        symbols_to_check = ["SPY", "AAPL", "MSFT"]

        for symbol_str in symbols_to_check:
            symbol = self.Symbol(symbol_str)

            if self.Securities.ContainsKey(symbol):
                # 获取 30 天历史
                hist = self.History(symbol, 30, Resolution.Daily)

                if not hist.empty:
                    # 计算统计
                    closes = hist['close']
                    avg_price = closes.mean()
                    max_price = closes.max()
                    min_price = closes.min()
                    volatility = closes.pct_change().std() * 100

                    self.Debug(f"{symbol_str}:")
                    self.Debug(f"  Avg: ${avg_price:.2f}")
                    self.Debug(f"  Range: ${min_price:.2f} - ${max_price:.2f}")
                    self.Debug(f"  Volatility: {volatility:.2f}%")


    def DemonstrateHistoryStatistics(self):
        """演示历史数据统计分析"""
        self.Debug("\n--- History Statistics ---")

        # 获取 90 天历史数据
        history = self.History(self.spy, 90, Resolution.Daily)

        if history.empty:
            return

        closes = history['close']

        # 基本统计
        self.Debug(f"Statistics for {len(history)} days:")
        self.Debug(f"  Mean: ${closes.mean():.2f}")
        self.Debug(f"  Median: ${closes.median():.2f}")
        self.Debug(f"  Std Dev: ${closes.std():.2f}")
        self.Debug(f"  Min: ${closes.min():.2f}")
        self.Debug(f"  Max: ${closes.max():.2f}")

        # 收益率统计
        returns = closes.pct_change().dropna()
        self.Debug(f"\nReturns Statistics:")
        self.Debug(f"  Daily Avg Return: {returns.mean() * 100:.3f}%")
        self.Debug(f"  Daily Std Dev: {returns.std() * 100:.3f}%")
        self.Debug(f"  Total Return: {(closes.iloc[-1] / closes.iloc[0] - 1) * 100:.2f}%")

        # 最大回撤
        rolling_max = closes.cummax()
        drawdown = (closes - rolling_max) / rolling_max
        max_drawdown = drawdown.min() * 100
        self.Debug(f"  Max Drawdown: {max_drawdown:.2f}%")


    def DemonstrateManualIndicator(self):
        """演示使用历史数据手动计算指标"""
        self.Debug("\n--- Manual Indicator Calculation ---")

        # 获取历史数据
        history = self.History(self.spy, 50, Resolution.Daily)

        if history.empty or len(history) < 50:
            return

        closes = history['close']

        # 手动计算 SMA(20)
        sma_20_manual = closes.tail(20).mean()
        sma_20_builtin = self.sma_50.Current.Value

        self.Debug(f"Manual SMA(20): ${sma_20_manual:.2f}")
        self.Debug(f"Built-in SMA(50): ${sma_20_builtin:.2f}")

        # 手动计算 RSI(14)
        delta = closes.diff()
        gain = (delta.where(delta > 0, 0)).tail(14).mean()
        loss = (-delta.where(delta < 0, 0)).tail(14).mean()

        if loss != 0:
            rs = gain / loss
            rsi_14 = 100 - (100 / (1 + rs))
            self.Debug(f"Manual RSI(14): {rsi_14:.2f}")


    # ========== Universe 选择 ==========

    def CoarseSelectionFunction(self, coarse):
        """
        粗筛选函数

        使用 CoarseFundamental 数据进行初步筛选
        """
        # 筛选条件：
        # 1. 有基本面数据
        # 2. 价格 > 5 美元
        # 3. 成交量 > 100 万
        # 4. 是普通股

        filtered = []

        for c in coarse:
            if not c.HasFundamentalData:
                continue

            if c.Price < 5:
                continue

            if c.Volume < 1000000:
                continue

            if c.SecurityType != SecurityType.Equity:
                continue

            filtered.append(c)

        # 按美元交易量排序，取前 100
        sorted_by_dollar_volume = sorted(
            filtered,
            key=lambda c: c.DollarVolume,
            reverse=True
        )

        selected = [c.Symbol for c in sorted_by_dollar_volume[:100]]

        self.Debug(f"[Coarse Selection] Filtered {len(selected)} stocks from {len(coarse)} total")

        return selected


    def FineSelectionFunction(self, fine):
        """
        精筛选函数

        使用 FineFundamental 数据进行深度分析
        """
        # 筛选条件：
        # 1. 市盈率在合理范围 (5 - 30)
        # 2. ROE > 0
        # 3. 市值 > 10 亿

        filtered = []

        for f in fine:
            # 检查市盈率
            pe_ratio = f.ValuationRatios.PERatio
            if pe_ratio <= 0 or pe_ratio > 30:
                continue

            # 检查 ROE
            roe = f.OperationRatios.ROE.Value
            if roe <= 0:
                continue

            # 检查市值
            market_cap = f.EarningReports.BasicAverageShares.Value * f.ValuationRatios.PERatio * f.EarningReports.BasicEPS.Value
            if market_cap < 1e9:  # 小于 10 亿
                continue

            filtered.append(f)

        # 按多个因子综合排序
        # 因子：ROE（越高越好），PE（越低越好）
        sorted_by_score = sorted(
            filtered,
            key=lambda f: (
                f.OperationRatios.ROE.Value / f.ValuationRatios.PERatio
            ),
            reverse=True
        )

        self.selected_symbols = [f.Symbol for f in sorted_by_score[:10]]

        self.Debug(f"[Fine Selection] Selected {len(self.selected_symbols)} stocks")

        return self.selected_symbols


    def OnSecuritiesChanged(self, changes):
        """
        Universe 变化时触发

        Args:
            changes: SecurityChanges
        """
        # 处理新增的证券
        for added in changes.AddedSecurities:
            self.Debug(f"[Universe Added] {added.Symbol}")

            # 添加指标
            symbol = added.Symbol
            self.SMA(symbol, 20, Resolution.Daily)
            # 注意：RSI 的第 3 个参数是 MovingAverageType（均线类型），
            # 不是 Resolution；分辨率从第 4 个参数开始
            self.RSI(symbol, 14, MovingAverageType.Simple, Resolution.Daily)

        # 处理移除的证券
        for removed in changes.RemovedSecurities:
            self.Debug(f"[Universe Removed] {removed.Symbol}")

            # 如果有持仓，平仓
            if self.Portfolio[removed.Symbol].Invested:
                self.Liquidate(removed.Symbol)


    # ========== 定时调仓 ==========

    def Rebalance(self):
        """调仓函数"""
        if not self.selected_symbols:
            return

        self.Debug(f"\n[Rebalance] {self.Time.date()}")

        # 平仓不在新列表中的
        for symbol in list(self.Portfolio.Keys):
            if symbol not in self.selected_symbols and self.Portfolio[symbol].Invested:
                self.Liquidate(symbol)
                self.Debug(f"  Sold {symbol}")

        # 等权买入新选中的股票
        if self.selected_symbols:
            weight = 1.0 / len(self.selected_symbols)

            for symbol in self.selected_symbols:
                self.SetHoldings(symbol, weight)

            self.Debug(f"  Bought {len(self.selected_symbols)} stocks with equal weight")


    # ========== 事件处理 ==========

    def OnOrderEvent(self, orderEvent):
        """订单事件"""
        if orderEvent.Status == OrderStatus.Filled:
            self.Debug(f"[Order Filled] {orderEvent.Symbol} | "
                      f"Qty: {orderEvent.FillQuantity} | "
                      f"Price: ${orderEvent.FillPrice:.2f}")


    def OnEndOfDay(self, symbol):
        """每日收盘"""
        if symbol == self.spy:
            portfolio = self.Portfolio
            self.Debug(f"\n[EOD {self.Time.date()}]")
            self.Debug(f"  Portfolio Value: ${portfolio.TotalPortfolioValue:,.2f}")
            self.Debug(f"  Cash: ${portfolio.Cash:,.2f}")


    def OnEndOfAlgorithm(self):
        """算法结束"""
        self.Debug("\n" + "="*60)
        self.Debug("BACKTEST COMPLETED")
        self.Debug("="*60)
        self.Debug(f"Final Portfolio Value: ${self.Portfolio.TotalPortfolioValue:,.2f}")
        total_return = (self.Portfolio.TotalPortfolioValue / self.starting_cash - 1) * 100
        self.Debug(f"Total Return: {total_return:.2f}%")
        self.Debug("="*60)


# ========== 简化的历史数据示例 ==========

class SimpleHistoryExample(QCAlgorithm):
    """
    简化的历史数据使用示例

    演示最常见的 History API 用法
    """

    def Initialize(self):
        self.SetStartDate(2023, 1, 1)
        self.SetEndDate(2023, 12, 31)
        self.SetCash(100000)

        self.spy = self.AddEquity("SPY", Resolution.Daily).Symbol

        # 创建指标
        self.sma_20 = self.SMA(self.spy, 20)

    def OnData(self, data):
        # 只在第一个交易日计算历史数据
        if self.Time.date() == self.StartDate.date():
            # 获取 20 天历史数据用于预热指标
            history = self.History(self.spy, 20, Resolution.Daily)

            # 注意：Python 的 History 返回 pandas DataFrame，
            # 不能按 bar 对象迭代；用 iterrows 逐行遍历，
            # 行索引就是每根 bar 的 EndTime，字段列名为小写（'close' 等）
            for index, row in history.iterrows():
                self.sma_20.Update(index, row['close'])

            self.Debug(f"Pre-warmed SMA(20): {self.sma_20.Current.Value:.2f}")

        # 等待指标准备
        if not self.sma_20.IsReady:
            return

        # 每月获取一次历史数据进行分析
        if self.Time.day == 1:
            # 获取过去 60 天的数据
            history = self.History(self.spy, 60, Resolution.Daily)

            if not history.empty:
                closes = history['close']

                # 计算 60 天收益率
                total_return = (closes.iloc[-1] / closes.iloc[0] - 1) * 100

                # 计算波动率
                volatility = closes.pct_change().std() * 100

                self.Debug(f"[Monthly Analysis] {self.Time.date()}")
                self.Debug(f"  60-day Return: {total_return:.2f}%")
                self.Debug(f"  Volatility: {volatility:.2f}%")
