CCXT

4.5.48 · active · verified Fri Apr 10

CCXT (Cryptocurrency eXchange Trading Library) is a JavaScript / TypeScript / Python / C# / PHP / Go library providing a unified API for connecting to and trading with over 100 cryptocurrency exchanges worldwide. It offers quick access to market data (tickers, order books, OHLCV, trade history) and enables algorithmic trading functionalities like placing market/limit orders, managing balances, and handling deposits/withdrawals. The library is actively maintained with frequent updates and new exchange integrations.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate an exchange client and fetch public market data (a ticker) for a specified symbol. It includes basic error handling and illustrates how to configure rate limiting. For private API access (e.g., fetching balance, placing orders), API keys are required and should be loaded securely, ideally from environment variables.

import ccxt
import os

exchange_id = 'binance'
exchange_class = getattr(ccxt, exchange_id)

# Public API access (no API keys needed for public data)
exchange = exchange_class({
    'rateLimit': 1200,
    'enableRateLimit': True, # Important for respecting exchange limits
})

try:
    # Fetch ticker for a symbol
    symbol = 'BTC/USDT'
    ticker = exchange.fetch_ticker(symbol)
    print(f"Fetched ticker for {symbol} on {exchange_id}: {ticker['last']} (last price)")

    # For private API, uncomment and replace with actual keys (use environment variables in production)
    # exchange_private = exchange_class({
    #     'apiKey': os.environ.get('CCXT_BINANCE_API_KEY', ''),
    #     'secret': os.environ.get('CCXT_BINANCE_SECRET', ''),
    #     'rateLimit': 1200,
    #     'enableRateLimit': True,
    # })
    # if exchange_private.apiKey and exchange_private.secret:
    #     balance = exchange_private.fetch_balance()
    #     print(f"Fetched balance for {exchange_id}: {balance['total']}")

except ccxt.NetworkError as e:
    print(f"Network error: {type(e).__name__} {str(e)}")
except ccxt.ExchangeError as e:
    print(f"Exchange error: {type(e).__name__} {str(e)}")
except Exception as e:
    print(f"An unexpected error occurred: {type(e).__name__} {str(e)}")

view raw JSON →