"""Backtester: simulation, benchmark, numbers and the report, on deterministic data.""" import pytest from app import backtest from app.config import INSTRUMENTS from tests.helpers import wave def test_drawdown_is_the_worst_fall_from_a_peak(): assert backtest._drawdown([100, 120, 60, 90]) == pytest.approx(-50.0) assert backtest._drawdown([100, 110, 120]) == 0.0 def test_first_buy_uses_ten_percent_of_the_account_and_costs_reduce_the_result(): free = backtest.simulate({"A": wave(700)}, fee=0, slippage=0) costly = backtest.simulate({"A": wave(700)}, fee=0.0015, slippage=0.0005) t = free["trades"][0] assert t["pnl"] / t["ret"] == pytest.approx(1000.0) # 10% of the 10,000 start assert costly["equity"][-1][1] < free["equity"][-1][1] assert len(free["trades"]) == len(costly["trades"]) >= 2 def test_position_limit_skips_signals_when_full(): twin = {"A": wave(700), "B": wave(700)} # identical, so both signal together one = backtest.simulate(twin, max_positions=1) two = backtest.simulate(twin, max_positions=2) assert {t["symbol"] for t in one["trades"]} == {"A"} assert {t["symbol"] for t in two["trades"]} == {"A", "B"} def test_hold_curve_is_an_equal_weight_average(): up = wave(10, drift=0, amp=0) for i, c in enumerate(up): # A doubles over the period up[i] = type(c)(c.ts, 0, 0, 100 * (1 + i / 9), 1) flat = [type(c)(c.ts, 0, 0, 50, 1) for c in up] # B stays put curve = backtest.hold_curve({"A": up, "B": flat}, [c.ts for c in up], 10_000) assert curve[0][1] == pytest.approx(10_000) and curve[-1][1] == pytest.approx(15_000) def three_assets(): return {"A": wave(700, phase=0.0), "B": wave(700, phase=1.3, drift=0.03), "C": wave(700, phase=2.6, amp=22.0)} def test_analyse_builds_the_whole_report(): r = backtest.analyse(three_assets()) assert r["overall"]["trades"] > 0 and r["period"]["assets"] == 3 assert r["in_sample"]["to"] == r["out_of_sample"]["from"] assert r["equity"][0][1] == 100 and r["hold_equity"][0][1] == 100 assert len(r["sensitivity"]) == 7 and sum(s["used"] for s in r["sensitivity"]) == 1 assert {s["symbol"] for s in r["by_symbol"]} == {"A", "B", "C"} assert r["sensitivity"][0]["return_pct"] == r["overall"]["return_pct"] # "settings used" row = headline text = " ".join(r["verdict"]) assert "Only" in text and "trades" in text # too few trades to conclude assert "news filter" in text and "do not predict" in text # the limits are always stated def test_analyse_without_sensitivity_and_with_too_little_data(): assert backtest.analyse(three_assets(), sensitivity=False)["sensitivity"] == [] with pytest.raises(ValueError): backtest.analyse({"A": wave(1)}) def test_run_downloads_each_market_and_records_failures(): asked = [] def fetch(sym, interval, range_): asked.append((sym, interval, range_)) if sym == "SPY": raise ConnectionError("rate limited") if sym == "SAP.DE": return wave(30), "EUR" # too little history return wave(700), "USD" seen = [] r = backtest.run(fetch, ["ASML", "SPY", "SAP", "BTCUSD", "NOT_A_TICKER"], "1d", 5, sensitivity=False, progress=lambda n, t: seen.append((n, t))) assert [a[0] for a in asked] == ["ASML.AS", "SPY", "SAP.DE", "BTC-USD"] # unknown ticker ignored assert all(a[1:] == ("1d", "5y") for a in asked) assert set(r["errors"]) == {"SPY", "SAP"} and r["period"]["assets"] == 2 assert "rate limited" in r["errors"]["SPY"] and "30 candles" in r["errors"]["SAP"] assert seen[-1] == (4, 4) def test_hourly_runs_ask_for_two_years_whatever_was_requested(): asked = [] backtest.run(lambda s, i, r: (asked.append(r), (wave(700), "USD"))[1], ["SPY"], "1h", 10, sensitivity=False) assert asked == ["2y"] def test_run_fails_clearly_when_nothing_downloads(): def fetch(*a): raise ConnectionError("blocked") with pytest.raises(RuntimeError, match="no price history"): backtest.run(fetch, ["SPY"], "1d", 5) def test_text_report_and_json_roundtrip(tmp_path): import json r = backtest.run(lambda s, i, rg: (wave(700), "USD"), ["SPY", "QQQ"], "1d", 5) text = backtest.report(r) assert "Backtest: 2 assets" in text and "Settings check:" in text and "By market:" in text p = tmp_path / "r.json" p.write_text(json.dumps(r)) assert json.loads(p.read_text())["overall"] == r["overall"] def test_every_configured_market_can_be_backtested(): # all tickers resolve to a Yahoo symbol, so the "run all" button has something to fetch for each assert all(i.yahoo for i in INSTRUMENTS.values())