Quick Start
Persistence is enabled by default. When you callhz.run(name="my_strategy"), a database is created at ./my_strategy.db.
Configuration
| Method | Behavior |
|---|---|
db_path not specified | Uses HORIZON_DB env var, or ./{name}.db |
db_path="/path/to/db" | Uses the specified path |
db_path=None | Disables persistence entirely |
HORIZON_DB env var | Overrides the default path |
SQLite Schema
The database contains five tables:fills - Append-only fill journal
fills - Append-only fill journal
Source of truth for position reconstruction. Uses
INSERT OR IGNORE on fill_id for idempotent dedup.order_events - Order lifecycle log
order_events - Order lifecycle log
One row per status change (append-only).
position_snapshots - Periodic snapshots
position_snapshots - Periodic snapshots
Periodic snapshots for fast recovery. Each snapshot is a batch of positions with a shared timestamp.
risk_events - Risk event log
risk_events - Risk event log
Kill switch activations, deactivations, and violations.
strategy_runs - Run audit trail
strategy_runs - Run audit trail
Records strategy run start/end with exchange info.
Crash Recovery
Recovery happens automatically on startup when persistence is enabled:Replay fills
Fills from the
fills table with timestamp >= snapshot_timestamp are replayed to update positions.Engine Persistence Methods
Automatic Behavior
Whenhz.run() manages the engine:
| Event | Action |
|---|---|
| Startup | recover_state(): load snapshot + replay fills |
| Every 50 cycles | snapshot_positions(): save current positions |
| Shutdown | snapshot_positions() + end_run() |
| Engine drop | Final snapshot + end run + cancel all |
Performance
The database is configured for low-latency trading workloads:prepare_cached for prepared statement reuse.