Skip to content

shivadixt/trading_bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Binance Futures Testnet Trading Bot

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.


What This Bot Can Do

  • 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

Project Structure

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

Tech Stack

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

Setup Instructions

Step 1 — Clone the Repository

git clone https://github.com/shivadixt/trading_bot.git
cd trading_bot

Step 2 — Create a Virtual Environment

python -m venv venv

Activate it:

# Windows
venv\Scripts\activate

# Mac or Linux
source venv/bin/activate

Step 3 — Install Dependencies

pip install -r requirements.txt

Step 4 — Get Binance Testnet API Keys

  1. Go to https://testnet.binancefuture.com
  2. Register or Login
  3. Go to API Management
  4. Generate your API Key and Secret
  5. Copy both keys

Step 5 — Create Your .env File

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.


How to Run

Interactive Mode (Step by Step Menu)

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

Headless Mode (Direct Terminal Commands)

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

How Order Output Looks

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

Logging

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

Input Validation

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 Handling

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

Bonus Features Implemented

1. Stop-Limit Order Type

Added as a third order type on top of MARKET and LIMIT. Available in both interactive and headless modes.

2. Enhanced CLI UX

Two modes of operation:

  • Interactive mode — step by step guided menu with confirmation screen
  • Headless mode — single line terminal commands using Typer

Architectural Decisions

Why Typer instead of argparse?

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

Why Interactive and Headless modes?

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.

Why separate validators.py?

All validation logic lives in one place. If any validation rule needs to change, only one file needs to be updated.

Why separate client.py?

The Binance connection is wrapped in its own file so if the API library ever changes, we only update one file.


Assumptions

  • 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

Sample log files from test runs are included in the logs/ folder:

  • One MARKET order log
  • One LIMIT order log

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages