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.1.tar.gz (15.4 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.1-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: binance_trader_wby-0.1.1.tar.gz
  • Upload date:
  • Size: 15.4 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.1.tar.gz
Algorithm Hash digest
SHA256 8b7f2575052092c6afba155c034fc8751ba2d162de22980ed1a0b430065a66d8
MD5 c48c299e64c59c983dffe464f4ec92dc
BLAKE2b-256 4404a3dd6cfb105a33193cd12c3ae674d15334d404354e341f70c80ab18887bb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for binance_trader_wby-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7cad5a2ae6ff5a7cb159b2f9eb939628fa6f3917de3df2eb0cb232682435613c
MD5 41a0eadfa2f9e3e7a7e27bc16db33d9d
BLAKE2b-256 1fb9141d4f1ac9c8e1b9929a14207602ae247303557ebfeddf3eb1e4a36dc5d2

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