# region imports
from AlgorithmImports import *
# endregion


class AllWeatherPortfolio(QCAlgorithm):
    """
    All-Weather Portfolio Strategy v5.0

    Key change from v4.0: Reduce IEF from 40% to 30%, increase GLD from 20% to 30%.
    Research (iter5, weight grid search 2015-2026 ; voir la note de
    reproductibilite plus bas) : IEF returned only 1.47% annualized
    due to the 2020-2023 Fed rate hike cycle, while GLD returned 13.24% annualized.
    Both are diversifiers (GLD corr=0.044 with SPY, IEF corr=-0.161), but GLD dominates
    on risk-adjusted return. Beta increase is marginal (+0.009: 0.328 -> 0.337).
    Alpha improves: 0.033 -> 0.043 (confirmed real diversification gain, not beta loading).

    Allocation v5.0:
      SPY 30%  - Actions US (unchanged)
      TLT  0%  - Long bonds: eliminated (v4.0)
      IEF 30%  - Intermediate bonds (was 40%: -10pp, too much duration drag 2020-2023)
      GLD 30%  - Or (was 20%: +10pp, inflation/uncertainty hedge, corr~0 with SPY)
      XLP 10%  - Consumer Staples (unchanged, defensive equity)

    Research findings (iter5, weight grid search):
    - Grid: 40+ combos tested. Shifting IEF->GLD improves Sharpe consistently.
    - IEF 40%->30%, GLD 20%->30%: yfinance Sharpe 1.101->1.149 (+0.048)
    - Beta stable: 0.328->0.337 (marginal, improvement is alpha-driven)
    - Alpha improves: 0.033->0.043 (diversification quality gain)
    - SPY held constant at 30% (no equity beta loading)

    Note de reproductibilite : les chiffres de la campagne iter5 ci-dessus
    (Sharpe yfinance 1.101 puis 1.149, beta 0.328 -> 0.337, alpha 0.033 -> 0.043)
    ne sont pas dans le carnet `research.ipynb` du depot, qui ne mesure que
    l'exploration H1-H4 ; ils ne sont pas rejouables en l'etat.

    Integrity check: improvement is NOT beta loading.
    - SPY weight unchanged at 30%
    - Beta increase only +0.009 (within noise)
    - Alpha increases: genuine diversification improvement
    - GLD: zero-to-near-zero correlation with SPY (0.044), true diversifier

    Backtest results:
    v1.0: Sharpe 0.250, CAGR 5.9%,  MaxDD 23.5% (Dalio static, DBC)
    v2.0: Sharpe 0.264, CAGR 5.8%,  MaxDD 17.6% (Gold Heavy + SMA50%)
    v2.1: Sharpe 0.365, CAGR 7.2%,  MaxDD 24.1% (Gold Heavy, no SMA)
    v2.2: Sharpe 0.325, CAGR 6.5%,  MaxDD 20.4% (SMA25%, worse)
    v3.0: Sharpe 0.482, CAGR 8.2%,  MaxDD 20.7%, Net +140.3%
    v4.0: Sharpe 0.520, CAGR 8.2%,  MaxDD 17.0% (TLT eliminated, IEF 40%)
    v5.0: Sharpe 0.474, CAGR 7.551%, MaxDD 16.400%, Net +107.2% (IEF 30%, GLD 30%)

    v5.0 : mesure du backtest QC Cloud de ce fichier sur sa fenetre codee
    (2015-01-01 -> 2024-12-31, 2516 seances negociables, 130 ordres). Les lignes
    v1.0 a v4.0 sont un historique de conception : ce fichier n'implemente que
    la v5.0 et ne rejoue aucune des versions anterieures. Pour le modele de
    portefeuille du carnet `quantbook.ipynb` (prix QC natifs, taux sans risque
    3 %, sans couts de transaction), la meme allocation rend Sharpe 0.603 /
    CAGR 8.0 % / MaxDD -17.2 % sur la fenetre du carnet : modele et fenetre
    differents, les deux jeux de chiffres ne se comparent pas directement.

    Ref: Dalio (2017), research.ipynb (exploration H1-H4, yfinance
    2015-01-02 -> 2025-12-31) et quantbook.ipynb (mesure QC native)
    """

    def initialize(self):
        self.set_start_date(2015, 1, 1)
        self.set_end_date(2024, 12, 31)
        self.set_cash(100000)

        # Brokerage: US ETFs (SPY/IEF/GLD/XLP) traded via IBKR margin account
        # Configurable brokerage for fee sweep (default: IBKR margin with real fees)
        self._brokerage_mode = self.get_parameter("brokerage", "ibkr")
        if self._brokerage_mode != "none":
            self.set_brokerage_model(BrokerageName.INTERACTIVE_BROKERS_BROKERAGE, AccountType.MARGIN)

        # v5.0: IEF reduced 40%->30%, GLD raised 20%->30%
        # Rationale: IEF returned only 1.47% annualized (2015-2026) due to rate hike drag.
        # GLD returned 13.24% with near-zero SPY correlation (0.044). Alpha increases.
        self.target_allocations = {
            "SPY": 0.30,   # 30% Actions US (unchanged)
            "IEF": 0.30,   # 30% Obligations intermediaires (was 40%: -10pp)
            "GLD": 0.30,   # 30% Or (was 20%: +10pp, better risk-adjusted diversifier)
            "XLP": 0.10,   # 10% Consumer Staples (defensif, dividendes, faible beta)
        }

        self.symbols = {}
        for ticker in self.target_allocations.keys():
            equity = self.add_equity(ticker, Resolution.DAILY)
            self.symbols[ticker] = equity.symbol

        # Tighter drift threshold: 3% (was 5%) for more responsive rebalancing
        self.rebalance_threshold = 0.03
        self.last_rebalance_month = -1

        self.schedule.on(
            self.date_rules.month_start("SPY"),
            self.time_rules.after_market_open("SPY", 30),
            self._check_rebalance
        )

        self.set_benchmark("SPY")

    def _check_rebalance(self):
        current_month = self.time.month
        # Quarterly rebalancing (Jan, Apr, Jul, Oct)
        if current_month in [1, 4, 7, 10]:
            if self.last_rebalance_month != current_month:
                self._rebalance()
                return
        # Drift-based rebalancing
        if self._check_drift():
            self._rebalance()

    def _check_drift(self):
        total_value = self.portfolio.total_portfolio_value
        if total_value == 0:
            return False
        for ticker, target in self.target_allocations.items():
            symbol = self.symbols[ticker]
            current = self.portfolio[symbol].holdings_value / total_value
            if abs(current - target) > self.rebalance_threshold:
                return True
        return False

    def _rebalance(self):
        self.last_rebalance_month = self.time.month
        for ticker, weight in self.target_allocations.items():
            self.set_holdings(self.symbols[ticker], weight)

    def on_data(self, data):
        if not self.portfolio.invested:
            self._rebalance()

    def on_end_of_algorithm(self):
        final = self.portfolio.total_portfolio_value
        self.log(f"AW v5.0: Final=${final:,.2f}, Return={(final-100000)/100000:.2%}")
