162 lines
8.1 KiB
Python
162 lines
8.1 KiB
Python
"""Scorecard: how the bot has actually done so far, in plain numbers, with honest warnings.
|
|
|
|
Everything is worked out from what the Store has logged: executed trades, equity snapshots,
|
|
and the price of every market at every candle (for the forward look at skipped buys).
|
|
"""
|
|
from bisect import bisect_right
|
|
|
|
HORIZONS = (6, 24, 72) # candles ahead
|
|
MIN_TRADES = 30 # below this, win rate and profit factor mean little
|
|
|
|
|
|
def closed_trades(events: list[dict]) -> tuple[list[dict], list[dict]]:
|
|
"""Pair executed buys with the next executed sell of the same market. Events oldest first.
|
|
Returns (closed, still_open)."""
|
|
open_, closed = {}, []
|
|
for e in events:
|
|
if e["decision"] != "executed":
|
|
continue
|
|
if e["action"] == "buy":
|
|
info = e.get("order_info") or {}
|
|
cost = info.get("qty", 0) * e["price"] if "qty" in info else info.get("notional", 0)
|
|
open_[e["symbol"]] = {"symbol": e["symbol"], "buy_ts": e["ts"], "buy_price": e["price"],
|
|
"cost": cost, "fee": info.get("fee", 0)}
|
|
elif e["symbol"] in open_:
|
|
b = open_.pop(e["symbol"])
|
|
info = e.get("order_info") or {}
|
|
if "pnl" in info:
|
|
pnl = info["pnl"]
|
|
else: # broker gave no profit figure: use the prices
|
|
pnl = b["cost"] * (e["price"] / b["buy_price"] - 1)
|
|
closed.append({**b, "sell_ts": e["ts"], "sell_price": e["price"], "pnl": round(pnl, 2),
|
|
"ret_pct": round(pnl / b["cost"] * 100, 2) if b["cost"] else 0.0,
|
|
"why": e["reason"]})
|
|
return closed, list(open_.values())
|
|
|
|
|
|
def trade_stats(closed: list[dict]) -> dict:
|
|
n = len(closed)
|
|
wins = [t["pnl"] for t in closed if t["pnl"] > 0]
|
|
losses = [t["pnl"] for t in closed if t["pnl"] <= 0]
|
|
gross_win, gross_loss = sum(wins), -sum(losses)
|
|
return {
|
|
"trades": n, "wins": len(wins), "losses": len(losses),
|
|
"win_rate": round(len(wins) / n * 100, 1) if n else None,
|
|
"avg_win": round(gross_win / len(wins), 2) if wins else None,
|
|
"avg_loss": round(-gross_loss / len(losses), 2) if losses else None,
|
|
"profit_factor": round(gross_win / gross_loss, 2) if gross_loss else None,
|
|
"total_pnl": round(gross_win - gross_loss, 2),
|
|
"best": max((t["pnl"] for t in closed), default=None),
|
|
"worst": min((t["pnl"] for t in closed), default=None),
|
|
}
|
|
|
|
|
|
def max_drawdown(values: list[float]) -> float:
|
|
peak, worst = None, 0.0
|
|
for v in values:
|
|
peak = v if peak is None else max(peak, v)
|
|
if peak:
|
|
worst = min(worst, (v / peak - 1) * 100)
|
|
return round(worst, 2) + 0.0 if worst else 0.0
|
|
|
|
|
|
def buy_and_hold(prices: dict[str, list[tuple[float, float]]], since: float) -> float | None:
|
|
"""Average % change of all markets since `since` (equal weight). prices: symbol -> [(ts, eur)]."""
|
|
rets = []
|
|
for pts in prices.values():
|
|
pts = [p for p in pts if p[0] >= since]
|
|
if len(pts) >= 2 and pts[0][1] > 0:
|
|
rets.append((pts[-1][1] / pts[0][1] - 1) * 100)
|
|
return round(sum(rets) / len(rets), 2) if rets else None
|
|
|
|
|
|
def classify(e: dict) -> str:
|
|
if e["decision"] == "executed":
|
|
return "bought"
|
|
r = e["reason"] or ""
|
|
return "vetoed_by_news" if r.startswith(("bearish news", "news filter")) else "blocked_other"
|
|
|
|
|
|
def forward_outcomes(events: list[dict], prices: dict[str, list[tuple[float, float]]]) -> dict:
|
|
"""For every buy signal: what did the price do over the next 6/24/72 candles?
|
|
Grouped by what the bot did with it. Shows whether the news filter helps or hurts."""
|
|
out = {g: {h: [] for h in HORIZONS} for g in ("bought", "vetoed_by_news", "blocked_other")}
|
|
times = {s: [p[0] for p in pts] for s, pts in prices.items()}
|
|
for e in events:
|
|
if e["action"] != "buy" or e["decision"] == "error" or e["decision"] == "ignored":
|
|
continue
|
|
pts = prices.get(e["symbol"])
|
|
if not pts or not e["price"]:
|
|
continue
|
|
i = bisect_right(times[e["symbol"]], e["candle_ts"] or 0) - 1 # candle the signal fired on
|
|
if i < 0:
|
|
continue
|
|
for h in HORIZONS:
|
|
if i + h < len(pts):
|
|
out[classify(e)][h].append((pts[i + h][1] / pts[i][1] - 1) * 100)
|
|
res = {}
|
|
for g, byh in out.items():
|
|
res[g] = {str(h): {"n": len(v), "avg_pct": round(sum(v) / len(v), 2) if v else None,
|
|
"up_pct": round(sum(x > 0 for x in v) / len(v) * 100) if v else None}
|
|
for h, v in byh.items()}
|
|
return res
|
|
|
|
|
|
def build(store, start_cash: float) -> dict:
|
|
"""`start_cash` is the paper account's starting balance; deposits made since are added to it."""
|
|
start_cash = store.invested() if hasattr(store, "invested") else start_cash
|
|
events = list(reversed(store.events(100000)))
|
|
closed, open_ = closed_trades(events)
|
|
stats = trade_stats(closed)
|
|
snaps = [(r["ts"], r["equity"]) for r in store.q("SELECT ts, equity FROM equity_snapshots ORDER BY ts")]
|
|
prices: dict[str, list] = {}
|
|
for r in store.q("SELECT symbol, ts, price FROM price_log ORDER BY symbol, ts"):
|
|
prices.setdefault(r["symbol"], []).append((r["ts"], r["price"]))
|
|
|
|
since = snaps[0][0] if snaps else None
|
|
equity_now = snaps[-1][1] if snaps else start_cash
|
|
days = (snaps[-1][0] - snaps[0][0]) / 86400 if len(snaps) > 1 else 0.0
|
|
buys = [e for e in events if e["action"] == "buy" and e["decision"] == "executed"]
|
|
deployed = sum(((e.get("order_info") or {}).get("qty", 0) * e["price"]) or
|
|
(e.get("order_info") or {}).get("notional", 0) for e in buys)
|
|
bot_ret = round((equity_now / start_cash - 1) * 100, 2)
|
|
hold = buy_and_hold(prices, since) if since else None
|
|
|
|
warnings = []
|
|
if days < 30:
|
|
span = "less than a day" if days < 1 else f"{days:.0f} days"
|
|
warnings.append(f"Only {span} of history. A month is the very minimum, and even then "
|
|
"it covers one market mood.")
|
|
if stats["trades"] < MIN_TRADES:
|
|
warnings.append(f"Only {stats['trades']} finished trades. Win rate and profit factor need "
|
|
f"{MIN_TRADES}+ trades to mean much; right now luck can explain them.")
|
|
if hold is not None and since:
|
|
diff = bot_ret - hold
|
|
if diff < 0:
|
|
warnings.append(f"The bot is {-diff:.1f} points behind simply holding all markets equally. "
|
|
"It only invests part of the account, so in a rising market it trails by design; "
|
|
"the question is whether it loses less in a falling one.")
|
|
else:
|
|
warnings.append(f"The bot is {diff:.1f} points ahead of simply holding all markets equally. "
|
|
"Check whether that is skill or a falling market it sat out.")
|
|
fwd = forward_outcomes(events, prices)
|
|
veto_n = fwd["vetoed_by_news"]["24"]["n"]
|
|
if veto_n < 10:
|
|
warnings.append(f"Only {veto_n} news-vetoed buys have a 24-candle result so far; too few to say "
|
|
"whether the news filter helps.")
|
|
return {
|
|
"days": round(days, 1), "start_cash": start_cash, "equity": round(equity_now, 2),
|
|
"return_pct": bot_ret, "hold_return_pct": hold,
|
|
"max_drawdown_pct": max_drawdown([v for _, v in snaps]) if snaps else 0.0,
|
|
"deployed": round(deployed, 2),
|
|
"return_on_deployed_pct": round(stats["total_pnl"] / deployed * 100, 2) if deployed else None,
|
|
**{"stats": stats},
|
|
"open_trades": len(open_), "recent_trades": closed[-15:][::-1],
|
|
"signals": {"bought": sum(e["decision"] == "executed" for e in events if e["action"] == "buy"),
|
|
"vetoed_by_news": sum(classify(e) == "vetoed_by_news" for e in events
|
|
if e["action"] == "buy" and e["decision"] == "blocked"),
|
|
"blocked_other": sum(classify(e) == "blocked_other" for e in events
|
|
if e["action"] == "buy" and e["decision"] == "blocked")},
|
|
"forward": fwd, "warnings": warnings,
|
|
}
|