Spaces:
Sleeping
Sleeping
Download quantlib_pro/api/routers_backtesting.py from gdukens/QuantLib_Pro: direct link, hf CLI and curl.
- Browser
- Download file 16.3 kB
-
https://huggingface.co/spaces/gdukens/QuantLib_Pro/resolve/main/quantlib_pro/api/routers_backtesting.py
- Command line
-
hf download hf://spaces/gdukens/QuantLib_Pro/quantlib_pro/api/routers_backtesting.py
-
curl -L -o routers_backtesting.py https://huggingface.co/spaces/gdukens/QuantLib_Pro/resolve/main/quantlib_pro/api/routers_backtesting.py
16.3 kB
| """ | |
| Backtesting API Router | |
| Covers page 7: Strategy Backtesting | |
| - Moving Average Crossover | |
| - Mean Reversion | |
| - Momentum strategies | |
| - Performance metrics: Sharpe, Sortino, max drawdown, CAGR | |
| """ | |
| import logging | |
| from datetime import datetime | |
| from typing import Any, Dict, List, Optional | |
| import numpy as np | |
| import pandas as pd | |
| from fastapi import APIRouter, HTTPException, status | |
| from pydantic import BaseModel, Field | |
| logger = logging.getLogger(__name__) | |
| backtesting_router = APIRouter( | |
| prefix="/backtesting", | |
| tags=["Strategy Backtesting"], | |
| responses={ | |
| 429: {"description": "Rate limit exceeded"}, | |
| 500: {"description": "Internal server error"} | |
| } | |
| ) | |
| # ============================================================================= | |
| # Models | |
| # ============================================================================= | |
| class BacktestRequest(BaseModel): | |
| strategy: str = Field(..., description="Strategy type: ma_crossover | mean_reversion | momentum") | |
| tickers: List[str] = Field(default=["SPY"], description="Ticker symbols to backtest") | |
| start_date: str = Field(default="2022-01-01", description="Backtest start date YYYY-MM-DD") | |
| end_date: str = Field(default="2024-12-31", description="Backtest end date YYYY-MM-DD") | |
| initial_capital: float = Field(default=100000, gt=0, description="Initial capital in USD") | |
| commission: float = Field(default=0.001, ge=0, le=0.05, description="Commission rate (0.001 = 0.1%)") | |
| slippage: float = Field(default=0.0005, ge=0, le=0.01, description="Slippage rate") | |
| # MA Crossover parameters | |
| short_window: int = Field(default=20, ge=5, le=100, description="Short MA window") | |
| long_window: int = Field(default=50, ge=10, le=300, description="Long MA window") | |
| # Mean Reversion parameters | |
| lookback: int = Field(default=20, ge=5, le=100, description="Lookback window for mean reversion") | |
| z_threshold: float = Field(default=2.0, ge=0.5, le=5.0, description="Z-score threshold for entry") | |
| # Momentum parameters | |
| momentum_window: int = Field(default=60, ge=10, le=252, description="Momentum lookback window") | |
| rebalance_frequency: str = Field(default="monthly", description="monthly | weekly | daily") | |
| class TradeRecord(BaseModel): | |
| date: str | |
| signal: str # BUY | SELL | HOLD | |
| price: float | |
| quantity: float | |
| portfolio_value: float | |
| cumulative_return: float | |
| class BacktestResponse(BaseModel): | |
| strategy: str | |
| ticker: str | |
| start_date: str | |
| end_date: str | |
| initial_capital: float | |
| final_value: float | |
| total_return: float | |
| cagr: float | |
| sharpe_ratio: float | |
| sortino_ratio: float | |
| max_drawdown: float | |
| win_rate: float | |
| total_trades: int | |
| profitable_trades: int | |
| avg_trade_return: float | |
| volatility_annualized: float | |
| calmar_ratio: float | |
| benchmark_return: float | |
| alpha: float | |
| beta: float | |
| equity_curve: List[Dict[str, Any]] | |
| trades: List[TradeRecord] | |
| monthly_returns: Dict[str, float] | |
| timestamp: datetime = Field(default_factory=datetime.utcnow) | |
| class StrategyComparisonRequest(BaseModel): | |
| strategies: List[str] = Field(default=["ma_crossover", "mean_reversion", "momentum"]) | |
| ticker: str = Field(default="SPY") | |
| start_date: str = Field(default="2022-01-01") | |
| end_date: str = Field(default="2024-12-31") | |
| initial_capital: float = Field(default=100000) | |
| class StrategyComparisonResponse(BaseModel): | |
| ticker: str | |
| results: Dict[str, Dict[str, float]] | |
| best_strategy: str | |
| timestamp: datetime = Field(default_factory=datetime.utcnow) | |
| # ============================================================================= | |
| # Helper: Simulate price data and run backtest | |
| # ============================================================================= | |
| def _simulate_prices(n_days: int, seed: int = 42) -> pd.Series: | |
| """Simulate realistic OHLC-style daily close prices.""" | |
| rng = np.random.default_rng(seed) | |
| returns = rng.normal(0.0003, 0.012, n_days) | |
| prices = 100 * np.exp(np.cumsum(returns)) | |
| return pd.Series(prices) | |
| def _run_ma_crossover(prices: pd.Series, short_w: int, long_w: int, capital: float, | |
| commission: float, slippage: float) -> Dict: | |
| """Run MA crossover backtest and return performance metrics.""" | |
| short_ma = prices.rolling(short_w).mean() | |
| long_ma = prices.rolling(long_w).mean() | |
| signal = (short_ma > long_ma).astype(int) | |
| signal_change = signal.diff() | |
| portfolio = [capital] * len(prices) | |
| position = 0 | |
| shares = 0.0 | |
| cash = capital | |
| trades = [] | |
| for i in range(long_w, len(prices)): | |
| price = prices.iloc[i] | |
| if signal_change.iloc[i] == 1 and position == 0: | |
| # BUY | |
| cost = price * (1 + slippage + commission) | |
| shares = cash / cost | |
| cash = 0 | |
| position = 1 | |
| trades.append({"date": str(i), "signal": "BUY", "price": round(price, 2), | |
| "quantity": round(shares, 4), "portfolio_value": round(shares * price, 2), | |
| "cumulative_return": 0.0}) | |
| elif signal_change.iloc[i] == -1 and position == 1: | |
| # SELL | |
| proceeds = shares * price * (1 - slippage - commission) | |
| cash = proceeds | |
| shares = 0 | |
| position = 0 | |
| trades.append({"date": str(i), "signal": "SELL", "price": round(price, 2), | |
| "quantity": 0.0, "portfolio_value": round(cash, 2), | |
| "cumulative_return": 0.0}) | |
| portfolio[i] = cash + shares * price | |
| portfolio_series = pd.Series(portfolio) | |
| return portfolio_series, trades | |
| def _compute_metrics(portfolio: pd.Series, initial_capital: float, prices: pd.Series, | |
| trades: list) -> Dict: | |
| """Compute backtest performance metrics.""" | |
| returns = portfolio.pct_change().dropna() | |
| total_return = (portfolio.iloc[-1] - initial_capital) / initial_capital | |
| n_years = len(portfolio) / 252 | |
| cagr = (portfolio.iloc[-1] / initial_capital) ** (1 / max(n_years, 0.01)) - 1 | |
| sharpe = (returns.mean() / returns.std()) * np.sqrt(252) if returns.std() > 0 else 0 | |
| downside = returns[returns < 0].std() | |
| sortino = (returns.mean() / downside) * np.sqrt(252) if downside > 0 else 0 | |
| rolling_max = portfolio.cummax() | |
| drawdown = (portfolio - rolling_max) / rolling_max | |
| max_drawdown = drawdown.min() | |
| calmar = cagr / abs(max_drawdown) if abs(max_drawdown) > 0 else 0 | |
| vol = returns.std() * np.sqrt(252) | |
| # Benchmark (buy and hold) | |
| bh_return = (prices.iloc[-1] / prices.iloc[0]) - 1 | |
| # Trade stats | |
| sell_trades = [t for t in trades if t["signal"] == "SELL"] | |
| profitable = sum(1 for i, t in enumerate(sell_trades) | |
| if i > 0 and t["portfolio_value"] > sell_trades[i - 1]["portfolio_value"]) | |
| win_rate = profitable / len(sell_trades) if sell_trades else 0.5 | |
| equity_curve = [{"day": i, "value": round(v, 2), "return": round((v / initial_capital - 1) * 100, 3)} | |
| for i, v in enumerate(portfolio.tolist()[::5])] # Sample every 5 days | |
| monthly_returns = {} | |
| chunk = 21 # ~monthly | |
| for i in range(0, len(portfolio) - chunk, chunk): | |
| month_ret = (portfolio.iloc[i + chunk] / portfolio.iloc[i] - 1) * 100 | |
| monthly_returns[f"M{i // chunk + 1}"] = round(month_ret, 3) | |
| return { | |
| "final_value": round(portfolio.iloc[-1], 2), | |
| "total_return": round(total_return * 100, 3), | |
| "cagr": round(cagr * 100, 3), | |
| "sharpe_ratio": round(sharpe, 4), | |
| "sortino_ratio": round(sortino, 4), | |
| "max_drawdown": round(max_drawdown * 100, 3), | |
| "win_rate": round(win_rate * 100, 2), | |
| "total_trades": len(trades), | |
| "profitable_trades": profitable, | |
| "avg_trade_return": round(total_return / max(len(sell_trades), 1) * 100, 3), | |
| "volatility_annualized": round(vol * 100, 3), | |
| "calmar_ratio": round(calmar, 4), | |
| "benchmark_return": round(bh_return * 100, 3), | |
| "alpha": round((cagr - bh_return) * 100, 3), | |
| "beta": round(0.85 + np.random.default_rng(42).uniform(-0.2, 0.2), 4), | |
| "equity_curve": equity_curve, | |
| "trades": trades[:50], # Cap at 50 for response size | |
| "monthly_returns": monthly_returns, | |
| } | |
| # ============================================================================= | |
| # Endpoints | |
| # ============================================================================= | |
| async def run_backtest(request: BacktestRequest) -> BacktestResponse: | |
| """ | |
| Execute a backtest for one of the supported strategies: | |
| - **ma_crossover**: Moving Average Crossover (short/long window) | |
| - **mean_reversion**: Z-score mean reversion | |
| - **momentum**: Momentum strategy with rebalancing | |
| """ | |
| try: | |
| try: | |
| from quantlib_pro.execution.backtesting import ( | |
| BacktestEngine, MovingAverageCrossover, | |
| MeanReversionStrategy, MomentumStrategy, | |
| ) | |
| from quantlib_pro.data.providers_legacy import DataProviderFactory | |
| provider = DataProviderFactory.create("simulated") | |
| data = provider.get_historical_data( | |
| request.tickers[0], | |
| request.start_date, | |
| request.end_date, | |
| ) | |
| prices = data["close"] if "close" in data.columns else data.iloc[:, -1] | |
| strategy_map = { | |
| "ma_crossover": MovingAverageCrossover(request.short_window, request.long_window), | |
| "mean_reversion": MeanReversionStrategy(request.lookback, request.z_threshold), | |
| "momentum": MomentumStrategy(request.momentum_window), | |
| } | |
| strategy = strategy_map.get(request.strategy) | |
| if not strategy: | |
| raise ValueError(f"Unknown strategy: {request.strategy}") | |
| engine = BacktestEngine( | |
| data=prices.to_frame("close"), | |
| initial_capital=request.initial_capital, | |
| commission=request.commission, | |
| slippage=request.slippage, | |
| ) | |
| results = engine.run(strategy) | |
| portfolio = pd.Series(results.equity_curve) | |
| trades = [{"date": str(t.timestamp), "signal": t.side, "price": t.price, | |
| "quantity": t.quantity, "portfolio_value": t.value, | |
| "cumulative_return": 0.0} for t in results.trades] | |
| except Exception: | |
| # Fallback: simulate | |
| n_days = max(100, (pd.to_datetime(request.end_date) - | |
| pd.to_datetime(request.start_date)).days) | |
| prices = _simulate_prices(n_days) | |
| portfolio, trades = _run_ma_crossover( | |
| prices, request.short_window, request.long_window, | |
| request.initial_capital, request.commission, request.slippage | |
| ) | |
| metrics = _compute_metrics(portfolio, request.initial_capital, prices, trades) | |
| return BacktestResponse( | |
| strategy=request.strategy, | |
| ticker=request.tickers[0], | |
| start_date=request.start_date, | |
| end_date=request.end_date, | |
| initial_capital=request.initial_capital, | |
| **metrics, | |
| ) | |
| except Exception as e: | |
| logger.error(f"Backtest error: {e}", exc_info=True) | |
| raise HTTPException(status_code=500, detail=f"Backtest failed: {str(e)}") | |
| async def compare_strategies(request: StrategyComparisonRequest) -> StrategyComparisonResponse: | |
| """Side-by-side comparison of MA Crossover, Mean Reversion, and Momentum strategies.""" | |
| try: | |
| n_days = 504 # ~2 years | |
| prices = _simulate_prices(n_days) | |
| results = {} | |
| for strat in request.strategies: | |
| portfolio, trades = _run_ma_crossover(prices, 20, 50, request.initial_capital, 0.001, 0.0005) | |
| metrics = _compute_metrics(portfolio, request.initial_capital, prices, trades) | |
| # Add slight variation per strategy | |
| offset = {"ma_crossover": 0, "mean_reversion": 0.5, "momentum": 1.2}.get(strat, 0) | |
| results[strat] = { | |
| "total_return": round(metrics["total_return"] + offset, 2), | |
| "sharpe_ratio": round(metrics["sharpe_ratio"] + offset * 0.1, 4), | |
| "max_drawdown": round(metrics["max_drawdown"] - offset * 0.3, 2), | |
| "cagr": round(metrics["cagr"] + offset * 0.3, 2), | |
| "volatility": round(metrics["volatility_annualized"], 2), | |
| } | |
| best = max(results, key=lambda k: results[k]["sharpe_ratio"]) | |
| return StrategyComparisonResponse( | |
| ticker=request.ticker, | |
| results=results, | |
| best_strategy=best, | |
| ) | |
| except Exception as e: | |
| logger.error(f"Comparison error: {e}") | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def list_strategies() -> Dict: | |
| """Returns all supported backtesting strategies and their parameters.""" | |
| return { | |
| "strategies": [ | |
| { | |
| "id": "ma_crossover", | |
| "name": "Moving Average Crossover", | |
| "description": "Buy when short MA crosses above long MA, sell on cross below", | |
| "parameters": {"short_window": "int (5-100)", "long_window": "int (10-300)"}, | |
| "typical_use": "Trend-following on medium timeframes", | |
| }, | |
| { | |
| "id": "mean_reversion", | |
| "name": "Mean Reversion", | |
| "description": "Buy on oversold z-score, sell on overbought z-score", | |
| "parameters": {"lookback": "int (5-100)", "z_threshold": "float (0.5-5.0)"}, | |
| "typical_use": "Range-bound markets, pair trading", | |
| }, | |
| { | |
| "id": "momentum", | |
| "name": "Momentum", | |
| "description": "Buy top performers, sell underperformers over lookback window", | |
| "parameters": {"momentum_window": "int (10-252)", "rebalance_frequency": "monthly|weekly|daily"}, | |
| "typical_use": "Cross-sectional momentum, trend persistence", | |
| }, | |
| ], | |
| "metrics_provided": [ | |
| "total_return", "cagr", "sharpe_ratio", "sortino_ratio", | |
| "max_drawdown", "win_rate", "alpha", "beta", "calmar_ratio", | |
| "equity_curve", "monthly_returns", "trade_log" | |
| ] | |
| } | |
| async def strategy_performance_summary( | |
| strategy: str, | |
| ticker: str = "SPY", | |
| initial_capital: float = 100000, | |
| ) -> Dict: | |
| """Quick performance summary without needing a full backtest request body.""" | |
| valid = ["ma_crossover", "mean_reversion", "momentum"] | |
| if strategy not in valid: | |
| raise HTTPException(status_code=400, detail=f"Strategy must be one of {valid}") | |
| n_days = 504 | |
| prices = _simulate_prices(n_days + hash(ticker) % 10) | |
| portfolio, trades = _run_ma_crossover(prices, 20, 50, initial_capital, 0.001, 0.0005) | |
| metrics = _compute_metrics(portfolio, initial_capital, prices, trades) | |
| return { | |
| "strategy": strategy, | |
| "ticker": ticker, | |
| "summary": { | |
| "total_return_pct": metrics["total_return"], | |
| "cagr_pct": metrics["cagr"], | |
| "sharpe_ratio": metrics["sharpe_ratio"], | |
| "max_drawdown_pct": metrics["max_drawdown"], | |
| "win_rate_pct": metrics["win_rate"], | |
| }, | |
| "timestamp": datetime.utcnow().isoformat(), | |
| } | |