Skip to content

GithubSohamSaha/Trading-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Trading Bot — Binance Futures Testnet (USDT-M)

A lightweight Python CLI application to place Market and Limit orders on the Binance Futures Testnet. Built with clean separation of concerns, structured logging, and robust input validation.


Project Structure

trading_bot/
├── bot/
│   ├── __pycache__/
│   ├── __init__.py
│   ├── client.py            # Binance client wrapper (auth + init)
│   ├── logging_config.py    # File + console logger setup
│   ├── orders.py            # Market and Limit order logic
│   └── validators.py        # CLI input validation
├── logs/
│   ├── trading_bot_YYYYMMDD.log   # Auto-generated daily log
│   ├── market_order_sample.log    # Sample MARKET order log
│   └── limit_order_sample.log     # Sample LIMIT order log
├── .env.example             # Template for API credentials
├── cli.py                   # CLI entry point (argparse)
├── README.md
└── requirements.txt

Setup

1. Get Binance Futures Testnet Credentials

  1. Go to https://testnet.binancefuture.com
  2. Log in using "Log In with GitHub" (no separate registration needed).
  3. On the dashboard, scroll to the API Key section and click Generate.
  4. Copy your API Key and Secret Key.

⚠️ This is a separate site from binance.com. Real Binance credentials will not work here.

2. Clone the Repository

git clone https://github.com/GithubSohamSaha/Trading-bot.git
cd Trading-bot

3. Create a Virtual Environment (recommended)

python -m venv venv

# Windows
venv\Scripts\activate

# macOS / Linux
source venv/bin/activate

4. Install Dependencies

pip install -r requirements.txt

5. Configure API Credentials

Create a .env file in the project root:

BINANCE_TESTNET_API_KEY=your_api_key_here
BINANCE_TESTNET_API_SECRET=your_api_secret_here

The app uses python-dotenv to load these automatically. Never commit your .env file.


How to Run

Test Connectivity

python cli.py --ping
[OK] Binance Futures Testnet is reachable.

Place a MARKET BUY Order

python cli.py --symbol BTCUSDT --side BUY --type MARKET --quantity 0.001

Actual Output:

───────────────────────────────────────────────────────
  ORDER REQUEST SUMMARY
───────────────────────────────────────────────────────
  Symbol     : BTCUSDT
  Side       : BUY
  Type       : MARKET
  Quantity   : 0.001
───────────────────────────────────────────────────────
2026-04-22 13:45:01 | INFO | trading_bot.orders | Placing MARKET order | symbol=BTCUSDT | side=BUY | qty=0.001
2026-04-22 13:45:02 | INFO | trading_bot.orders | MARKET order placed successfully
───────────────────────────────────────────────────────
  ORDER RESPONSE
───────────────────────────────────────────────────────
  Order ID   : 13060665209
  Symbol     : BTCUSDT
  Side       : BUY
  Type       : MARKET
  Orig Qty   : 0.0010
  Exec Qty   : 0.0000
  Avg Price  : 0.00
  Price      : 0.00
  Status     : NEW
  TIF        : GTC
───────────────────────────────────────────────────────

[SUCCESS] MARKET BUY order placed successfully!

Place a MARKET SELL Order

python cli.py --symbol BTCUSDT --side SELL --type MARKET --quantity 0.001

Place a LIMIT BUY Order

python cli.py --symbol BTCUSDT --side BUY --type LIMIT --quantity 0.001 --price 50000

Actual Output:

───────────────────────────────────────────────────────
  ORDER REQUEST SUMMARY
───────────────────────────────────────────────────────
  Symbol     : BTCUSDT
  Side       : BUY
  Type       : LIMIT
  Quantity   : 0.001
  Price      : 50000.0
───────────────────────────────────────────────────────
2026-04-22 13:46:43 | INFO | trading_bot.orders | Placing LIMIT order | symbol=BTCUSDT | side=BUY | qty=0.001 | price=50000.0
2026-04-22 13:46:43 | INFO | trading_bot.orders | LIMIT order placed successfully
───────────────────────────────────────────────────────
  ORDER RESPONSE
───────────────────────────────────────────────────────
  Order ID   : 13060669540
  Symbol     : BTCUSDT
  Side       : BUY
  Type       : LIMIT
  Orig Qty   : 0.0010
  Exec Qty   : 0.0000
  Avg Price  : 0.00
  Price      : 50000.00
  Status     : NEW
  TIF        : GTC
───────────────────────────────────────────────────────

[SUCCESS] LIMIT BUY order placed successfully!

Place a LIMIT SELL Order

python cli.py --symbol BTCUSDT --side SELL --type LIMIT --quantity 0.001 --price 95000

Actual Output:

───────────────────────────────────────────────────────
  ORDER REQUEST SUMMARY
───────────────────────────────────────────────────────
  Symbol     : BTCUSDT
  Side       : SELL
  Type       : LIMIT
  Quantity   : 0.001
  Price      : 95000.0
───────────────────────────────────────────────────────
2026-04-22 13:49:26 | INFO | trading_bot.orders | Placing LIMIT order | symbol=BTCUSDT | side=SELL | qty=0.001 | price=95000.0
2026-04-22 13:49:27 | INFO | trading_bot.orders | LIMIT order placed successfully
───────────────────────────────────────────────────────
  ORDER RESPONSE
───────────────────────────────────────────────────────
  Order ID   : 13060669541
  Symbol     : BTCUSDT
  Side       : SELL
  Type       : LIMIT
  Orig Qty   : 0.0010
  Exec Qty   : 0.0000
  Avg Price  : 0.00
  Price      : 95000.00
  Status     : NEW
  TIF        : GTC
───────────────────────────────────────────────────────

[SUCCESS] LIMIT SELL order placed successfully!

CLI Reference

Argument Required Description
--symbol / -s Trading pair (e.g. BTCUSDT, ETHUSDT)
--side BUY or SELL
--type / -t MARKET or LIMIT
--quantity / -q Order size in base asset units
--price / -p For LIMIT only Limit price
--ping Test connectivity and exit

Logging

All logs are written to logs/trading_bot_YYYYMMDD.log (one file per day).

Handler Level Purpose
File DEBUG Full API request params + raw responses
Console INFO Key events only (order placed, errors)

Error Handling

Scenario Behaviour
Missing API credentials in .env Clear error message + exit code 1
Invalid symbol format Validation error before any API call
Invalid side or order type Validation error before any API call
Missing --price for LIMIT order Validation error before any API call
Quantity or price ≤ 0 Validation error before any API call
Binance API error (4xx) Error logged with code + message, failure printed
Network / connectivity failure Error logged, failure printed

Assumptions

  • Testnet only — hardcoded to https://testnet.binancefuture.com. To use mainnet, set testnet=False in client.py.
  • USDT-M Futures — only USD-margined perpetual futures are supported.
  • Time-in-Force for LIMIT orders defaults to GTC (Good Till Cancelled).
  • Quantity precision must match Binance Futures symbol rules (e.g. BTCUSDT: 3 decimal places). Adjust if you get a precision error.
  • Credentials are loaded from a .env file in the project root via python-dotenv.

Requirements

  • Python 3.8+
  • python-binance==1.0.19
  • python-dotenv
  • requests>=2.31.0

About

Python-based Binance Futures Testnet trading bot with CLI support for MARKET/LIMIT BUY & SELL orders, input validation, structured logging, and robust error handling.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages