- python-lab/notebooks: 6 runnable offline-safe notebooks (closes the last plan.md gap). - docs: OpenAPI now covers all 56 routes (+/journal /ensemble /skills /version); new CLI.md + API.md; refreshed WHATS_NEW/PRIZE_MAP/FEATURE_MATRIX/PRODUCT_OVERVIEW/INDEX. - crates/strategy-ensemble: native Rust ensemble meta-allocator (blend by regime, 9 tests, clippy-clean), registered in workspace. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
345 lines
12 KiB
Plaintext
345 lines
12 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# 06 - Submission Charts\n",
|
|
"\n",
|
|
"The headline tables for the submission, assembled from the live\n",
|
|
"event log and run report: identity, regime time, drawdown summary,\n",
|
|
"and per-asset attribution. Everything reuses `guardrail_lab` and\n",
|
|
"renders as text/Markdown so it runs with the base stack.\n",
|
|
"\n",
|
|
"**Offline-safe:** every section degrades to a hint when data is\n",
|
|
"absent.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# --- Guardrail Alpha notebook bootstrap (offline-safe) ---\n",
|
|
"import sys\n",
|
|
"from pathlib import Path\n",
|
|
"\n",
|
|
"\n",
|
|
"def _find_repo_root(start: Path) -> Path:\n",
|
|
" \"\"\"Return the first ancestor that contains python-lab/guardrail_lab.\"\"\"\n",
|
|
" for candidate in [start, *start.parents]:\n",
|
|
" if (candidate / \"python-lab\" / \"guardrail_lab\").is_dir():\n",
|
|
" return candidate\n",
|
|
" return start\n",
|
|
"\n",
|
|
"\n",
|
|
"# In a notebook __file__ is undefined, so anchor on the current working dir.\n",
|
|
"REPO_ROOT = _find_repo_root(Path.cwd())\n",
|
|
"LAB_PATH = REPO_ROOT / \"python-lab\"\n",
|
|
"if str(LAB_PATH) not in sys.path:\n",
|
|
" sys.path.insert(0, str(LAB_PATH))\n",
|
|
"\n",
|
|
"\n",
|
|
"def _first_existing(*candidates: Path):\n",
|
|
" \"\"\"Return the first path that exists, else None.\"\"\"\n",
|
|
" for path in candidates:\n",
|
|
" if path.exists():\n",
|
|
" return path\n",
|
|
" return None\n",
|
|
"\n",
|
|
"\n",
|
|
"DATA_DIR = REPO_ROOT / \"data\"\n",
|
|
"CONFIG_DIR = REPO_ROOT / \"configs\"\n",
|
|
"\n",
|
|
"# Prefer a real run; fall back to the seeded demo artifacts if present.\n",
|
|
"DB_PATH = _first_existing(\n",
|
|
" DATA_DIR / \"guardrail_alpha.db\",\n",
|
|
" DATA_DIR / \"demo_guardrail_alpha.db\",\n",
|
|
")\n",
|
|
"RUN_REPORT_PATH = _first_existing(\n",
|
|
" DATA_DIR / \"run_report.json\",\n",
|
|
" DATA_DIR / \"demo_run_report.json\",\n",
|
|
")\n",
|
|
"EXPERIMENTS_DIR = DATA_DIR / \"experiments\"\n",
|
|
"BACKTESTS_DIR = DATA_DIR / \"backtests\"\n",
|
|
"ELIGIBLE_ASSETS_PATH = CONFIG_DIR / \"eligible_assets.bsc.json\"\n",
|
|
"ASSET_CATEGORIES_PATH = CONFIG_DIR / \"asset_categories.json\"\n",
|
|
"\n",
|
|
"NO_DATA_HINT = (\n",
|
|
" \"No data found under data/. Run the agent (paper/live) or seed a demo run \"\n",
|
|
" \"first, e.g. python3 -m guardrail_lab.seed (from python-lab/), then \"\n",
|
|
" \"re-run this notebook. The notebook is offline-safe and will not raise.\"\n",
|
|
")\n",
|
|
"\n",
|
|
"print(\"repo root :\", REPO_ROOT)\n",
|
|
"print(\"event log :\", DB_PATH if DB_PATH else \"(none yet)\")\n",
|
|
"print(\"run report:\", RUN_REPORT_PATH if RUN_REPORT_PATH else \"(none yet)\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def render_table(rows, columns, title=None):\n",
|
|
" \"\"\"Print an aligned text table. rows: list[dict]; columns: list[(key,label)].\"\"\"\n",
|
|
" if title:\n",
|
|
" print(title)\n",
|
|
" print(\"=\" * len(title))\n",
|
|
" if not rows:\n",
|
|
" print(\"(no rows)\")\n",
|
|
" return\n",
|
|
" labels = [label for _, label in columns]\n",
|
|
" keys = [key for key, _ in columns]\n",
|
|
" cells = [[(\"\" if r.get(k) is None else str(r.get(k))) for k in keys] for r in rows]\n",
|
|
" widths = [\n",
|
|
" max(len(labels[i]), *(len(row[i]) for row in cells)) for i in range(len(keys))\n",
|
|
" ]\n",
|
|
" header = \" \".join(labels[i].ljust(widths[i]) for i in range(len(keys)))\n",
|
|
" print(header)\n",
|
|
" print(\" \".join(\"-\" * widths[i] for i in range(len(keys))))\n",
|
|
" for row in cells:\n",
|
|
" print(\" \".join(row[i].ljust(widths[i]) for i in range(len(keys))))\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Load everything\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from guardrail_lab.db import load_events, event_counts\n",
|
|
"from guardrail_lab.loaders import load_run_report\n",
|
|
"from guardrail_lab.metrics import nav_series, max_drawdown, trade_count\n",
|
|
"from guardrail_lab.regime_analysis import time_in_regime\n",
|
|
"from guardrail_lab.drawdown import analyze_drawdown_from_events\n",
|
|
"from guardrail_lab.attribution import trade_attribution\n",
|
|
"\n",
|
|
"events = load_events(str(DB_PATH)) if DB_PATH else []\n",
|
|
"report = load_run_report(str(RUN_REPORT_PATH)) if RUN_REPORT_PATH else None\n",
|
|
"\n",
|
|
"if not events and report is None:\n",
|
|
" print(NO_DATA_HINT)\n",
|
|
"else:\n",
|
|
" print(f\"Events: {len(events)} | Run report: \"\n",
|
|
" f\"{'loaded' if report else '(none)'}\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Headline: agent identity & status\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"if report:\n",
|
|
" ident = [\n",
|
|
" {\"field\": \"run_id\", \"value\": report.get(\"run_id\", \"n/a\")},\n",
|
|
" {\"field\": \"mode\", \"value\": report.get(\"mode\", \"n/a\")},\n",
|
|
" {\"field\": \"wallet_address\", \"value\": report.get(\"wallet_address\", \"n/a\")},\n",
|
|
" {\"field\": \"policy_hash\", \"value\": report.get(\"policy_hash\", \"n/a\")},\n",
|
|
" {\"field\": \"kill_switch\", \"value\": report.get(\"kill_switch\", \"n/a\")},\n",
|
|
" {\"field\": \"starting_nav_usd\", \"value\": report.get(\"starting_nav_usd\", \"n/a\")},\n",
|
|
" {\"field\": \"nav_usd\", \"value\": report.get(\"nav_usd\", \"n/a\")},\n",
|
|
" {\"field\": \"total_drawdown_pct\", \"value\": report.get(\"total_drawdown_pct\", \"n/a\")},\n",
|
|
" ]\n",
|
|
" render_table(ident, [(\"field\", \"FIELD\"), (\"value\", \"VALUE\")],\n",
|
|
" title=\"Agent identity & status\")\n",
|
|
"else:\n",
|
|
" print(\"No run report available;\", NO_DATA_HINT)\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Headline: performance summary\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"if events:\n",
|
|
" curve = nav_series(events)\n",
|
|
" navs = [nav for _, nav in curve]\n",
|
|
" perf = []\n",
|
|
" if navs:\n",
|
|
" ret = (navs[-1] - navs[0]) / navs[0] * 100.0 if navs[0] else 0.0\n",
|
|
" perf.append({\"metric\": \"starting_nav_usd\", \"value\": f\"{navs[0]:,.2f}\"})\n",
|
|
" perf.append({\"metric\": \"latest_nav_usd\", \"value\": f\"{navs[-1]:,.2f}\"})\n",
|
|
" perf.append({\"metric\": \"total_return_pct\", \"value\": f\"{ret:+.3f}%\"})\n",
|
|
" perf.append({\"metric\": \"max_drawdown_pct\", \"value\": f\"{max_drawdown(navs) * 100.0:.3f}%\"})\n",
|
|
" perf.append({\"metric\": \"confirmed_trades\", \"value\": str(trade_count(events))})\n",
|
|
" render_table(perf, [(\"metric\", \"METRIC\"), (\"value\", \"VALUE\")],\n",
|
|
" title=\"Performance summary\")\n",
|
|
"else:\n",
|
|
" print(NO_DATA_HINT)\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Headline: time in regime\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"if events:\n",
|
|
" rows = time_in_regime(events)\n",
|
|
" if rows:\n",
|
|
" render_table(\n",
|
|
" [\n",
|
|
" {\n",
|
|
" \"regime\": t.regime,\n",
|
|
" \"classifications\": t.classifications,\n",
|
|
" \"share\": f\"{t.fraction:.1%}\",\n",
|
|
" \"seconds\": f\"{t.seconds:.1f}\",\n",
|
|
" }\n",
|
|
" for t in rows\n",
|
|
" ],\n",
|
|
" [\n",
|
|
" (\"regime\", \"REGIME\"),\n",
|
|
" (\"classifications\", \"COUNT\"),\n",
|
|
" (\"share\", \"SHARE\"),\n",
|
|
" (\"seconds\", \"SECONDS\"),\n",
|
|
" ],\n",
|
|
" title=\"Time in regime\",\n",
|
|
" )\n",
|
|
" else:\n",
|
|
" print(\"No regime classifications in the log yet.\")\n",
|
|
"else:\n",
|
|
" print(NO_DATA_HINT)\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Headline: drawdown summary\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"if events:\n",
|
|
" dd = analyze_drawdown_from_events(events, top_n=3)\n",
|
|
" if dd.points:\n",
|
|
" print(f\"Max drawdown: {dd.max_drawdown_pct:.4f}% \"\n",
|
|
" f\"(peak {dd.peak_timestamp or 'n/a'} -> trough {dd.trough_timestamp or 'n/a'})\")\n",
|
|
" if dd.episodes:\n",
|
|
" render_table(\n",
|
|
" [\n",
|
|
" {\n",
|
|
" \"depth\": f\"{ep.depth_pct:.4f}%\",\n",
|
|
" \"trough\": ep.trough_timestamp,\n",
|
|
" \"recovered\": ep.recovered,\n",
|
|
" }\n",
|
|
" for ep in dd.episodes\n",
|
|
" ],\n",
|
|
" [(\"depth\", \"DEPTH\"), (\"trough\", \"TROUGH_TS\"), (\"recovered\", \"RECOVERED\")],\n",
|
|
" title=\"Top drawdown episodes\",\n",
|
|
" )\n",
|
|
" else:\n",
|
|
" print(\"No drawdown episodes (no decline below a prior peak).\")\n",
|
|
" else:\n",
|
|
" print(\"No NAV history for drawdown analysis yet.\")\n",
|
|
"else:\n",
|
|
" print(NO_DATA_HINT)\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Headline: trade attribution summary\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"if events:\n",
|
|
" attribution = trade_attribution(events)\n",
|
|
" if attribution:\n",
|
|
" total_usd = sum(a[\"total_amount_usd\"] for a in attribution)\n",
|
|
" render_table(\n",
|
|
" [\n",
|
|
" {\n",
|
|
" \"symbol\": a[\"symbol\"],\n",
|
|
" \"trades\": a[\"count\"],\n",
|
|
" \"notional_usd\": f\"{a['total_amount_usd']:,.2f}\",\n",
|
|
" \"share\": f\"{(a['total_amount_usd'] / total_usd * 100.0):.1f}%\" if total_usd else \"n/a\",\n",
|
|
" }\n",
|
|
" for a in attribution\n",
|
|
" ],\n",
|
|
" [\n",
|
|
" (\"symbol\", \"SYMBOL\"),\n",
|
|
" (\"trades\", \"TRADES\"),\n",
|
|
" (\"notional_usd\", \"NOTIONAL_USD\"),\n",
|
|
" (\"share\", \"SHARE\"),\n",
|
|
" ],\n",
|
|
" title=\"Trade attribution\",\n",
|
|
" )\n",
|
|
" else:\n",
|
|
" print(\"No confirmed trades in the log yet.\")\n",
|
|
"else:\n",
|
|
" print(NO_DATA_HINT)\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Notes\n",
|
|
"\n",
|
|
"These tables are the offline-safe, dependency-free version of the\n",
|
|
"submission headline charts. For richer HTML/PNG output the\n",
|
|
"`guardrail_lab.submission` and `guardrail_lab.charts` modules build\n",
|
|
"on the same underlying functions when the optional plotting stack\n",
|
|
"is installed.\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"name": "python",
|
|
"version": "3.11"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|