A Python CLI trading bot that places orders on Binance Futures Testnet (USDT-M). Built as part of the Primetrade.ai Python Developer application task.
- Place MARKET orders (fills immediately at current price)
- Place LIMIT orders (fills when price reaches your target)
- Place STOP-LIMIT orders (triggers a limit order at stop price) — Bonus
- Two modes of operation — Interactive menu and Headless CLI — Bonus
- Logs every action, request and error to a log file
- Validates all user input before touching the API
- Handles API errors and network failures gracefully
trading_bot/
bot/
__init__.py # Makes bot a Python package
client.py # Binance API connection wrapper
orders.py # Order placement logic
validators.py # Input validation
logging_config.py # Logger setup
ui_interactive.py # Step by step interactive menu
ui_headless.py # Direct terminal commands using Typer
cli.py # Main entry point
.env # API keys (never uploaded to GitHub)
.gitignore # Prevents sensitive files from being uploaded
requirements.txt # All dependencies
README.md # This file
logs/
trading_bot.log # Auto generated when bot runs
| Tool | Purpose |
|---|---|
| Python 3.x | Core language |
| python-binance | Binance Futures API wrapper |
| Typer (>=0.12) | Headless CLI commands |
| python-dotenv | Loads API keys from .env file |
| logging | Logs all actions to file and terminal |
git clone https://github.com/shivadixt/trading_bot.git
cd trading_bot
python -m venv venv
Activate it:
# Windows
venv\Scripts\activate
# Mac or Linux
source venv/bin/activate
pip install -r requirements.txt
- Go to https://testnet.binancefuture.com
- Register or Login
- Go to API Management
- Generate your API Key and Secret
- Copy both keys
Create a file called .env in the root of the project and add:
API_KEY=your_api_key_here
API_SECRET=your_api_secret_here
WARNING: Never share this file or upload it to GitHub. It is already added to .gitignore.
python cli.py
The bot will guide you through each step:
=============================================
Binance Futures Trading Bot
Running on Testnet — No Real Money
=============================================
Step 1 of 4 — Enter Trading Symbol
Examples: BTCUSDT, ETHUSDT, 1000SHIBUSDT
---------------------------------------------
Enter symbol: BTCUSDT
Step 2 of 4 — Choose Order Side
1. BUY
2. SELL
---------------------------------------------
Enter choice (1 or 2): 1
Step 3 of 4 — Choose Order Type
1. MARKET (fills immediately at current price)
2. LIMIT (fills when price reaches your target)
3. STOP-LIMIT (triggers a limit order at stop price)
---------------------------------------------
Enter choice (1, 2 or 3): 1
Step 4 of 4 — Enter Quantity
Example: 0.01
---------------------------------------------
Enter quantity: 0.01
Market Order:
python cli.py place-order --symbol BTCUSDT --side BUY --order-type MARKET --quantity 0.01
Limit Order:
python cli.py place-order --symbol BTCUSDT --side BUY --order-type LIMIT --quantity 0.01 --price 30000
Stop-Limit Order:
python cli.py place-order --symbol BTCUSDT --side BUY --order-type STOP_LIMIT --quantity 0.01 --price 30000 --stop-price 29000
Help Menu:
python cli.py --help
python cli.py place-order --help
------- Order Summary -------
Order ID : 123456789
Symbol : BTCUSDT
Side : BUY
Order Type : MARKET
Quantity : 0.01
Price : 0.0
Stop Price : 0.0
Status : FILLED
Executed Qty : 0.01
Avg Price : 30000.50
-----------------------------
Every action is automatically logged to logs/trading_bot.log
Log format:
2026-05-09 01:50:53 | orders | INFO | Placing MARKET order | Symbol: BTCUSDT | Side: BUY | Quantity: 0.01
2026-05-09 01:50:54 | orders | INFO | MARKET order placed successfully
Log levels used:
- DEBUG — Internal validation details
- INFO — Important events like orders placed
- ERROR — Something went wrong
- EXCEPTION — Full traceback for unexpected errors
The bot validates every input before touching the API:
| Input | Validation |
|---|---|
| Symbol | Must be letters and numbers only. Example: BTCUSDT, 1000SHIBUSDT |
| Side | Must be BUY or SELL only |
| Order Type | Must be MARKET, LIMIT or STOP_LIMIT only |
| Quantity | Must be a positive number greater than zero |
| Price | Must be a positive number, required for LIMIT and STOP_LIMIT |
| Stop Price | Must be a positive number, required for STOP_LIMIT only |
If any input is wrong the bot tells you exactly what is wrong and asks you to try again — it never crashes.
| Error Type | How We Handle It |
|---|---|
| Invalid user input | Caught by validators, user is asked to try again |
| Binance API rejection | Caught as BinanceAPIException, clean message shown |
| Network failure | Caught as general Exception, logged and shown |
| Missing API keys | Caught at startup, clear message shown |
Added as a third order type on top of MARKET and LIMIT. Available in both interactive and headless modes.
Two modes of operation:
- Interactive mode — step by step guided menu with confirmation screen
- Headless mode — single line terminal commands using Typer
The assignment listed argparse, Typer and Click as options. We chose Typer because:
- Much cleaner and more readable code
- Auto generates beautiful help menus
- Colored error messages out of the box
- Modern standard for Python CLI apps
We separated the two modes into ui_interactive.py and ui_headless.py to keep the code clean and maintainable. Each file has one responsibility. cli.py just decides which one to launch.
All validation logic lives in one place. If any validation rule needs to change, only one file needs to be updated.
The Binance connection is wrapped in its own file so if the API library ever changes, we only update one file.
- Bot runs on Binance Futures Testnet only — no real money involved
- Minimum quantity follows Binance Testnet rules for each symbol
- STOP_LIMIT orders require both a limit price and a stop price
- STOP_LIMIT orders use the Binance Algo Order API which requires a minimum notional of $20
- GTC (Good Till Cancelled) is used as default timeInForce for LIMIT and STOP_LIMIT orders
- API keys must be generated from https://testnet.binancefuture.com specifically
Sample log files from test runs are included in the logs/ folder:
- One MARKET order log
- One LIMIT order log