A low-latency market maker bot for cryptocurrency trading on Binance testnet.
This bot connects to Binance via WebSocket API to monitor order book changes and maintains two limit orders (bid and ask) at ±2% from the current mid-price. When the mid-price changes, it immediately cancels existing orders and places new ones.
Key Features:
- Real-time order book tracking via WebSocket
- Automatic bid/ask order placement and management
- Auto-reconnection on connection loss
- Low-latency order updates
- Comprehensive logging and performance metrics
- Server time synchronization
- Rate limiter with automatic throttling
| Metric | Value | Description |
|---|---|---|
| Decision latency | 0.104 ms | Time from receiving market data to making a trading decision. Lower values indicate faster reactions. |
| Processing latency | 0.775 ms | Time from receiving market data until order processing begins (includes decision time, queueing, and thread scheduling). |
| Order acknowledgment latency | 72.85 ms | Time from receiving market data to receiving confirmation from the exchange. |
| Component | Details |
|---|---|
| Instance Type | AWS EC2 t3.nano |
| vCPU / RAM | 2 CPUs / 0.5 GB |
| OS | Ubuntu 22.04 LTS |
| Network | Tokyo region, public IP |
| Test Duration | 2 hours of live market data |
| Symbol | SOLUSDT (Binance) |
Here are some sample performance plots generated by the bot:
- C++20 compiler (GCC 9+, Clang 10+, or MSVC 2019+)
- CMake 3.15+
- OpenSSL
- libcurl
- Boost (filesystem)
- libwebsockets
- Python 3.8+ (for data visualization)
Before building the project, copy config.json.example to config.json and update it with your own API credentials and settings:
cp config.json.example config.jsonThen edit config.json:
{
"api_key": "YOUR_API_KEY",
"api_secret": "YOUR_API_SECRET",
"rest_base_url": "https://testnet.binance.vision",
"ws_base_url": "stream.binance.com",
"log_level": "INFO",
"symbol": "BNBUSDT",
"enable_log_file": true,
"log_file_path": "logs/market_maker.log",
"enable_stat_log_file": true,
"stat_csv_file": "logs/market_maker.csv"
}Configuration Parameters:
api_key/api_secret- Binance API credentialsrest_base_url- REST API endpointws_base_url- WebSocket API endpointlog_level- Logging level (DEBUG/INFO/WARNING/ERROR)symbol- Trading pair (e.g., BNBUSDT, BTCUSDT)enable_log_file- Enable file logginglog_file_path- Path to log fileenable_stat_log_file- Enable CSV statistics loggingstat_csv_file- Path to CSV statistics file
# Install dependencies (Ubuntu/Debian)
sudo apt-get install cmake g++ libssl-dev libcurl4-openssl-dev libboost-filesystem-dev libwebsockets-dev
# Build
mkdir build && cd build
cmake ..
cmake --build .# Run from build directory
cd build
./bin/market_maker
# Or from project root after build
./build/bin/market_makerPress Ctrl+C to stop gracefully.
Visualize trading performance and metrics using Python:
# Generate performance plots
python scripts/plot_stats.py build/logs/market_maker.csv
Available Visualizations:
- Order latency over time
- Price spread analysis
- Order success/failure rates
- WebSocket reconnection events
src/
├── main.cpp # Entry point
├── OrderManager.cpp # Order management logic
├── OrderClient.cpp # Exchange API client
├── ExchangeClient.cpp # Exchange connectivity
├── WebSocketManager.cpp # WebSocket handling
├── WssClient.cpp # WebSocket client implementation
├── ExchangeInfo.cpp # Exchange metadata
├── Logger.cpp # Logging system
├── StatManager.cpp # Performance statistics
├── Config.cpp # Configuration loader
├── utils.cpp # Utility functions
└── strategies/ # Trading strategies
└── SimpleSpreadStrategy.cpp
scripts/
└── plot_stats.py # Visualization
- Initialization: Loads config, connects to Binance API, fetches exchange info
- WebSocket Connection: Subscribes to order book stream for the configured symbol
- Mid-Price Calculation: Calculates mid-price as
(best_bid + best_ask) / 2 - Order Placement: Places bid at
mid_price * 0.98and ask atmid_price * 1.02 - Price Monitoring: When mid-price changes, cancels existing orders and places new ones
- Auto-Reconnection: Automatically reconnects on WebSocket disconnection
The bot generates two types of logs:
-
Application Log (
logs/market_maker.log):- Order placement, cancellation, and updates
- WebSocket connection events
- Error messages and warnings
-
Statistics CSV (
logs/market_maker.csv):- Latency metrics
- Order execution times
- Performance statistics
This project is for educational and assessment purposes.


