This project implements a Delta Neutral Hedging Strategy on OKX for trading pairs BTC-USDT, ETH-USDT, and SOL-USDT. It simultaneously trades spot and perpetual swap contracts to hedge price risk while capturing funding rate differentials.
main.py is the entry point of the strategy. Its core responsibilities include:
-
Tracking Basis and Volatility:
Using the configured pairs, the script calculates the basis (price difference between spot and perpetual contracts) and tracks recent volatility and funding rates. -
Adaptive Position Sizing and Allocation:
It applies statistical methods to adaptively size positions based on historical basis and volatility, allocating capital across pairs proportionally. -
Asynchronous Funding Event Listener:
The script runs an async listener (funding_listener) that receives live funding rate updates via WebSocket. On each funding event, it:- Computes updated basis values.
- Evaluates if conditions meet thresholds to open delta-neutral positions (long spot, short perp).
- Opens hedged positions using the
open_positionfunction. - Logs all relevant events, errors, and state changes through a dual logger system (JSON and CSV).
-
Robust Logging & Error Handling:
The logger records info, warnings, and errors during runtime, enabling traceability and debugging.
- The script maintains a tracker for basis and funding rates per pair.
- Upon receiving funding rate updates, it calculates an adaptive threshold for basis.
- If the basis exceeds the threshold and no position is open for that pair, it allocates a size and opens a hedged position.
- Positions are sized based on confidence derived from the basis relative to volatility.
- The script continuously listens to funding updates and dynamically manages positions.
- Python 3.7+
- Dependencies:
numpy,asyncio, plus the custom modules (config.py,strategy.py,logger_setup.py,ws_client.py) - OKX API credentials stored securely (e.g.,
.envfile)