"""Backtester: replay the trend strategy over years of past prices. What it tells you, and what it does not - It runs the SAME rules as the live bot (app/strategy.py) on historical candles, with the same sizing (10% of the account per buy, at most 4 positions) plus fees and slippage. - It compares the result with simply holding the same assets, reports the last 40% of the period separately (a rough stand-in for data the rules haven't "seen"), and re-runs with nearby settings to show whether the result depends on exact numbers. - It does NOT include the news filter (there is no free news archive), ignores currency moves (each asset's return in its own currency counts as the euro return), and skips the daily loss limit and the euro cap, which would only shrink the numbers without changing the picture. - A good backtest is necessary, not sufficient. Past results do not predict future ones. Command line: python -m app.backtest --interval 1d --years 5 --out result.json """ import argparse import json import math import time from bisect import bisect_right from dataclasses import asdict, replace from .config import INSTRUMENTS from .strategy import DEFAULT, Bars, Params, signal YEAR = 365.25 * 86400 MIN_CANDLES = 120 # skip assets with less history than this # ---------------------------------------------------------------- the simulation def simulate(data: dict, params: Params = DEFAULT, *, start_cash: float = 10_000.0, size_pct: float = 0.10, max_positions: int = 4, fee: float = 0.0015, slippage: float = 0.0005) -> dict: """Replay candles for several assets as one account. data: {symbol: [candle, ...]} oldest first; candles have ts, high, low, close, volume. A buy puts size_pct of the account into the asset (if fewer than max_positions are open). Prices are filled at the signal candle's close, worsened by `slippage`, minus `fee` per side. """ events = sorted(((c.ts, order, sym, c) for order, (sym, cs) in enumerate(data.items()) for c in cs), key=lambda e: (e[0], e[1])) bars = {sym: Bars() for sym in data} last: dict[str, float] = {} pos: dict[str, dict] = {} cash = start_cash trades, equity, exposure = [], [], [] def account_value() -> float: return cash + sum(p["units"] * last[s] for s, p in pos.items()) i = 0 while i < len(events): ts = events[i][0] while i < len(events) and events[i][0] == ts: _, _, sym, c = events[i] i += 1 b = bars[sym] b.add(c.high, c.low, c.close, c.volume) last[sym] = c.close if sym in pos: pos[sym]["bars"] += 1 act = signal(b, params) if act is None: continue action, reason = act if action == "buy": if len(pos) >= max_positions: continue # no room: the signal is skipped, as in the live bot amount = min(account_value() * size_pct, cash / (1 + fee)) if amount < 1: continue px = c.close * (1 + slippage) cost = amount * (1 + fee) cash -= cost pos[sym] = {"units": amount / px, "cost": cost, "entry_px": px, "entry_ts": ts, "bars": 0} b.in_position = True else: p = pos.pop(sym) px = c.close * (1 - slippage) net = p["units"] * px * (1 - fee) cash += net trades.append({"symbol": sym, "entry_ts": p["entry_ts"], "exit_ts": ts, "entry_px": p["entry_px"], "exit_px": px, "ret": net / p["cost"] - 1, "pnl": net - p["cost"], "bars": p["bars"], "reason": reason}) b.in_position = False b.trail = None value = account_value() equity.append((ts, value)) exposure.append(1 - cash / value if value > 0 else 0) open_positions = [{"symbol": s, "entry_ts": p["entry_ts"], "ret": p["units"] * last[s] / p["cost"] - 1} for s, p in pos.items()] return {"trades": trades, "equity": equity, "open": open_positions, "exposure": sum(exposure) / len(exposure) if exposure else 0.0} def hold_curve(data: dict, timeline: list[int], start_cash: float = 10_000.0) -> list[tuple[int, float]]: """Equal-weight buy-and-hold of the same assets, sampled at the same moments.""" ts_lists = {s: [c.ts for c in cs] for s, cs in data.items()} first = {s: cs[0].close for s, cs in data.items()} out = [] for ts in timeline: total = 0.0 for s, cs in data.items(): k = bisect_right(ts_lists[s], ts) - 1 total += (cs[k].close if k >= 0 else first[s]) / first[s] out.append((ts, start_cash * total / len(data))) return out # ---------------------------------------------------------------- numbers def _drawdown(values: list[float]) -> float: peak, worst = -math.inf, 0.0 for v in values: peak = max(peak, v) worst = min(worst, v / peak - 1) return worst * 100 def stats(equity: list, trades: list, exposure: float | None = None) -> dict: """Headline numbers for a stretch of the equity curve and the trades that ended in it.""" vals = [v for _, v in equity] years = (equity[-1][0] - equity[0][0]) / YEAR if len(equity) > 1 else 0 total = vals[-1] / vals[0] - 1 if vals else 0 wins = [t for t in trades if t["pnl"] > 0] losses = [t for t in trades if t["pnl"] <= 0] gross_win = sum(t["pnl"] for t in wins) gross_loss = -sum(t["pnl"] for t in losses) out = { "return_pct": round(total * 100, 1), "per_year_pct": round(((1 + total) ** (1 / years) - 1) * 100, 1) if years >= 0.5 and total > -1 else None, "max_drawdown_pct": round(_drawdown(vals), 1) if vals else 0.0, "trades": len(trades), "win_rate_pct": round(100 * len(wins) / len(trades), 1) if trades else None, "avg_win_pct": round(100 * sum(t["ret"] for t in wins) / len(wins), 2) if wins else None, "avg_loss_pct": round(100 * sum(t["ret"] for t in losses) / len(losses), 2) if losses else None, "profit_factor": round(gross_win / gross_loss, 2) if gross_loss > 0 else None, "years": round(years, 2), } if exposure is not None: out["time_invested_pct"] = round(exposure * 100, 1) return out def _segment(curve: list, lo: int, hi: int) -> list: return [(t, v) for t, v in curve if lo <= t <= hi] def _downsample(curve: list, n: int = 320) -> list: if len(curve) <= n: return curve step = len(curve) / n out = [curve[int(i * step)] for i in range(n)] out[-1] = curve[-1] return out # ---------------------------------------------------------------- the full report SENSITIVITY = [ ("Slow trend 10 / 30", {"fast": 10, "slow": 30}), ("Slow trend 34 / 89", {"fast": 34, "slow": 89}), ("Tighter stop (2x ATR)", {"atr_mult": 2.0}), ("Looser stop (4x ATR)", {"atr_mult": 4.0}), ("No volume filter", {"vol_mult": 0.0}), ("Stricter trend (ADX 25)", {"adx_min": 25}), ] def analyse(data: dict, params: Params = DEFAULT, *, sensitivity: bool = True, **costs) -> dict: """Run the backtest on already-downloaded candles and build the whole report.""" base = simulate(data, params, **costs) equity = base["equity"] if len(equity) < 2: raise ValueError("not enough history to test") start_cash = costs.get("start_cash", 10_000.0) hold = hold_curve(data, [t for t, _ in equity], start_cash) t0, t1 = equity[0][0], equity[-1][0] split = t0 + int(0.6 * (t1 - t0)) overall = stats(equity, base["trades"], base["exposure"]) benchmark = stats(hold, []) parts = {} for name, lo, hi in (("in_sample", t0, split), ("out_of_sample", split, t1)): seg_t = [t for t in base["trades"] if lo <= t["exit_ts"] <= hi] parts[name] = {"strategy": stats(_segment(equity, lo, hi), seg_t), "hold": stats(_segment(hold, lo, hi), []), "from": lo, "to": hi} by_symbol = [] for sym, cs in data.items(): tr = [t for t in base["trades"] if t["symbol"] == sym] wins = [t for t in tr if t["pnl"] > 0] by_symbol.append({ "symbol": sym, "exchange": INSTRUMENTS[sym].exchange if sym in INSTRUMENTS else "", "trades": len(tr), "win_rate_pct": round(100 * len(wins) / len(tr), 1) if tr else None, "avg_trade_pct": round(100 * sum(t["ret"] for t in tr) / len(tr), 2) if tr else None, "hold_pct": round((cs[-1].close / cs[0].close - 1) * 100, 1)}) sens = [] if sensitivity: for label, change in [("Settings used", {})] + SENSITIVITY: r = base if not change else simulate(data, replace(params, **change), **costs) st = stats(r["equity"], r["trades"]) sens.append({"label": label, "used": not change, "return_pct": st["return_pct"], "max_drawdown_pct": st["max_drawdown_pct"], "trades": st["trades"]}) result = { "generated": int(time.time()), "params": asdict(params), "assumptions": {"start_cash": start_cash, "size_pct": costs.get("size_pct", 0.10), "max_positions": costs.get("max_positions", 4), "fee_pct": costs.get("fee", 0.0015) * 100, "slippage_pct": costs.get("slippage", 0.0005) * 100}, "period": {"from": t0, "to": t1, "assets": len(data)}, "overall": overall, "benchmark": benchmark, **parts, "by_symbol": by_symbol, "sensitivity": sens, "open_positions": base["open"], "equity": [[t, round(v / equity[0][1] * 100, 2)] for t, v in _downsample(equity)], "hold_equity": [[t, round(v / hold[0][1] * 100, 2)] for t, v in _downsample(hold)], } result["verdict"] = verdict(result) return result def verdict(r: dict) -> list[str]: """Plain-language reading of the numbers. Says what they show, and what they cannot.""" o, b = r["overall"], r["benchmark"] out = [f"Over {o['years']:.1f} years the strategy returned {o['return_pct']:+.1f}% against " f"{b['return_pct']:+.1f}% for simply holding the same assets in equal parts. Its worst " f"drop from a peak was {o['max_drawdown_pct']:.1f}% against {b['max_drawdown_pct']:.1f}% for holding. " f"It was invested {o.get('time_invested_pct', 0):.0f}% of the time, so part of any gap is just " f"less exposure."] oos = r["out_of_sample"] out.append(f"On the last 40% of the period, the closest thing to an unseen test, it returned " f"{oos['strategy']['return_pct']:+.1f}% against {oos['hold']['return_pct']:+.1f}% for holding " f"(first 60%: {r['in_sample']['strategy']['return_pct']:+.1f}% against " f"{r['in_sample']['hold']['return_pct']:+.1f}%).") if r["sensitivity"]: variants = [s for s in r["sensitivity"] if not s["used"]] positive = sum(1 for s in variants if s["return_pct"] > 0) same_sign = positive in (0, len(variants)) out.append(f"{positive} of {len(variants)} nearby settings finished positive. " + ("The result does not hinge on the exact numbers." if same_sign else "The result flips with small changes to the settings, which is a warning sign " "that it may be luck rather than an edge.")) if o["trades"] < 30: out.append(f"Only {o['trades']} trades: too few for a firm conclusion either way.") out.append("Not included: the news filter (no free news archive exists), currency moves, the daily loss " "limit and the euro cap. Past results do not predict future ones.") return out # ---------------------------------------------------------------- downloading + running def run(fetch, symbols=None, interval: str = "1d", years: int = 5, params: Params = DEFAULT, sensitivity: bool = True, progress=None, **costs) -> dict: """Download history for each asset and analyse it. fetch(yahoo_symbol, interval, range_) must return (candles, currency), as marketdata.fetch does. Hourly history on Yahoo only goes back about two years, so hourly runs are capped at 2. """ range_ = "2y" if interval == "1h" else f"{max(1, int(years))}y" syms = [s for s in (symbols or INSTRUMENTS) if s in INSTRUMENTS] data, errors = {}, {} for n, sym in enumerate(syms, 1): try: candles, _ = fetch(INSTRUMENTS[sym].yahoo, interval, range_) if len(candles) < MIN_CANDLES: raise ValueError(f"only {len(candles)} candles of history") data[sym] = candles except Exception as e: errors[sym] = str(e)[:140] if progress: progress(n, len(syms)) if not data: raise RuntimeError("no price history could be downloaded: " + "; ".join(list(errors.values())[:3])) result = analyse(data, params, sensitivity=sensitivity, **costs) result.update({"interval": interval, "range": range_, "errors": errors}) return result def report(r: dict) -> str: """Plain-text version for the terminal.""" o, b = r["overall"], r["benchmark"] lines = [f"Backtest: {r['period']['assets']} assets, {r['interval']} candles, {o['years']:.1f} years", f" Strategy: {o['return_pct']:+.1f}% (per year {o['per_year_pct']}%) worst drop {o['max_drawdown_pct']:.1f}% " f"{o['trades']} trades win rate {o['win_rate_pct']}% profit factor {o['profit_factor']}", f" Holding: {b['return_pct']:+.1f}% (per year {b['per_year_pct']}%) worst drop {b['max_drawdown_pct']:.1f}%", "", "Settings check:"] lines += [f" {s['label']:<26} {s['return_pct']:+8.1f}% worst drop {s['max_drawdown_pct']:6.1f}% {s['trades']:>4} trades" for s in r["sensitivity"]] lines += ["", "By market:"] lines += [f" {s['symbol']:<8} {s['trades']:>3} trades avg {s['avg_trade_pct']}% hold {s['hold_pct']:+.1f}%" for s in r["by_symbol"]] lines += [""] + [f"- {v}" for v in r["verdict"]] if r.get("errors"): lines += ["", "Could not download: " + ", ".join(f"{k} ({v})" for k, v in r["errors"].items())] return "\n".join(lines) def main(): from . import marketdata ap = argparse.ArgumentParser(description="Replay the Yote-In-Vest strategy over past prices.") ap.add_argument("--interval", choices=["1d", "1h"], default="1d") ap.add_argument("--years", type=int, default=5, help="daily history length (hourly is capped at 2)") ap.add_argument("--symbols", help="comma-separated, default all") ap.add_argument("--no-sensitivity", action="store_true", help="skip the nearby-settings runs (faster)") ap.add_argument("--out", help="also write the full result as JSON") a = ap.parse_args() syms = [s.strip().upper() for s in a.symbols.split(",")] if a.symbols else None def progress(n, total): print(f"\rdownloading {n}/{total}", end="", flush=True) time.sleep(0.25) # be polite to Yahoo result = run(marketdata.fetch, syms, a.interval, a.years, sensitivity=not a.no_sensitivity, progress=progress) print("\r" + " " * 24 + "\r", end="") print(report(result)) if a.out: with open(a.out, "w") as f: json.dump(result, f) if __name__ == "__main__": main()