Skip to content

Sahil-aka/assignment

Repository files navigation

Crypto Trading Bot - Binance Futures Testnet

A simplified Python trading bot for Binance Futures Testnet that supports market orders, limit orders, and stop-limit orders (bonus feature).

Features

Core Requirements ✅

  • ✅ 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

Bonus Features 🎁

  • ✅ Stop-Limit orders
  • ✅ Interactive CLI interface (enhanced UI)

Prerequisites

  1. Python 3.7+ installed on your system
  2. Binance Testnet Account:
    • Register at Binance Testnet
    • Generate API credentials (API Key and Secret)
    • Ensure your testnet account is activated

Installation

  1. Clone or download this repository

  2. Install dependencies:

    pip install -r requirements.txt

Configuration

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:

  1. Config file (recommended)

    • Copy config.example.jsonconfig.json
    • Fill in your api_key, api_secret, and optionally set "testnet": true|false
    • config.json is git-ignored so your keys stay local
  2. Environment variables

    set BINANCE_API_KEY=your_key          # Windows PowerShell
    set BINANCE_API_SECRET=your_secret
    set BINANCE_TESTNET=true              # optional
  3. CLI flags (one-off)
    Pass --api-key / --api-secret when running cli.py

If no credentials are found, the interactive CLI will prompt you for them.

Usage

Method 1: Command-Line Interface (CLI)

Place a Market Order

python cli.py market BUY BTCUSDT 0.001

Place a Limit Order

python cli.py limit SELL BTCUSDT 0.001 50000

Place a Stop-Limit Order (Bonus)

python cli.py stop-limit BUY BTCUSDT 0.001 49000 49500

Check Account Balance

python cli.py balance

Get Order Status

python cli.py status BTCUSDT 12345678

Cancel an Order

python cli.py cancel BTCUSDT 12345678

Method 2: Interactive CLI (Bonus - Enhanced UI)

Run the interactive interface:

python interactive_cli.py

You'll be prompted to:

  1. Enter your API credentials (auto-loaded if config/env is set)
  2. Select from a menu of options
  3. Follow guided prompts for each operation

Method 3: Programmatic Usage

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)

Order Types

Market Order

Executes immediately at the current market price.

  • Side: BUY or SELL
  • Required: symbol, side, quantity

Limit Order

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

Stop-Limit Order (Bonus)

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

Logging

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.

Error Handling

The bot includes comprehensive error handling:

  • Input validation (symbol format, quantity, price)
  • Binance API error handling
  • Network error handling
  • User-friendly error messages

Project Structure

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)

Code Structure

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

Testing

Before using with real funds, always test on Binance Testnet:

  1. Use testnet API credentials
  2. Verify orders are placed correctly
  3. Check logs for any issues
  4. Test all order types

Security Notes

⚠️ Important Security Reminders:

  • 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

Troubleshooting

Common Issues

  1. "Invalid API key"

    • Verify your API credentials are correct
    • Ensure your testnet account is activated
    • Check that you're using testnet credentials, not mainnet
  2. "Insufficient balance"

    • Check your testnet account balance
    • Request testnet funds if needed
  3. "Invalid symbol"

    • Ensure symbol is uppercase (e.g., BTCUSDT, not btcusdt)
    • Verify the symbol exists on Binance Futures
  4. Connection errors

    • Check your internet connection
    • Verify Binance Testnet is accessible

License

This project is created for the Junior Python Developer - Crypto Trading Bot assignment.

Contact

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors