A simplified Python trading bot for Binance Futures Testnet that supports market orders, limit orders, and stop-limit orders (bonus feature).
- ✅ Place market orders (BUY/SELL)
- ✅ Place limit orders (BUY/SELL)
- ✅ Binance Futures Testnet integration (USDT-M)
- ✅ Command-line interface with input validation
- ✅ Comprehensive logging (API requests, responses, errors)
- ✅ Error handling and validation
- ✅ Order status checking
- ✅ Order cancellation
- ✅ Account balance checking
- ✅ Stop-Limit orders
- ✅ Interactive CLI interface (enhanced UI)
- Python 3.7+ installed on your system
- Binance Testnet Account:
- Register at Binance Testnet
- Generate API credentials (API Key and Secret)
- Ensure your testnet account is activated
-
Clone or download this repository
-
Install dependencies:
pip install -r requirements.txt
The bot uses Binance Futures Testnet by default: https://testnet.binancefuture.com
You only need to enter your API keys once. Choose any of the options below:
-
Config file (recommended)
- Copy
config.example.json➜config.json - Fill in your
api_key,api_secret, and optionally set"testnet": true|false config.jsonis git-ignored so your keys stay local
- Copy
-
Environment variables
set BINANCE_API_KEY=your_key # Windows PowerShell set BINANCE_API_SECRET=your_secret set BINANCE_TESTNET=true # optional
-
CLI flags (one-off)
Pass--api-key/--api-secretwhen runningcli.py
If no credentials are found, the interactive CLI will prompt you for them.
python cli.py market BUY BTCUSDT 0.001python cli.py limit SELL BTCUSDT 0.001 50000python cli.py stop-limit BUY BTCUSDT 0.001 49000 49500python cli.py balancepython cli.py status BTCUSDT 12345678python cli.py cancel BTCUSDT 12345678Run the interactive interface:
python interactive_cli.pyYou'll be prompted to:
- Enter your API credentials (auto-loaded if config/env is set)
- Select from a menu of options
- Follow guided prompts for each operation
from config_loader import resolve_credentials
from trading_bot import BasicBot
creds = resolve_credentials()
bot = BasicBot(
api_key=creds["api_key"],
api_secret=creds["api_secret"],
testnet=creds["testnet"],
)
# Place a market order
order = bot.place_market_order(
symbol="BTCUSDT",
side="BUY",
quantity=0.001
)
# Place a limit order
order = bot.place_limit_order(
symbol="BTCUSDT",
side="SELL",
quantity=0.001,
price=50000
)
# Place a stop-limit order (bonus)
order = bot.place_stop_limit_order(
symbol="BTCUSDT",
side="BUY",
quantity=0.001,
price=49000,
stop_price=49500
)
# Check balance
balance = bot.get_account_balance()
# Get order status
status = bot.get_order_status("BTCUSDT", order_id=12345678)
# Cancel order
bot.cancel_order("BTCUSDT", order_id=12345678)Executes immediately at the current market price.
- Side: BUY or SELL
- Required: symbol, side, quantity
Executes only when the price reaches your specified limit price.
- Side: BUY or SELL
- Required: symbol, side, quantity, price
- Optional: time_in_force (GTC, IOC, FOK) - default: GTC
A stop-limit order combines a stop order with a limit order. When the stop price is reached, a limit order is placed.
- Side: BUY or SELL
- Required: symbol, side, quantity, price (limit), stop_price (trigger)
- Optional: time_in_force (GTC, IOC, FOK) - default: GTC
All API requests, responses, and errors are logged to:
- Console: Important messages (INFO level and above)
- Log Files: Detailed logs in
logs/trading_bot_YYYYMMDD.log
Log files are automatically created in the logs/ directory with daily rotation.
The bot includes comprehensive error handling:
- Input validation (symbol format, quantity, price)
- Binance API error handling
- Network error handling
- User-friendly error messages
assignment/
├── trading_bot.py # Main bot class
├── cli.py # Command-line interface
├── interactive_cli.py # Interactive CLI (bonus)
├── config_loader.py # Credential/config helpers
├── config.example.json # Copy to config.json with your keys
├── requirements.txt # Python dependencies
├── README.md # This file
└── logs/ # Log files (auto-created)
The code is organized for reusability and maintainability:
- BasicBot class: Core trading functionality
- Modular methods: Each order type has its own method
- Input validation: Comprehensive validation before API calls
- Logging: Detailed logging at each step
- Error handling: Try-catch blocks with specific error types
Before using with real funds, always test on Binance Testnet:
- Use testnet API credentials
- Verify orders are placed correctly
- Check logs for any issues
- Test all order types
- Never commit API keys to version control
- Use environment variables or secure credential storage in production
- This bot is configured for TESTNET only by default
- Always verify you're using testnet credentials
-
"Invalid API key"
- Verify your API credentials are correct
- Ensure your testnet account is activated
- Check that you're using testnet credentials, not mainnet
-
"Insufficient balance"
- Check your testnet account balance
- Request testnet funds if needed
-
"Invalid symbol"
- Ensure symbol is uppercase (e.g., BTCUSDT, not btcusdt)
- Verify the symbol exists on Binance Futures
-
Connection errors
- Check your internet connection
- Verify Binance Testnet is accessible
This project is created for the Junior Python Developer - Crypto Trading Bot assignment.
For questions or issues, please refer to the assignment submission guidelines.
Note: This bot is designed for educational purposes and testing on Binance Futures Testnet. Always test thoroughly before considering any production use.