EMERGENCY-EXIT-MULTI-EXCHANGE-KILLSWITCH(1)

NAME

emergency-exit-multi-exchange-killswitchA single-button script that flattens all open positions and cancels all pending orders across 5+ major exchanges…

SYNOPSIS

$pip install -r

INFO

35 stars
0 views

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:
    • mock adapter (fully testable, deterministic local runs)
    • ccxt adapter (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

  1. Load selected profile and runtime safety limits.
  2. Resolve enabled exchanges (or user-selected subset).
  3. 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.
  4. Aggregate per-exchange results into a final status:
    • OK
    • PARTIAL_FAILURE
    • FAILED (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 workers
  • verify_after_exit: enforce post-action checks
  • max_residual_notional: fail run if residual risk exceeds threshold
  • require_live_confirmation: guard live mode execution

Exchange controls:

  • enabled: include/exclude exchange
  • adapter: mock or ccxt
  • seed_data: deterministic test data for mock mode
  • api_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

SEE ALSO

clihub4/16/2026EMERGENCY-EXIT-MULTI-EXCHANGE-KILLSWITCH(1)