A clean Python CLI application that places Market, Limit, and Stop-Market orders on the Binance Futures Testnet. Built with a layered architecture, structured logging, and proper error handling.
trading_bot/
├── bot/
│ ├── __init__.py
│ ├── client.py # Binance API wrapper (signing, HTTP)
│ ├── orders.py # Order placement logic
│ ├── validators.py # Input validation
│ └── logging_config.py # Rotating file + console logger
├── logs/
│ └── bot.log # All requests, responses, errors
├── cli.py # CLI entry point (argparse)
├── .env.example # Credential template
├── .gitignore
├── requirements.txt
└── README.md
- Go to https://testnet.binancefuture.com
- Register or log in
- Navigate to API Management → click Generate
- Copy your API Key and Secret Key
git clone https://github.com/YOUR_USERNAME/trading-bot.git
cd trading-bot
# Create a virtual environment (recommended)
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txtcp .env.example .envOpen .env and fill in your keys:
BINANCE_API_KEY=your_testnet_api_key_here
BINANCE_API_SECRET=your_testnet_api_secret_here
⚠️ Never commit your.envfile. It is already in.gitignore.
python cli.py --symbol BTCUSDT --side BUY --type MARKET --qty 0.01python cli.py --symbol BTCUSDT --side SELL --type LIMIT --qty 0.01 --price 87000python cli.py --symbol ETHUSDT --side SELL --type STOP_MARKET --qty 0.1 --price 2000python cli.py --help─── Order Request Summary ───────────────────────
Symbol : BTCUSDT
Side : BUY
Type : MARKET
Quantity : 0.01
─────────────────────────────────────────────────
─── Order Response ───────────────────────────────
Order ID : 4799218156
Status : FILLED
Executed Qty : 0.010
Avg Price : 84321.10
Client Order : web_a1b2c3d4
─────────────────────────────────────────────────
✓ Order placed successfully!
All activity is logged to logs/bot.log:
- DEBUG — full request params and raw API response body
- INFO — order intent and outcome (orderId, status, executedQty)
- ERROR — API errors, network failures, validation failures
The console shows INFO and above. The log file captures everything (DEBUG+). Log files rotate at 5 MB and keep 3 backups.
| Scenario | Behaviour |
|---|---|
Missing --price on LIMIT order |
Clear validation error, exits with code 1 |
| Invalid symbol / negative quantity | Validation error before any API call |
| API key missing or wrong | Friendly message + logged error |
| Binance API error (e.g. insufficient margin) | Error code + message shown and logged |
| Network timeout / no internet | Friendly timeout message + logged |
- All trades use the USDT-M Futures Testnet (
testnet.binancefuture.com) - Quantity precision is passed as-is — Binance will reject if it violates the symbol's lot size filter on the testnet
timeInForcedefaults toGTC(Good Till Cancelled) for LIMIT orders- For STOP_MARKET orders,
--priceis used as thestopPrice - Credentials are loaded from
.envviapython-dotenv
- Python 3.8+
requests— HTTP clientpython-dotenv— loads.envfile
See requirements.txt for pinned versions.