import json import pytest from fastapi.testclient import TestClient from app import main, scorecard from app.store import Store def ev(ts, symbol, action, price, decision="executed", reason="", info=None, candle_ts=0): return dict(ts=ts, symbol=symbol, action=action, price=price, decision=decision, reason=reason, order_info=info, candle_ts=candle_ts) def test_trades_are_paired_and_summarised(): events = [ ev("1", "A", "buy", 100, info={"qty": 1.0}), ev("2", "A", "sell", 110, info={"pnl": 9.5}, reason="atr_stop"), ev("3", "B", "buy", 50, info={"qty": 2.0}), ev("4", "B", "sell", 40, info={"pnl": -20.3}), ev("5", "C", "buy", 10, info={"notional": 100}), # still open ev("6", "D", "buy", 10, decision="blocked"), # never bought ] closed, open_ = scorecard.closed_trades(events) assert [t["pnl"] for t in closed] == [9.5, -20.3] and len(open_) == 1 assert closed[0]["ret_pct"] == 9.5 st = scorecard.trade_stats(closed) assert st["win_rate"] == 50.0 and st["profit_factor"] == pytest.approx(0.47, abs=0.01) assert st["total_pnl"] == -10.8 assert scorecard.trade_stats([])["win_rate"] is None def test_sell_without_profit_figure_uses_prices(): closed, _ = scorecard.closed_trades([ev("1", "X", "buy", 100, info={"notional": 200}), ev("2", "X", "sell", 90, info={})]) assert closed[0]["pnl"] == -20.0 def test_drawdown_and_buy_and_hold(): assert scorecard.max_drawdown([100, 120, 60, 90]) == -50.0 assert scorecard.max_drawdown([]) == 0.0 prices = {"A": [(1, 100), (2, 120)], "B": [(1, 10), (2, 9)], "C": [(1, 5)]} assert scorecard.buy_and_hold(prices, 1) == 5.0 # (+20% and -10%) / 2; C has too little assert scorecard.buy_and_hold(prices, 99) is None def test_forward_outcomes_split_by_what_the_bot_did(): prices = {"A": [(t, 100 + t) for t in range(100)], "B": [(t, 200 - t) for t in range(100)]} events = [ ev("1", "A", "buy", 100, candle_ts=10), ev("2", "B", "buy", 200, "blocked", "bearish news (80%): x", candle_ts=10), ev("3", "A", "buy", 100, "blocked", "max 4 open positions", candle_ts=10), ev("4", "A", "sell", 100, candle_ts=10), # sells are ignored ev("5", "A", "buy", 100, "blocked", "x", candle_ts=95), # no future candles yet ] f = scorecard.forward_outcomes(events, prices) assert f["bought"]["6"] == {"n": 1, "avg_pct": pytest.approx(5.45, abs=0.01), "up_pct": 100} assert f["vetoed_by_news"]["24"]["avg_pct"] < 0 and f["vetoed_by_news"]["24"]["up_pct"] == 0 assert f["blocked_other"]["72"]["n"] == 1 assert f["blocked_other"]["6"]["n"] == 1 # the late signal has no 6-ahead result def test_classify(): assert scorecard.classify(ev("", "A", "buy", 1, "blocked", "news filter unavailable, x")) == "vetoed_by_news" assert scorecard.classify(ev("", "A", "buy", 1, "blocked", "NYSE is closed")) == "blocked_other" def test_build_on_an_empty_store_warns_instead_of_crashing(tmp_path): s = Store(str(tmp_path / "e.db"), 10_000) r = scorecard.build(s, 10_000) assert r["stats"]["trades"] == 0 and r["return_pct"] == 0.0 and r["equity"] == 10_000 assert any("finished trades" in w for w in r["warnings"]) assert any("of history" in w for w in r["warnings"]) def test_build_from_logged_history(tmp_path): s = Store(str(tmp_path / "h.db"), 10_000) for i in range(40): s.log_price("AAA", 1000 + i * 3600, 100 + i) s.log_equity(1000 + i * 3600, 10_000 + i * 10) s.log("AAA", "buy", 105, "executed", "r", order_info={"qty": 1.0}, candle_ts=1000 + 5 * 3600) s.log("AAA", "sell", 120, "executed", "r", order_info={"pnl": 14.0}, candle_ts=1000 + 20 * 3600) r = scorecard.build(s, 10_000) assert r["stats"]["trades"] == 1 and r["return_pct"] == pytest.approx(3.9) assert r["hold_return_pct"] == pytest.approx(39.0) assert r["max_drawdown_pct"] == 0.0 assert r["forward"]["bought"]["24"]["n"] == 1 assert any("behind simply holding" in w for w in r["warnings"]) s.clear_history() assert scorecard.build(s, 10_000)["stats"]["trades"] == 0 def test_old_database_is_upgraded(tmp_path): import sqlite3 p = str(tmp_path / "old.db") c = sqlite3.connect(p) c.execute("CREATE TABLE events (id INTEGER PRIMARY KEY AUTOINCREMENT, ts TEXT NOT NULL, symbol TEXT, " "action TEXT, price REAL, decision TEXT, reason TEXT, sentiment TEXT, order_info TEXT)") c.commit(); c.close() s = Store(p, 100) s.log("A", "buy", 1, "blocked", "x") assert s.events(1)[0]["candle_ts"] is not None def test_api_scorecard_and_backtest(tmp_path, monkeypatch): from tests.helpers import wave c = TestClient(main.app) assert c.get("/api/scorecard").status_code == 200 assert c.get("/api/backtest").json()["running"] is False assert c.post("/api/backtest/run", json={"secret": "wrong"}).status_code == 401 assert c.post("/api/backtest/run", json={"secret": "s3cret", "interval": "5m"}).status_code == 422 monkeypatch.setattr(main, "BACKTEST_FILE", tmp_path / "bt.json") main.bt.update(running=True, done=0, total=0, error="") assert c.post("/api/backtest/run", json={"secret": "s3cret"}).status_code == 409 main._run_backtest("1d", 5, fetch=lambda s, i, r: (wave(700), "USD")) got = c.get("/api/backtest").json() assert got["running"] is False and got["error"] == "" and got["result"]["overall"]["trades"] > 0 assert got["done"] == got["total"] == len(main.INSTRUMENTS) def broken(*a): raise ConnectionError("offline") main._run_backtest("1d", 5, fetch=broken) assert "no price history" in c.get("/api/backtest").json()["error"]