guardrail/python-lab/scripts/export_drawdown_chart.py
caelum0x 2d2b15c7fe feat: full guardrail-alpha system + CLI modularization + Vercel deploy config
- Rust engine: real bootstrap/wiring preflight (guardrail-agent), execution layer (retry/router/pre-trade/post-trade/reconciliation), guardrail-doctor diagnostics with allowlist-drift guard.

- guardrail-cli: extracted pure helpers into util.rs (split in progress).

- 6 Track-2 skills + ensemble; python-lab analytics (regime/drawdown/montecarlo/dossier/ensemble/journal/seed); clients (ts/py/go/mcp/langchain/web-lite/proof-verifier); alert relay; scenarios; helm/k8s/compose; docs.

- dashboard/vercel.json + docs/VERCEL_DEPLOY.md for Vercel deployment.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-15 11:02:22 +03:00

75 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""Export the drawdown chart (percent below running NAV peak).
Renders a PNG when matplotlib is installed. When matplotlib is missing the
underlying (timestamp, drawdown_pct) series is written to CSV instead and a
notice is printed, so the script always succeeds.
Run from the repository root:
python3 python-lab/scripts/export_drawdown_chart.py
or from python-lab/:
python3 scripts/export_drawdown_chart.py
Optional positional argument overrides the database path::
python3 scripts/export_drawdown_chart.py [db_path]
Standard-library only (matplotlib is optional).
"""
import sys
from pathlib import Path
# Make ``guardrail_lab`` importable regardless of the current directory.
_LAB_ROOT = Path(__file__).resolve().parent.parent
if str(_LAB_ROOT) not in sys.path:
sys.path.insert(0, str(_LAB_ROOT))
from guardrail_lab import charts # noqa: E402
from guardrail_lab.db import load_events # noqa: E402
from guardrail_lab.metrics import drawdown_series # noqa: E402
DEFAULT_DB = "data/guardrail_alpha.db"
DRAWDOWN_PNG = "data/exports/drawdown.png"
DRAWDOWN_CSV = "data/exports/drawdown.csv"
def main() -> int:
db_path = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_DB
if not Path(db_path).exists():
print(f"export_drawdown_chart: no database at {db_path} (nothing to export)")
return 0
events = load_events(db_path)
series = drawdown_series(events)
if not series:
print("export_drawdown_chart: no portfolio_reconciled events found")
return 0
png = charts.plot_drawdown(events, DRAWDOWN_PNG)
if png is not None:
artifact = png
print(f"export_drawdown_chart: wrote PNG {artifact}")
else:
artifact = charts.write_drawdown_csv(events, DRAWDOWN_CSV)
print(
f"export_drawdown_chart: matplotlib unavailable, wrote CSV {artifact}"
)
drawdowns = [drawdown_pct for _, drawdown_pct in series]
max_dd = min(drawdowns) # most negative is the worst drawdown
worst_index = drawdowns.index(max_dd)
print(f" points: {len(series)}")
print(f" max drawdown: {max_dd:.2f}% @ {series[worst_index][0]}")
return 0
if __name__ == "__main__":
raise SystemExit(main())