# ccxt-python > CCXT cryptocurrency exchange library for Binance Futures gold trading. REST and WebSocket API patterns. - Author: aqibhassan - Repository: aqibhassan/gold-trading-bot - Version: 20260209222550 - Stars: 0 - Forks: 0 - Last Updated: 2026-02-09 - Source: https://github.com/aqibhassan/gold-trading-bot - Web: https://mule.run/skillshub/@@aqibhassan/gold-trading-bot~ccxt-python:20260209222550 --- --- name: ccxt-python description: CCXT cryptocurrency exchange library for Binance Futures gold trading. REST and WebSocket API patterns. --- # CCXT Python — Binance Futures ## When to Use Apply when connecting to Binance Futures, fetching market data, placing orders, or managing positions for XAUUSDT. ## Setup ```python import ccxt.pro as ccxtpro # Async WebSocket support exchange = ccxtpro.binance({ 'apiKey': os.environ['BINANCE_API_KEY'], 'secret': os.environ['BINANCE_SECRET'], 'options': { 'defaultType': 'future', # Futures, not spot 'adjustForTimeDifference': True, }, 'sandbox': True, # Testnet for paper trading }) ``` ## Key Operations ### Fetch OHLCV ```python ohlcv = await exchange.fetch_ohlcv('XAU/USDT:USDT', '1m', limit=500) # Returns: [[timestamp, open, high, low, close, volume], ...] ``` ### Place Orders ```python # Limit order order = await exchange.create_limit_buy_order( 'XAU/USDT:USDT', amount=0.01, price=2650.00, params={'timeInForce': 'GTC'} ) # Market order order = await exchange.create_market_buy_order('XAU/USDT:USDT', amount=0.01) # Stop-loss order = await exchange.create_order( 'XAU/USDT:USDT', 'stop_market', 'sell', amount=0.01, price=None, params={'stopPrice': 2640.00, 'closePosition': False} ) ``` ### WebSocket Streams ```python # Real-time trades while True: trades = await exchange.watch_trades('XAU/USDT:USDT') for trade in trades: process_tick(trade) # Order book while True: orderbook = await exchange.watch_order_book('XAU/USDT:USDT') process_book(orderbook) # Account updates (fills, positions) while True: balance = await exchange.watch_balance() orders = await exchange.watch_orders('XAU/USDT:USDT') ``` ### Position Management ```python positions = await exchange.fetch_positions(['XAU/USDT:USDT']) balance = await exchange.fetch_balance() ``` ## Rate Limits - REST: 1200 weight/min (most endpoints = 1-5 weight) - WebSocket: 5 messages/sec per connection - Order rate: 10 orders/sec, 100,000 orders/day - Use CCXT's built-in rate limiter: `exchange.rateLimit` ## Error Handling ```python try: order = await exchange.create_order(...) except ccxt.InsufficientFunds: logger.error("Insufficient margin") except ccxt.InvalidOrder as e: logger.error(f"Invalid order: {e}") except ccxt.NetworkError: logger.warning("Network error — retry with backoff") except ccxt.ExchangeError as e: logger.error(f"Exchange error: {e}") ``` ## Gold-Specific Notes - Symbol: `XAU/USDT:USDT` (USDT-margined perpetual) - Gold has wider spreads during Asian session — prefer limit orders - Funding rate impacts P&L — monitor and factor into strategy - Testnet symbol may differ — verify with `exchange.load_markets()`