Skip to content

vidishagawas121/trading_bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ€– Binance Futures Testnet Trading Bot

A clean, production-structured Python CLI application for placing orders on the Binance USDT-M Futures Testnet.


Features

Feature Status
Market & Limit orders βœ…
BUY and SELL sides βœ…
Stop-Market orders (bonus) βœ…
Input validation with clear error messages βœ…
Structured logging to file + console βœ…
Full exception handling (API / network / input) βœ…
Layered architecture (client / orders / validators / CLI) βœ…
Account balance and open order listing βœ…

Project Structure

trading_bot/
β”œβ”€β”€ bot/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ client.py          # Binance REST API wrapper (auth, signing, requests)
β”‚   β”œβ”€β”€ orders.py          # Order placement orchestration + output formatting
β”‚   β”œβ”€β”€ validators.py      # Input validation for all order parameters
β”‚   └── logging_config.py  # Rotating file + console logging setup
β”œβ”€β”€ cli.py                 # CLI entry point (argparse sub-commands)
β”œβ”€β”€ logs/
β”‚   └── trading_bot.log    # Auto-created; sample log included
β”œβ”€β”€ requirements.txt
└── README.md

Setup

1. Clone / download

git clone https://github.com/<your-username>/trading-bot.git
cd trading-bot

2. Install dependencies

pip install -r requirements.txt

4. Get Binance Futures Testnet credentials

  1. Go to https://testnet.binancefuture.com
  2. Log in with your GitHub account
  3. Navigate to API Key β†’ generate a key pair
  4. Copy the API Key and Secret Key

5. Set credentials

Add β€” .env file in the project root:

BINANCE_API_KEY=your_api_key_here
BINANCE_API_SECRET=your_api_secret_here

How to Run

Test connectivity

python cli.py ping

Place a MARKET order

# BUY 0.001 BTC at market price
python cli.py place --symbol BTCUSDT --side BUY --type MARKET --quantity 0.001 or py cli.py place --symbol BTCUSDT --side BUY --type MARKET --quantity 0.001 (if oython doesn't work try with py)

# SELL 0.1 ETH at market price
python cli.py place --symbol ETHUSDT --side SELL --type MARKET --quantity 0.1 

Place a LIMIT order

# SELL 0.001 BTC at $100,000 (resting limit)
python cli.py place --symbol BTCUSDT --side SELL --type LIMIT --quantity 0.001 --price 100000 py cli.py place --symbol BTCUSDT --side SELL --type LIMIT --quantity 0.001 --price 100000

# BUY 0.002 BTC at $55,000 with IOC time-in-force
python cli.py place --symbol BTCUSDT --side BUY --type LIMIT --quantity 0.002 --price 55000 --tif IOC

Place a Stop-Market order (bonus)

# SELL stop-market at $90,000 trigger
python cli.py place --symbol BTCUSDT --side SELL --type STOP_MARKET --quantity 0.001 --stop-price 90000

List open orders

python cli.py orders
python cli.py orders --symbol BTCUSDT

Show account balances

python cli.py account

Adjust verbosity

python cli.py --log-level DEBUG place --symbol BTCUSDT --side BUY --type MARKET --quantity 0.001

Example Output

============================================================
  ORDER REQUEST SUMMARY
============================================================
  Symbol     : BTCUSDT
  Side       : BUY
  Type       : MARKET
  Quantity   : 0.001
============================================================

============================================================
  ORDER RESPONSE
============================================================
  orderId        : 4007437269
  symbol         : BTCUSDT
  side           : BUY
  type           : MARKET
  origQty        : 0.001
  executedQty    : 0.001
  price          : 0
  avgPrice       : 96420.10
  status         : FILLED
  timeInForce    : GTC
  updateTime     : 1746089524182
============================================================

βœ…  Order placed successfully! OrderId: 4007437269

##OUTPUT SCREENSHOTS 1st Screenshot market

2nd Screenshot limit

Success image

Logging

Logs are written to logs/trading_bot.log (rotating, max 5 MB Γ— 3 backups).

Each log entry includes:

  • Timestamp (ISO 8601)
  • Level (DEBUG / INFO / WARNING / ERROR)
  • Module name
  • Human-readable message

API errors, network failures, and validation errors are all captured with context.


CLI Reference

usage: trading_bot [-h] [--base-url URL] [--log-level {DEBUG,INFO,WARNING,ERROR}]
                   <command> ...

Commands:
  place     Place a new order
  orders    List open orders
  account   Show account balances
  ping      Test connectivity

place arguments:
  --symbol      Trading pair, e.g. BTCUSDT          (required)
  --side        BUY or SELL                          (required)
  --type        MARKET | LIMIT | STOP_MARKET | STOP  (required)
  --quantity    Order size in base asset             (required)
  --price       Limit price (required for LIMIT)
  --stop-price  Trigger price (required for STOP_MARKET / STOP)
  --tif         Time-in-force: GTC|IOC|FOK|GTX       (default: GTC)
  --reduce-only Place a reduce-only order

Assumptions

  1. Testnet only β€” the default base URL is https://testnet.binancefuture.com. Pass --base-url to override.
  2. USDT-M Futures β€” this bot targets the USD-margined perpetual futures market, not spot.
  3. No order book validation β€” symbol precision (tick size, lot size) is not pre-checked; the exchange will reject requests that violate these rules with a clear error message.
  4. Credentials via env / .env β€” no credentials are ever stored in source files.
  5. reduce_only defaults to False β€” pass --reduce-only to override.

Dependencies

requests>=2.31.0    # HTTP client
python-dotenv>=1.0.0  # .env file loading

Python 3.9+ required.

About

Binance Futures Testnet Trading Bot is a Python-based command-line application that allows users to place MARKET and LIMIT orders on the Binance USDT-M Futures Testnet. The project is designed with a modular architecture, focusing on clean code structure, input validation, logging, and robust error handling.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages