A Python CLI for trading on Binance Futures Testnet. Supports Market, Limit, and Stop-Market orders with request signing, input validation, and file logging.
- Python 3.9+
- Binance Futures Testnet account (free, no KYC)
- Go to https://testnet.binancefuture.com
- Sign up with GitHub (no KYC required)
- Click API Key in top navigation
- Click Generate and copy your API Key + Secret immediately
pip install -r requirements.txt-
Copy
.env.exampleto.env:cp .env.example .env
-
Edit
.envand add your credentials:BINANCE_API_KEY=your_api_key_here BINANCE_API_SECRET=your_api_secret_here
.env is in .gitignore — never commit it with real credentials
Test connectivity:
python cli.py --pingCheck account balance:
python cli.py --accountPlace a market order:
python cli.py --symbol BTCUSDT --side BUY --type MARKET --quantity 0.001Place a limit order:
python cli.py --symbol ETHUSDT --side SELL --type LIMIT --quantity 0.01 --price 3000Place a stop-market order:
python cli.py --symbol BTCUSDT --side BUY --type STOP_MARKET --quantity 0.001 --stop-price 70000bot/
├── client.py # Binance API client (HTTP, signing, error handling)
├── orders.py # Order builders and response formatting
├── validators.py # Input validation
└── logging_config.py # File and console logging
cli.py # CLI entry point
logs/ # Log files (auto-created)
Side : BUY
Type : MARKET
Quantity : 0.001
──────────────────────────────────────────────────
──────────────────────────────────────────────────
ORDER RESPONSE
──────────────────────────────────────────────────
Order ID : 8389765432
Client Order ID: x-abc123
Status : FILLED
Executed Qty : 0.001
Avg Price : 67432.10
Cum Quote : 67.43210
Update Time : 1717200001234
──────────────────────────────────────────────────
✓ Order placed successfully (status: FILLED)
python cli.py --symbol ETHUSDT --side SELL --type LIMIT --quantity 0.05 --price 3200python cli.py --symbol BTCUSDT --side BUY --type LIMIT --quantity 0.002 --price 60000 --tif IOCTriggers a market BUY when BTC price hits 70,000:
python cli.py --symbol BTCUSDT --side BUY --type STOP_MARKET --quantity 0.001 --stop-price 70000| Flag | Required | Description |
|---|---|---|
--symbol |
For orders | Trading pair, e.g. BTCUSDT |
--side |
For orders | BUY or SELL |
--type |
For orders | MARKET, LIMIT, or STOP_MARKET |
--quantity |
For orders | Order size (e.g. 0.001) |
--price |
LIMIT only | Limit price |
--stop-price |
STOP_MARKET only | Trigger price |
--tif |
LIMIT only | GTC (default), IOC, FOK |
--ping |
— | Test connectivity |
--account |
— | Show account balances |
--api-key |
If not set in env | Testnet API key |
--api-secret |
If not set in env | Testnet API secret |
--log-dir |
— | Log directory (default: logs/) |
--log-level |
— | DEBUG (default), INFO, WARNING, ERROR |
Log files are written to logs/trading_bot_YYYYMMDD.log automatically.
- File handler captures
DEBUGand above — includes raw request params and response bodies - Console handler shows
INFOand above — clean summary only - API signatures are redacted from logs for security
Sample log entry:
2026-06-01 10:10:28 | INFO | trading_bot.orders | Placing order: symbol=BTCUSDT side=BUY type=MARKET qty=0.001 price=N/A
2026-06-01 10:10:28 | DEBUG | trading_bot.client | → POST /fapi/v1/order params={...}
2026-06-01 10:10:28 | DEBUG | trading_bot.client | ← HTTP 200 body={...}
2026-06-01 10:10:28 | INFO | trading_bot.orders | Order response: {...}
The bot handles three categories of failures gracefully:
| Error Type | Example | Handling |
|---|---|---|
| Validation errors | Invalid side, missing price | Printed + logged, exit code 1 |
| Binance API errors | Invalid symbol, insufficient margin | BinanceAPIError raised, details printed |
| Network failures | Timeout, DNS failure | requests.RequestException caught and logged |
- Testnet only: The base URL is hardcoded to
https://testnet.binancefuture.com. Do not use real API keys. - USDT-M Futures: All orders target the USDT-M futures market (perpetual contracts).
- Quantity precision: The bot passes quantities as-is. If the exchange rejects due to precision/filter errors (e.g.
LOT_SIZE), adjust your quantity to match the symbol's step size shown inGET /fapi/v1/exchangeInfo. - No dependency on
python-binance: Uses onlyrequestsfor full transparency and testnet compatibility. - Python ≥ 3.9 is assumed for
dictmerge operators and type annotations.
The modules include inline validation that can be run directly:
# Run all validators
python -c "from bot.validators import *; print('Validators OK')"
# Check client signing logic
python -c "from bot.client import BinanceClient; c = BinanceClient('k', 's'); print('Client OK')"- Market orders (BUY + SELL)
- Limit orders (BUY + SELL) with timeInForce
- Stop-Market orders (bonus order type)
- Full input validation with clear error messages
- Structured code:
client.py/orders.py/validators.py/logging_config.py - Logging to file (DEBUG) and console (INFO)
- Exception handling: validation, API, network
-
requirements.txt - Log files from MARKET and LIMIT orders in
logs/