Skip to main content

A Python library for Binance cryptocurrency trading

Project description

Binance Trader

A Python library for Binance cryptocurrency trading API with support for spot, futures, and order management.

Author: WUBOYUAN
Contact: wuboyuan92@126.com

Features

  • Spot Trading: Buy and sell cryptocurrencies on Binance spot market
  • Futures Trading: Trade perpetual contracts with leverage
  • Order Management: Check order status, cancel orders, view history
  • Real-time Data: Get market data, prices, and order book
  • Risk Management: Built-in position sizing and risk controls

Installation

pip install binance-trader

Quick Start

from binance_trader import BinanceTrader

# Initialize trader
trader = BinanceTrader(
    api_key='your_api_key',
    api_secret='your_api_secret',
    testnet=True  # Use testnet for testing
)

# Get account balance
balance = trader.get_balance()
print(balance)

# Place a spot order
order = trader.spot.buy(
    symbol='BTCUSDT',
    quantity=0.001,
    order_type='MARKET'
)
print(order)

Configuration

Using Environment Variables (Recommended)

export BINANCE_API_KEY='your_api_key'
export BINANCE_API_SECRET='your_api_secret'
from binance_trader import BinanceTrader

trader = BinanceTrader()  # Automatically loads from environment

Using Config File

Create ~/.binance_trader/config.json:

{
    "api_key": "your_api_key",
    "api_secret": "your_api_secret",
    "testnet": true
}

Usage Examples

Spot Trading

from binance_trader import BinanceTrader

trader = BinanceTrader()

# Market buy
order = trader.spot.buy_market('BTCUSDT', quantity=0.001)

# Limit buy
order = trader.spot.buy_limit('BTCUSDT', quantity=0.001, price=50000)

# Sell
order = trader.spot.sell_market('BTCUSDT', quantity=0.001)

# Get open orders
orders = trader.spot.get_open_orders('BTCUSDT')

# Cancel order
trader.spot.cancel_order('BTCUSDT', order_id=123456)

Futures Trading

from binance_trader import BinanceTrader

trader = BinanceTrader()

# Set leverage
trader.futures.set_leverage('BTCUSDT', leverage=10)

# Open long position
order = trader.futures.open_long('BTCUSDT', quantity=0.01)

# Open short position
order = trader.futures.open_short('BTCUSDT', quantity=0.01)

# Close position
order = trader.futures.close_position('BTCUSDT')

# Get position info
position = trader.futures.get_position('BTCUSDT')

Order Management

from binance_trader import BinanceTrader

trader = BinanceTrader()

# Check specific order
order = trader.orders.get_order('BTCUSDT', order_id=123456)

# Get all open orders
orders = trader.orders.get_all_open_orders()

# Get order history
history = trader.orders.get_history('BTCUSDT', limit=100)

# Cancel all orders for a symbol
trader.orders.cancel_all('BTCUSDT')

Market Data

from binance_trader import BinanceTrader

trader = BinanceTrader()

# Get current price
price = trader.market.get_price('BTCUSDT')

# Get order book
depth = trader.market.get_order_book('BTCUSDT', limit=10)

# Get recent trades
trades = trader.market.get_recent_trades('BTCUSDT', limit=50)

# Get klines/candlestick data
klines = trader.market.get_klines('BTCUSDT', interval='1h', limit=100)

Advanced Usage

Risk Management

from binance_trader import BinanceTrader
from binance_trader.risk import RiskManager

trader = BinanceTrader()
risk = RiskManager(trader)

# Calculate position size based on risk percentage
size = risk.calculate_position_size(
    symbol='BTCUSDT',
    risk_percent=2.0,  # Risk 2% of account
    stop_loss_percent=5.0  # 5% stop loss
)

# Place order with risk management
order = risk.place_order_with_stop_loss(
    symbol='BTCUSDT',
    side='BUY',
    risk_percent=2.0,
    stop_loss_percent=5.0
)

WebSocket Streams (Real-time Data)

from binance_trader import BinanceTrader
from binance_trader.stream import BinanceStream

trader = BinanceTrader()
stream = BinanceStream(trader)

# Subscribe to price updates
def on_price_update(data):
    print(f"Price update: {data}")

stream.subscribe_trade('BTCUSDT', on_price_update)
stream.start()

API Reference

See API Documentation for detailed API reference.

Testing

Use Binance testnet for testing without real money:

from binance_trader import BinanceTrader

trader = BinanceTrader(testnet=True)
# All operations use testnet funds

Get testnet API keys at: https://testnet.binance.vision/

Requirements

  • Python 3.8+
  • requests
  • websocket-client
  • python-dotenv

License

MIT License - see LICENSE file

Disclaimer

This library is for educational purposes. Cryptocurrency trading involves significant risk. Use at your own risk.

Support

Changelog

See CHANGELOG.md for version history.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

binance_trader_wby-0.1.0.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

binance_trader_wby-0.1.0-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file binance_trader_wby-0.1.0.tar.gz.

File metadata

  • Download URL: binance_trader_wby-0.1.0.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for binance_trader_wby-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bba1311ffd9cca88d78974bd1728f77924992f1bd3962c25c99617b19d3f5f8d
MD5 e4ca41356438171a6e02d1f43a994908
BLAKE2b-256 7365f3a1981d0b80074a7dae6c92945d3854e83387d2f13637244b6b136ed161

See more details on using hashes here.

File details

Details for the file binance_trader_wby-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for binance_trader_wby-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 35388a97e7a2f8306b1f91f40d3fdb3f51db37919ee4a4923c31fd59b234e0f1
MD5 a290b341b6e1564771457d14363425e9
BLAKE2b-256 3e7bc517838cd075b9d8f460b860413d3ad6daebba7280da4d5e47092c954f10

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page