A Python trading bot designed for extended.exchange that maintains a pseudo delta-neutral exposure by going long BTC perpetuals while shorting ETH perpetuals using a data-driven hedge ratio. The bot focuses on farming trading volume while attempting to control directional risk through continual rebalancing and stop-loss management.
- Pseudo Delta-Neutral Positioning – Long BTC and short ETH with automatic sizing driven by historical correlations.
- Data-Driven Hedge Ratio – Computes a minimum-variance hedge ratio from the last 365 days of daily returns.
- Persistent State – Persists hedge ratio, open position snapshots, and timestamps to
state.jsonfor seamless restarts. - Startup Reconciliation – Validates saved state against live exchange positions before trading begins.
- Configurable Stop-Loss – Automatically closes both legs when unrealised losses breach a configurable threshold.
- Graceful Shutdown – Intercepts
SIGINT/SIGTERM(e.g., Ctrl+C) and exits without closing positions to avoid unintended flattening.
- Python 3.11+
- Extended Exchange API key/secret with trading permissions
Install Python dependencies with:
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtThe bot reads configuration from environment variables or an optional JSON config file (pass via --config). Environment variable defaults are shown below:
| Variable | Description | Default |
|---|---|---|
EXTENDED_API_URL |
REST API base URL | https://api.extended.exchange |
EXTENDED_API_KEY |
API key | required for trading |
EXTENDED_API_SECRET |
API secret | required for trading |
BTC_SYMBOL |
BTC market symbol | BTC-PERP |
ETH_SYMBOL |
ETH market symbol | ETH-PERP |
BASE_POSITION_SIZE |
Target BTC size in contracts | 0.01 |
LOOKBACK_DAYS |
Candle lookback for hedge ratio | 365 |
REBALANCE_INTERVAL |
Seconds between rebalance cycles | 300 |
HEDGE_REFRESH_INTERVAL |
Seconds between hedge ratio refreshes | 3600 |
STOP_LOSS_PCT |
Max tolerated unrealised loss per leg (e.g. 0.08 = 8%) |
0.08 |
MIN_ORDER_SIZE |
Minimum trade size (prevents tiny orders) | 0.0001 |
POSITION_TOLERANCE |
Size tolerance before resyncing saved state | 1e-4 |
BOT_STATE_PATH |
Location of the persistent state file | state.json |
The JSON configuration file uses identical keys and overrides environment variables when supplied.
python bot.py --log-level INFODuring startup the bot:
- Loads configuration and the persisted
state.json(creating a new one if absent). - Recomputes the hedge ratio and reconciles saved position snapshots with on-exchange positions.
- Enters the main loop that periodically:
- Refreshes the hedge ratio on the configured cadence.
- Rebalances BTC/ETH positions to the pseudo delta-neutral target.
- Checks for stop-loss breaches and flattens both legs when triggered.
- Persists state to disk for restart resilience.
Stop the bot with Ctrl+C. Positions remain open; the bot saves the last known state and exits cleanly.
- This is not a perfect delta-neutral strategy. Correlations drift and the hedge ratio can change materially.
- Make sure your account has sufficient margin to support both legs and potential drawdowns.
- Always test on a demo environment (if available) before deploying with real capital.
- Review the Extended Exchange API documentation for authentication requirements and rate limits.
├── bot.py # CLI entry-point
├── extended_bot
│ ├── __init__.py # Package exports
│ ├── client.py # REST client wrapper
│ ├── config.py # Configuration dataclass + loaders
│ ├── hedge.py # Hedge ratio utilities
│ ├── state.py # State persistence helpers
│ └── strategy.py # Strategy orchestration
├── requirements.txt # Python dependencies
└── state.json # Created automatically at runtime
Happy hedging! 🛡️