A clean, production-ready Python CLI tool for placing and managing orders on the Binance USDT-M Futures Testnet.
| Feature | Detail |
|---|---|
| Order types | MARKET, LIMIT, STOP_MARKET (bonus), STOP (bonus) |
| Sides | BUY and SELL |
| CLI | argparse with full --help, subcommands, and validation messages |
| Logging | Rotating file (logs/trading_bot.log) + console; structured, non-noisy |
| Error handling | Validation errors, API errors (BinanceAPIError), network failures (BinanceNetworkError) |
| Code structure | Layered: client.py → orders.py → cli.py |
trading_bot/
├── bot/
│ ├── __init__.py
│ ├── client.py # Binance REST client (HMAC signing, HTTP, error handling)
│ ├── orders.py # Order business logic + response parsing
│ ├── validators.py # Input validation (raises ValueError on bad input)
│ └── logging_config.py # Rotating file + console logger setup
├── cli.py # CLI entry point (argparse subcommands)
├── logs/
│ └── trading_bot.log # Auto-created on first run
├── README.md
└── requirements.txt
- Go to https://testnet.binancefuture.com
- Log in (GitHub SSO available)
- Navigate to API Key → Generate a new key pair
- Save your API Key and Secret Key
git clone https://github.com/<your-username>/trading-bot.git
cd trading-bot
python3 -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -r requirements.txtexport BINANCE_API_KEY="your_testnet_api_key"
export BINANCE_API_SECRET="your_testnet_api_secret"On Windows (PowerShell):
$env:BINANCE_API_KEY = "your_testnet_api_key"
$env:BINANCE_API_SECRET = "your_testnet_api_secret"python cli.py place --symbol BTCUSDT --side BUY --type MARKET --qty 0.001Expected output:
── Order Request ──────────────────────────────
Symbol : BTCUSDT
Side : BUY
Type : MARKET
Quantity : 0.001
── Submitting… ────────────────────────────────
── Order Response ─────────────────────────────
Order ID : 3801259865
Status : FILLED
Symbol : BTCUSDT
Side : BUY
Type : MARKET
Orig Qty : 0.001
Exec Qty : 0.001
Avg Price : 65432.10
✓ Order placed successfully! (ID: 3801259865)
python cli.py place --symbol BTCUSDT --side SELL --type LIMIT --qty 0.001 --price 70000Use --tif IOC or --tif FOK to override the default GTC time-in-force.
python cli.py place --symbol BTCUSDT --side SELL --type STOP_MARKET --qty 0.001 --stop-price 60000python cli.py place --symbol BTCUSDT --side SELL --type STOP --qty 0.001 --price 59500 --stop-price 60000python cli.py accountpython cli.py order --symbol BTCUSDT --order-id 3801259865python cli.py cancel --symbol BTCUSDT --order-id 3801260001python cli.py --help
python cli.py place --helpAll runs append to logs/trading_bot.log (auto-created).
- Console →
INFOand above (clean, user-facing messages) - File →
DEBUGand above (full request bodies, responses, errors)
The file rotates at 5 MB and keeps 3 backups.
Sample log lines:
2025-07-14T09:12:02 | INFO | bot.client | Placing order | symbol=BTCUSDT side=BUY type=MARKET qty=0.001
2025-07-14T09:12:02 | DEBUG | bot.client | POST /fapi/v1/order body=symbol=BTCUSDT&...&signature=a1b2c3...
2025-07-14T09:12:02 | DEBUG | bot.client | HTTP 200 POST https://testnet.binancefuture.com/fapi/v1/order
2025-07-14T09:12:02 | INFO | bot.orders | Order placed | id=3801259865 status=FILLED execQty=0.001 avgPrice=65432.10
| Scenario | Behaviour |
|---|---|
| Missing credentials | Prints instructions, exits with code 1 |
| Invalid input (bad symbol, negative qty, missing price) | Prints validation message, exits with code 2 |
| API error (e.g. insufficient margin) | Prints Binance error code + message, exits with code 3 |
| Network failure / timeout | Prints error, exits with code 4; 3 automatic retries for 5xx errors |
- All orders use the USDT-M Futures Testnet (
https://testnet.binancefuture.com). positionSidedefaults toBOTH(one-way mode); hedge-mode is not supported.- Quantity and price precision are passed as-is; for production use, these should be rounded to the symbol's
LOT_SIZE/PRICE_FILTERfromGET /fapi/v1/exchangeInfo. - No
.envfile loader is included; credentials are read from environment variables only.
requests>=2.31.0
urllib3>=2.0.0
No Binance SDK needed — all API calls use direct HMAC-signed REST requests.