A production-ready Python trading bot for Binance Futures Testnet with an interactive CLI featuring enhanced UX — color-coded output, live validation, spinners, and a polished menu system.
- Market & Limit Orders — Place orders with guided, step-by-step prompts
- Interactive CLI — Rich panels, tables, spinners, and color-coded output
- Inline Validation — Real-time feedback on every input (symbol, quantity, price)
- Order History — View session orders and fetch history from Binance
- Account Balance — Check your testnet account balance
- Comprehensive Logging — Rotating file logs + colored console output
- Error Handling — Graceful handling of network, API, and input errors with retry logic
- Secure Credentials — API keys stored in
.env, masked in logs (only last 4 chars shown)
trading_bot/
├── bot/
│ ├── __init__.py # Package init, version, exports
│ ├── client.py # Binance API client (httpx + HMAC signing)
│ ├── orders.py # Order placement & management logic
│ ├── validators.py # Input validation (symbol, qty, price)
│ ├── logging_config.py # Dual logging setup (file + console)
│ └── models.py # Data models (OrderRequest, OrderResponse, enums)
├── cli.py # Interactive CLI entry point
├── README.md # This file
├── requirements.txt # Python dependencies
├── .env.example # Environment variable template
└── logs/
└── trading_bot.log # Auto-generated log file
cd "Trading Bot"python -m venv venv
# Windows
venv\Scripts\activate
# macOS/Linux
source venv/bin/activatepip install -r requirements.txt- Register on Binance Futures Testnet
- Generate API credentials (API Key & Secret)
- Create your
.envfile:
copy .env.example .env- Edit
.envand add your credentials:
BINANCE_API_KEY=your_actual_api_key_here
BINANCE_API_SECRET=your_actual_api_secret_here
BINANCE_TESTNET=true
⚠️ Never commit your.envfile to version control!
python cli.pyYou'll see the interactive menu:
╔════════════════════════════════════════════╗
║ BINANCE FUTURES TRADING BOT ║
║ Testnet Mode 🔬 ║
╚════════════════════════════════════════════╝
? Select an action:
📈 Place Market Order
📊 Place Limit Order
📋 View Order History
💰 Check Account Balance
⚙️ Settings
🚪 Exit
- Select "Place Market Order" from the menu
- Enter symbol:
BTCUSDT→ ✅ Symbol found - Select side:
BUY (Long)orSELL (Short) - Enter quantity:
0.001→ ✅ Quantity validated - Confirm the order → Press
y - View the result with Order ID, status, and execution details
Same flow as above, plus:
- You'll see the current market price as a reference
- Enter your desired limit price
- The bot validates against Binance tick size rules
- See all orders placed in the current session
- Optionally fetch historical orders from Binance for any symbol
- View all non-zero asset balances
- Shows available balance and unrealized PnL
All bot activity is logged to logs/trading_bot.log:
[2024-01-15 10:30:45] INFO - Starting Binance Futures Trading Bot
[2024-01-15 10:30:46] INFO - Initializing Binance client (TESTNET) | API Key: ****abcd
[2024-01-15 10:30:47] INFO - Loaded 287 trading symbols
[2024-01-15 10:30:48] INFO - User Input: symbol=BTCUSDT, side=BUY, type=MARKET, qty=0.001
[2024-01-15 10:30:48] DEBUG - Validating inputs...
[2024-01-15 10:30:48] DEBUG - Inputs validated successfully
[2024-01-15 10:30:49] INFO - Placing order: Symbol: BTCUSDT | Side: BUY | Type: MARKET | Quantity: 0.001
[2024-01-15 10:30:50] INFO - Order placed successfully! ID: 1234567890 | Status: FILLED
Security: Full API keys are never logged — only the last 4 characters are shown.
| Package | Purpose |
|---|---|
httpx |
HTTP client for Binance API calls |
python-dotenv |
Load credentials from .env file |
rich |
Terminal UI (panels, tables, spinners, colors) |
questionary |
Interactive prompts with validation |
All signed requests use HMAC-SHA256:
- Add
timestampparameter (current Unix time in ms) - Create query string from all parameters
- Sign with your API secret using SHA-256
- Append
signatureto the request
| Error Type | Handling |
|---|---|
| Network timeout | Retry with exponential backoff (1s, 2s, 4s) |
| HTTP 5xx | Retry up to 3 times |
| HTTP 429 (rate limit) | Wait for Retry-After header duration |
| API errors (4xx) | Display error code + message with helpful hints |
| Invalid input | Caught before API call with inline feedback |
| Endpoint | Method | Purpose |
|---|---|---|
/fapi/v1/exchangeInfo |
GET | Symbol info & trading rules |
/fapi/v1/ticker/price |
GET | Current market price |
/fapi/v1/order |
POST | Place order |
/fapi/v1/allOrders |
GET | Order history |
/fapi/v1/openOrders |
GET | Open orders |
/fapi/v2/balance |
GET | Account balance |
- Testnet Only — This bot is configured for Binance Futures Testnet by default
- No Live Trading — The settings menu shows testnet mode but does not enable live switching for safety
- API Keys — Must be stored in a
.envfile; never hardcoded - Quantity Validation — Based on Binance's LOT_SIZE filter from exchangeInfo
- Session History — Order history within the session is stored in-memory only
- Single-threaded — The bot runs synchronously; no background order monitoring
- Bot starts and connects to testnet
- Exchange info loads successfully
- Market BUY order for BTCUSDT executes
- Limit SELL order for ETHUSDT executes
- Invalid symbol shows error with suggestions
- Invalid quantity shows error with hints
- Order history displays correctly
- Account balance shows non-zero assets
- Log file contains proper entries
- API keys are masked in logs
This project is for educational and testing purposes only. Use at your own risk. Never trade with real funds using code that hasn't been thoroughly tested and audited.