# [ARCHIVED - Sharpe ceiling ~0.13, cf ARCHIVE.md]
# region imports
from AlgorithmImports import *
# endregion


class TurnOfMonthEffect(QCAlgorithm):
    """
    Turn of the Month Effect v2.1

    Calendar anomaly: buy SPY+QQQ around month boundary.
    Window: last 4 + first 4 trading days. 1.5x leverage.
    SMA200 regime filter.

    This is the validated best version for 2015-2026:
    v2.0 SPY+QQQ with 4/4 window remains the optimal configuration.

    Iteration 3 learnings (research.ipynb + 6 backtests):
    - SPY-only (v3.1): Sharpe -0.026 - QQQ outperformance in tech bull is crucial
    - Momentum filter (v3.2): Sharpe 0.006 - anti-correlated with ToM alpha
    - SPY+QQQ+IWM (v3.3): Sharpe 0.027 - IWM dilutes QQQ in 2015-2026
    - Stop-loss -4% (v3.4): Sharpe 0.096, MaxDD 21.2% - cuts too many good cycles
    - Window 4/3 (v3.0): Sharpe -0.203 - only better in theory, not practice

    Iteration 4 learnings (research.ipynb H7-H9, 2026-03-08):
    - H7: VIX filter (VIX<20/25/30) tested - SMA200 already captures regime change,
      VIX adds no incremental value. SMA200 is simpler and equally effective.
    - H8: Dynamic VIX sizing (inv_vix, discrete3, discrete2) tested - no Sharpe gain
      vs fixed 1.5x. Varying leverage by VIX is beta-loading, not signal amplification.
    - H9: ToM premium is HIGHER in bear markets (SPY < SMA200), LOWER in bull.
      2015-2026 is ~90% bull -> ToM premium is structurally diluted.
      This confirms v2.1 is the correct implementation, period constraint is structural.

    Iteration 5 learnings (2026-03-09):
    - Stop-loss -8% (v2.2): Sharpe 0.117, MaxDD 24.4% - WORSE than baseline
      Short holding period (~8 trading days) means positions recover within window.
      Stop-loss cuts cycles that would have recovered, reducing wins without
      meaningfully reducing MaxDD (which comes from sustained market drawdowns).
      Conclusion: no stop-loss is optimal for this calendar strategy.

    Why 0.128 is the honest ceiling for 2015-2026:
    The ToM effect has Sharpe ~0.36 (research, 2000-2025, 1.5x) because it
    outperforms in bear markets (forced institutional rebalancing at month-end).
    In 2015-2026 (pure bull), every day has similar returns, and only brief
    bear episodes (Dec 2018, COVID Mar 2020) generate the expected ToM premium.
    The strategy correctly demonstrates the anomaly but is period-constrained.

    Honest assessment:
    - Effect exists: t=2.38 on day 1, confirmed by research
    - 2015-2026 is a hard period for calendar strategies (no sustained bear regime)
    - Sharpe 0.128 reflects the SIGNAL quality on this period, not a code failure
    - The academic Sharpe (0.36 on 2000-2025) includes 2000-02, 2008, 2020 crises
      where ToM premium is strong. 2015-2026 lacks these bear episodes.
    - Pedagogically: demonstrates ToM, regime filtering, and period-dependency of signals

    Backtest history:
    v1.0: Sharpe -0.243, CAGR  1.5%, MaxDD 13.2%
    v2.0: Sharpe  0.127, CAGR  4.8%, MaxDD 23.7% (iteration 2)
    v2.1: Sharpe  0.128, CAGR  4.8%, MaxDD 23.7% (iteration 3, best confirmed)
    v2.2: Sharpe  0.117, CAGR  4.7%, MaxDD 24.4% (iteration 5, stop-loss -8%, REVERTED)

    Ref: Ariel (1987), Lakonishok & Smidt (1988), research.ipynb
    """

    def initialize(self):
        self.set_start_date(2015, 1, 1)
        self.set_end_date(2024, 12, 31)
        self.set_cash(100000)
        self.set_brokerage_model(BrokerageName.INTERACTIVE_BROKERS_BROKERAGE, AccountType.MARGIN)

        self.spy = self.add_equity("SPY", Resolution.DAILY).symbol
        self.qqq = self.add_equity("QQQ", Resolution.DAILY).symbol

        # SMA200 regime filter
        self.spy_sma = self.sma(self.spy, 200, Resolution.DAILY)

        # Trading day tracking
        self.trading_day_of_month = 0
        self.current_month = -1

        # Parameters
        self.entry_days_before_eom = 4   # Last 4 trading days
        self.hold_days_after_bom = 4     # First 4 trading days
        self.leverage = 1.5              # 1.5x leverage

        self.schedule.on(
            self.date_rules.every_day("SPY"),
            self.time_rules.after_market_open("SPY", 30),
            self._check_calendar
        )

        self.is_invested = False
        self.set_benchmark("SPY")
        self.set_warm_up(200, Resolution.DAILY)

    def _check_calendar(self):
        if self.is_warming_up:
            return

        # Track trading days in month
        if self.time.month != self.current_month:
            self.current_month = self.time.month
            self.trading_day_of_month = 1
        else:
            self.trading_day_of_month += 1

        # Estimate trading days remaining via calendar
        import calendar
        _, days_in_month = calendar.monthrange(self.time.year, self.time.month)
        calendar_days_remaining = days_in_month - self.time.day
        trading_days_remaining = int(calendar_days_remaining * 0.7)

        # Entry: last N trading days OR first N trading days
        in_tom_window = (trading_days_remaining <= self.entry_days_before_eom or
                         self.trading_day_of_month <= self.hold_days_after_bom)

        # Regime filter
        spy_above_sma = True
        if self.spy_sma.is_ready:
            spy_above_sma = self.securities[self.spy].price > self.spy_sma.current.value

        if in_tom_window and spy_above_sma and not self.is_invested:
            weight = self.leverage / 2.0
            self.set_holdings(self.spy, weight)
            self.set_holdings(self.qqq, weight)
            self.is_invested = True

        elif (not in_tom_window or not spy_above_sma) and self.is_invested:
            self.liquidate(self.spy)
            self.liquidate(self.qqq)
            self.is_invested = False

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