NAME
emergency-exit-multi-exchange-killswitch — A single-button script that flattens all open positions and cancels all pending orders across 5+ major exchanges…
SYNOPSIS
pip install -rINFO
DESCRIPTION
A single-button script that flattens all open positions and cancels all pending orders across 5+ major exchanges simultaneously.
README
Emergency Exit Multi Exchange Killswitch
Production-ready Python CLI to cancel open orders and flatten positions across multiple exchanges in one coordinated emergency run.
What is implemented
- Multi-exchange orchestration with configurable concurrency
- Deterministic execution order: cancel first, flatten second, verify last
- Actions:
status,cancel-only,flatten-all - Exchange filtering (
--exchanges bybit,okx) - Adapter architecture:
mockadapter (fully testable, deterministic local runs)ccxtadapter (real exchange integration path)
- Residual risk verification and fail-fast thresholds (
max_residual_notional) - Safe live execution guard requiring explicit confirmation
- Structured run summary with per-exchange and total metrics
- Automated local test suite
Project structure
.
├── main.py # CLI entrypoint
├── config.py # Profile/config loader (JSON + optional YAML)
├── adapters.py # Exchange adapter interface + mock + ccxt adapter
├── killswitch.py # Orchestration engine + reporting formatter
├── models.py # Core typed data models
├── config.example.json # Working sample config
├── requirements.txt
└── test_killswitch.py # Unit/integration-style tests using mock adapter
How it works
- Load selected profile and runtime safety limits.
- Resolve enabled exchanges (or user-selected subset).
- For each exchange (concurrently):
- Fetch and cancel all open orders.
- If action is
flatten-all, fetch and close all open positions using reduce-only semantics. - Verify residual open orders and residual notional.
- Aggregate per-exchange results into a final status:
OKPARTIAL_FAILUREFAILED(for threshold/scope validation failures)
Installation
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
Quick start
1) Check profile and enabled exchanges
python3 main.py --action status --profile production --config config.example.json
2) Paper cancel-only run
python3 main.py --action cancel-only --mode paper --profile production --config config.example.json
3) Paper flatten-all run
python3 main.py --action flatten-all --mode paper --profile production --config config.example.json
4) Scope to selected exchanges
python3 main.py --action flatten-all --mode paper --profile production --config config.example.json --exchanges bybit,okx
5) Live mode safety confirmation
Live mode requires explicit confirmation:
python3 main.py --action flatten-all --mode live --confirm-live YES --profile production --config config.example.json
Or set:
export KILLSWITCH_LIVE_CONFIRM=YES
Configuration
The project supports profile-based config in JSON by default and YAML optionally (if PyYAML is installed).
Key runtime controls:
concurrency: parallel exchange workersverify_after_exit: enforce post-action checksmax_residual_notional: fail run if residual risk exceeds thresholdrequire_live_confirmation: guard live mode execution
Exchange controls:
enabled: include/exclude exchangeadapter:mockorccxtseed_data: deterministic test data for mock modeapi_key_env,api_secret_env,password_env: environment-variable mapping for ccxt adapter
Testing
Run tests directly:
python3 -m unittest -v
Current test coverage includes:
- Cancel-only flow
- Flatten-all flow
- Exchange scoping/filter behavior
Example output
==================================================================
UTC 2026-04-14 10:20:00 UTC
[killswitch] profile=production action=flatten-all mode=paper
[killswitch] status=OK
------------------------------------------------------------------
[binance] cancelled=2 closed=1 flattened_notional=3100.0000 residual_orders=0 residual_notional=0.0000
[bybit] cancelled=1 closed=1 flattened_notional=5220.0000 residual_orders=0 residual_notional=0.0000
[okx] cancelled=0 closed=0 flattened_notional=0.0000 residual_orders=0 residual_notional=0.0000
------------------------------------------------------------------
[result] cancelled_orders=3 positions_closed=2 notional_flattened=8320.0000 residual_open_orders=0 residual_notional=0.0000
==================================================================
Risk notice
This software can place live orders and close live positions. Always validate:
- Exchange/account mapping
- Position sizing and symbol conventions
- API credential scope
- Paper-mode behavior before live activation
Use at your own risk. This repository is infrastructure software, not financial advice.
Contact
- GitHub: @leionion