Repository navigation
v0.7.3 — okx_demo_flatten.sh pre-launch helper
Pre-launch position-cleanup helper for OKX demo trading.
What is in
tools/okx_demo_flatten.sh — operational helper that resets the OKX demo account to a known-clean state before starting spreadara. Stops the v0.7.1/.7.2 failure mode where a previous SIGKILL'd run leaves a position or open order behind, and the next startup trips the position_divergence circuit breaker on the first reconcile_now() call.
What it does, in order:
- Asserts
SPREADARA_OKX_API_KEY/_API_SECRET/_PASSPHRASEenv vars are set. - Queries
GET /api/v5/account/positions?instId=BTC-USDT-SWAP. - If any non-zero position exists →
POST /api/v5/trade/close-position(cross-margin, net mode). - Queries
GET /api/v5/trade/orders-pendingand cancels each. - Verifies: 0 positions, 0 open orders. Exits 0 on success, 1 on any anomaly.
Hardcoded safety: every call sends x-simulated-trading: 1. Cannot touch a live OKX account even if the env vars happen to hold production keys — the demo-only header makes OKX route to the simulated book.
Tunable via env vars (defaults shown):
SPREADARA_OKX_INST_ID=BTC-USDT-SWAP
SPREADARA_OKX_MGN_MODE=cross
Usage
./tools/okx_demo_flatten.sh && ./build/spreadara --config config/config.tomlThe && chain means spreadara only starts if the flatten succeeded. If OKX is unreachable or auth fails, you don't enter trading with an unknown exchange state.
Why this is the right shape
The position_divergence → notify_exception → CircuitBreaker.halted=true chain is correct behavior — a real mismatch between local and exchange state is the canonical "do not trade" condition (see Phase 3). The fix is not to weaken the divergence check; it's to ensure the operator starts from a known-clean state. This script automates that pre-flight.
Tests
No automated tests added — the script is a thin curl wrapper. Validated end-to-end against the live OKX demo endpoint during the v0.7.1/.7.2 validation session: positions closed cleanly, exit 0, subsequent spreadara boot showed local=0 exchange=0 → no halt.
Not in scope
Dashboard polish (bottom-row layout, EventsFeed header, footer version) and the [backtest] enabled = true / [testnet] enabled = true local config toggles remain uncommitted in the working tree as operator-side state. They will land in a separate commit if/when the user wants them in a release.