Small Python CLI that places MARKET and LIMIT orders on Binance USDT-M Futures Testnet, with structured layers, file logging, and input validation.
- MARKET and LIMIT orders (BUY / SELL)
- Bonus: STOP_LIMIT orders (
--type STOP_LIMIT+--stop-price) - Typer CLI with clear request/response output
- Interactive menu:
python cli.py menu - Layered design:
client.py(API),orders.py(business logic),validators.py,cli.py - Logs API requests, responses, and errors to
logs/trading_bot.log
- Register at Binance Futures Testnet and create API key + secret.
- Python 3.10+ recommended.
- Fund testnet wallet if required for your symbol.
cd trading_bot
python -m venv .venv
# Windows
.venv\Scripts\activate
# macOS/Linux
# source .venv/bin/activate
pip install -r requirements.txt
copy .env.example .env # Windows
# cp .env.example .env # macOS/LinuxEdit .env:
BINANCE_API_KEY=your_testnet_key
BINANCE_API_SECRET=your_testnet_secret
BINANCE_FUTURES_BASE_URL=https://testnet.binancefuture.comFrom the trading_bot directory:
python cli.py place --symbol BTCUSDT --side BUY --type MARKET --quantity 0.001python cli.py place --symbol BTCUSDT --side SELL --type LIMIT --quantity 0.001 --price 95000Use a limit price away from the market if you only want the order accepted (status NEW) without immediate fill.
python cli.py place --symbol BTCUSDT --side SELL --type STOP_LIMIT --quantity 0.001 --price 94000 --stop-price 94500python cli.py menupython cli.py --help
python cli.py place --helpThe CLI prints:
- Order request summary (symbol, side, type, quantity, price)
- Order response details (
orderId,status,executedQty,avgPrice, …) - Success or Failure panel
All API traffic is also written to logs/trading_bot.log.
trading_bot/
bot/
config.py # Env vars, constants
models.py # OrderRequest, OrderResult dataclasses
validators.py # Input validation
client.py # Signed REST client (httpx)
orders.py # OrderService (business logic)
display.py # CLI formatting
logging_config.py
exceptions.py
cli.py # Typer entry point (thin layer)
tests/ # Unit tests
logs/ # Runtime + sample logs
requirements.txt
pyproject.toml
.env.example
README.md
- One-way position mode (default
positionSide=BOTH; not sent explicitly). - USDT-M pairs only; symbols must end with
USDT. - LIMIT orders use
timeInForce=GTC. newOrderRespType=RESULTso MARKET fills returnexecutedQty/avgPricewhen available.- Minimum quantity/step size is not pre-checked locally; Binance may reject invalid precision (error surfaced as API error).
- Testnet base URL is
https://testnet.binancefuture.comas specified in the task brief.
| Situation | Exit code | User message |
|---|---|---|
| Invalid CLI input | 1 | Validation failure |
| Missing API keys (.env) | 2 | Configuration error |
| Network / timeout | 3 | Network error |
| Binance API error | 4 | API error |
| Other bot errors | 5 | Generic failure |
Sample logs from successful testnet runs are included:
logs/market_order_example.log— MARKET BUY on BTCUSDTlogs/limit_order_example.log— LIMIT SELL on BTCUSDT
After you run live orders, your own entries append to logs/trading_bot.log.
MIT — for evaluation submission only.