"""RSI 超賣均值回歸：三項穩健性驗證（/blog/python-talib-158-technical-indicators 驗證段的數字來源）。

條件與文中回測相同，只是不截斷結束日：
- 股票池：60 日均量大於 500 萬股
- 訊號：data.indicator("RSI", timeperiod=14) 小於 30
- 符合條件的股票等權持有，2016 年起每月換股，sim() 預設手續費 0.1425%（文中打 5 折）與證交稅 0.3%

三項檢查：
1. 鄰近參數：RSI 天數 10、14、20 乘上超賣門檻 25、30、35，9 組日夏普要有 7 組以上高於同期 0050 含息
2. 前後兩段：期間對半切，兩段都要高於 0050 含息
3. 交易成本：sim() 預設手續費與證交稅（另跑不扣成本、手續費 5 折兩種對照）

統計口徑：日夏普 = 日報酬平均 / 標準差 × √252，不扣無風險利率；
0050 含息 = etl:adj_close 還原價買進持有，與策略同一段期間、不扣成本。
執行後會在目前目錄輸出 metrics.json（統計）。

    pip install finlab TA-Lib
    python strategy.py

finlab 需要資料時會自動引導登入，不需要在程式裡填 token。
"""

import json

import numpy as np
import pandas as pd

import finlab
from finlab import data
from finlab.backtest import sim

STRATEGY_NAME = "RSI 超賣均值回歸"
START = "2016-01-01"
RSI_DAYS = 14     # RSI 天數
THRESHOLD = 30    # RSI 低於此值視為超賣
RSI_DAYS_GRID = [10, 14, 20]
THRESHOLD_GRID = [25, 30, 35]


def build_report(rsi_days=RSI_DAYS, threshold=THRESHOLD, **cost):
    close = data.get("price:收盤價")
    vol = data.get("price:成交股數")
    rsi = data.indicator("RSI", timeperiod=rsi_days)

    liquid = (vol.rolling(60).mean() > 5_000_000) & close.notna()
    position = ((rsi < threshold) & liquid).astype(float)
    position = position.div(position.sum(axis=1).replace(0, np.nan), axis=0).fillna(0)
    return sim(position.loc[START:], resample="M", upload=False, name=STRATEGY_NAME, **cost)


def curve_stats(curve: pd.Series) -> dict:
    curve = curve / curve.iloc[0]
    daily = curve.pct_change().dropna()
    years = (curve.index[-1] - curve.index[0]).days / 365.25
    return {
        "cagr": curve.iloc[-1] ** (1 / years) - 1,
        "daily_sharpe": daily.mean() / daily.std() * 252 ** 0.5,
        "max_drawdown": (curve / curve.cummax() - 1).min(),
        "total_return": curve.iloc[-1] - 1,
    }


def trimmed(creturn: pd.Series) -> pd.Series:
    """從第一次有部位的前一天起算。"""
    creturn = creturn.dropna()
    first_move = (creturn != creturn.iloc[0]).argmax()
    return creturn.iloc[max(first_move - 1, 0):]


def window_stats(creturn: pd.Series, bench_all: pd.Series) -> dict:
    bench = bench_all.loc[creturn.index[0]:creturn.index[-1]]
    return {"window": f"{creturn.index[0].date()}/{creturn.index[-1].date()}",
            "strategy": curve_stats(creturn), "benchmark_0050": curve_stats(bench)}


def rounded(obj):
    if isinstance(obj, dict):
        return {k: rounded(v) for k, v in obj.items()}
    return round(float(obj), 6) if isinstance(obj, float) else obj


def main() -> None:
    finlab.login()
    bench_all = data.get("etl:adj_close")["0050"].dropna()
    creturn = trimmed(build_report().creturn)
    mid = creturn.loc[creturn.index[0] + (creturn.index[-1] - creturn.index[0]) / 2:].index[0]
    result = {
        "main": window_stats(creturn, bench_all),
        "no_cost": curve_stats(trimmed(build_report(fee_ratio=0, tax_ratio=0).creturn)),
        "half_fee": curve_stats(trimmed(build_report(fee_ratio=0.001425 / 2).creturn)),
        "grid": {f"門檻 {t}、RSI {d} 日": window_stats(trimmed(build_report(d, t).creturn), bench_all)
                 for t in THRESHOLD_GRID for d in RSI_DAYS_GRID},
        "halves": {"first": window_stats(creturn.loc[:mid], bench_all),
                   "second": window_stats(creturn.loc[mid:], bench_all)},
    }

    wins = sum(g["strategy"]["daily_sharpe"] > g["benchmark_0050"]["daily_sharpe"] for g in result["grid"].values())
    print(f"期間 {result['main']['window']}")
    for label, s in [("策略", result["main"]["strategy"]), ("0050 含息", result["main"]["benchmark_0050"]),
                     ("不扣成本", result["no_cost"]), ("手續費 5 折", result["half_fee"])]:
        print(f"{label}：年化 {s['cagr']:.2%}，日夏普 {s['daily_sharpe']:.2f}，最大回撤 {s['max_drawdown']:.2%}")
    print(f"鄰近參數：{wins}/9 組日夏普高於 0050 含息")
    for label, g in result["grid"].items():
        print(f"  {label}：{g['strategy']['daily_sharpe']:.2f}"
              f"（年化 {g['strategy']['cagr']:.2%}；0050 {g['benchmark_0050']['daily_sharpe']:.2f}）")
    for key, part in result["halves"].items():
        print(f"{key} {part['window']}：{part['strategy']['daily_sharpe']:.2f} 對 {part['benchmark_0050']['daily_sharpe']:.2f}")

    metrics = {
        "star": STRATEGY_NAME,
        **result,
        "run_date": str(pd.Timestamp.today().date()),
        "finlab_version": finlab.__version__,
        "generated_by": "static/blog/python-talib-158-technical-indicators/validation/strategy.py",
    }
    with open("metrics.json", "w", encoding="utf-8") as f:
        json.dump(rounded(metrics), f, ensure_ascii=False, indent=2)


if __name__ == "__main__":
    main()
