import pandas as pd
import numpy as np

STRATEGY = {'id': 'MR-03',
 'name': 'Kalman Filter Mean Reversion',
 'mode': 'single',
 'tags': ['Mean Reversion', 'Statistical'],
 'summary': 'Buy negative deviations from an adaptive Kalman estimate.',
 'description': 'The current close updates the estimate. A one-sided recovery threshold '
                'closes the position, including recovery that overshoots zero.',
 'bestOn': 'Assets with mean-reverting characteristics',
 'frequency': 'Daily',
 'assetClasses': ['Index', 'Commodity', 'FX'],
 'holdingPeriod': 'Days',
 'instruments': ['SPY', 'GLD', 'USO', 'EURUSD=X'],
 'pseudocode': {'Entry': ['When flat, enter if z<−entry_z.'],
                'Exit': ['When held, exit if z>−exit_z; otherwise retain the position.'],
                'Indicators': ['After 20 observations, Q adapts to the population standard '
                               'deviation of the prior 20 log returns. Divide '
                               'price-minus-estimate by the 20-row residual sample standard '
                               'deviation, floored at 1e-8.'],
                'Inputs': ['Q=.001, R=1, entry_z=2, exit_z=.5;20-observation warmup.'],
                'Position sizing': ['100% long or cash.']},
 'notes': ['There is no maximum holding period or independent stop-loss. The estimate is '
           'model-dependent, not verified intrinsic value.'],
 'updated': '2025-06-01',
 'recommendedCompare': ['SPY', 'GLD']}


def generate_signals(prices, config=None):
    config = config or {}
    base_q = float(config.get("Q", 0.001))
    R = float(config.get("R", 1.0))
    entry_threshold = float(config.get("entry_z", 2.0))
    exit_threshold = float(config.get("exit_z", 0.5))

    close = prices.values
    n = len(close)

    # Initialize Kalman Filter
    x_est = np.zeros(n)  # State estimate (fair value)
    P = np.zeros(n)  # Estimate error covariance

    x_est[0] = close[0]
    P[0] = 1.0

    # Calculate returns for adaptive Q
    returns = np.zeros(n)
    returns[1:] = np.diff(np.log(close + 1e-8))

    vol_window = 20

    # Kalman Filter recursion with adaptive Q
    for i in range(1, n):
        # Adaptive Q: increase during high volatility
        if i > vol_window:
            recent_vol = np.std(returns[max(0, i - vol_window) : i])
            Q = base_q * (1 + 10 * recent_vol)
        else:
            Q = base_q

        # Predict step
        x_pred = x_est[i - 1]
        P_pred = P[i - 1] + Q

        # Update step
        K = P_pred / (P_pred + R)  # Kalman gain
        x_est[i] = x_pred + K * (close[i] - x_pred)
        P[i] = (1 - K) * P_pred

    # Calculate standardized residual (z-score)
    residual = close - x_est
    residual_series = pd.Series(residual)
    residual_std = residual_series.rolling(20).std().values
    z_score = residual / np.maximum(residual_std, 1e-8)

    # Generate signals
    signal = pd.Series(0.0, index=prices.index)

    for i in range(20, n):
        prev_signal = signal.iloc[i - 1]

        if prev_signal == 0:
            # Enter long when price is significantly below fair value
            if z_score[i] < -entry_threshold:
                signal.iloc[i] = 1.0
            else:
                signal.iloc[i] = 0.0
        else:
            # Exit when z-score returns to near zero
            if z_score[i] > -exit_threshold:
                signal.iloc[i] = 0.0
            else:
                signal.iloc[i] = 1.0

    return signal.fillna(0.0)


# ---------------------------------------------------------------------------
# Runner added by Quant Arena (https://quantarenas.com/code/) so this file works on its own.
#
#   pip install pandas numpy yfinance
#   python MR-03.py SPY              # downloads daily prices with yfinance
#   python MR-03.py prices.csv       # daily Date and Close; include Open, High, Low for OHLC rules
#
# This default-parameter example does not replay the site's walk-forward fits
# or archived data. Evidence downloads replay the published run's recorded
# arithmetic and include archived source. Recomputing strategy decisions needs
# the exact original inputs, which may not be public.
# Convention, same as the site: the position decided at the close of day t earns
# the return of day t+1. Results are before trading costs. Not investment advice.
# ---------------------------------------------------------------------------
def _load_prices(source):
    import numpy as np

    if source.lower().endswith(".csv"):
        frame = pd.read_csv(source, index_col=0, parse_dates=True)
    else:
        import yfinance as yf
        try:
            frame = yf.download(source, start="2010-01-01", auto_adjust=True, progress=False)
        except Exception as error:
            raise ValueError("Could not download daily prices: " + str(error)) from error
        if not isinstance(frame, pd.DataFrame):
            raise ValueError("Expected a table of daily prices for one ticker.")
        if isinstance(frame.columns, pd.MultiIndex):
            levels = [level for level in range(frame.columns.nlevels)
                      if "Close" in frame.columns.get_level_values(level)]
            if len(levels) != 1:
                raise ValueError("Expected daily prices for one ticker.")
            frame.columns = frame.columns.get_level_values(levels[0])
    required = ["Open", "High", "Low", "Close"] if STRATEGY.get("input") == "ohlc" else ["Close"]
    if frame.columns.has_duplicates:
        raise ValueError("Expected one column per price field for one ticker.")
    missing = [name for name in required if name not in frame.columns]
    if missing:
        raise ValueError("Missing price columns: " + ", ".join(missing))
    if not isinstance(frame.index, pd.DatetimeIndex) or frame.index.hasnans or frame.index.normalize().has_duplicates:
        raise ValueError("Prices need one row per valid calendar date.")
    frame = frame.copy()
    frame[required] = frame[required].apply(pd.to_numeric, errors="raise")
    frame = frame.sort_index().dropna(subset=required)
    prices = frame[required].to_numpy(dtype=float)
    if len(frame) < 2:
        raise ValueError("Need at least two complete daily observations.")
    if not np.isfinite(prices).all() or (prices <= 0).any():
        raise ValueError("Prices must be finite and positive.")
    return frame


if __name__ == "__main__":
    import sys

    try:
        prices = _load_prices(sys.argv[1] if len(sys.argv) > 1 else "SPY")
    except (OSError, TypeError, ValueError) as error:
        raise SystemExit("Input error: " + str(error)) from error
    close = prices["Close"]
    signal_input = prices if STRATEGY.get("input") == "ohlc" else close
    position = generate_signals(signal_input).reindex(close.index).ffill().fillna(0.0)
    daily = close.pct_change().fillna(0.0) * position.shift(1).fillna(0.0)
    equity = (1 + daily).cumprod()
    years = (close.index[-1].date() - close.index[0].date()).days / 365.25
    print(STRATEGY["id"], STRATEGY["name"], "|", close.index[0].date(), "to", close.index[-1].date())
    print("Total return   {:+.1%}".format(equity.iloc[-1] - 1))
    print("CAGR           {:+.1%}".format(equity.iloc[-1] ** (1 / years) - 1))
    print("Sharpe         {:.2f}".format(daily.mean() / daily.std() * 252 ** 0.5 if daily.std() else 0.0))
    print("Max drawdown   {:.1%}".format((equity / equity.cummax() - 1).min()))
    print("Days in market {:.0%}".format((position != 0).mean()))
    print("Buy and hold   {:+.1%}".format(close.iloc[-1] / close.iloc[0] - 1))
