Ccxt python

From CarlosCaPe/octorato · View interactive version

Ccxt python is an AI agent skill from the GitHub repository CarlosCaPe/octorato. CCXT library for crypto exchanges in Python, REST and WebSocket: connect, pull market data, place orders and stream tickers. For trading bots and analysis, sync or asyncio.

How to use this skill

Skill file

CCXT for Python

A comprehensive guide to using CCXT in Python projects for cryptocurrency exchange integration.

Installation

REST API (Standard)

pip install ccxt

WebSocket API (Real-time, ccxt.pro)

pip install ccxt

Optional Performance Enhancements

pip install orjson      # Faster JSON parsing
pip install coincurve   # Faster ECDSA signing (45ms → 0.05ms)

Both REST and WebSocket APIs are included in the same package.

Quick Start

REST API - Synchronous

import ccxt

exchange = ccxt.binance()
exchange.load_markets()
ticker = exchange.fetch_ticker('BTC/USDT')
print(ticker)

REST API - Asynchronous

import asyncio
import ccxt.async_support as ccxt

async def main():
    exchange = ccxt.binance()
    await exchange.load_markets()
    ticker = await exchange.fetch_ticker('BTC/USDT')
    print(ticker)
    await exchange.close()  # Important!

asyncio.run(main())

WebSocket API - Real-time Updates

import asyncio
import ccxt.pro as ccxtpro

async def main():
    exchange = ccxtpro.binance()
    while True:
        ticker = await exchange.watch_ticker('BTC/USDT')
        print(ticker)  # Live updates!
    await exchange.close()

asyncio.run(main())

REST vs WebSocket

| Import | For REST | For WebSocket |

|--------|----------|---------------|

| Sync | import ccxt | (WebSocket requires async) |

| Async | import ccxt.async_support as ccxt | import ccxt.pro as ccxtpro |

| Feature | REST API | WebSocket API |

|---------|----------|---------------|

| Use for | One-time queries, placing orders | Real-time monitoring, live price feeds |

| Method prefix | fetch_* (fetch_ticker, fetch_order_book) | watch_* (watch_ticker, watch_order_book) |

| Speed | Slower (HTTP request/response) | Faster (persistent connection) |

| Rate limits | Strict (1-2 req/sec) | More lenient (continuous stream) |

| Best for | Trading, account management | Price monitoring, arbitrage detection |

When to use REST:

When to use WebSocket:

Creating Exchange Instance

REST API - Synchronous

import ccxt

# Public API (no authentication)
exchange = ccxt.binance({
    'enableRateLimit': True  # Recommended!
})

# Private API (with authentication)
exchange = ccxt.binance({
    'apiKey': 'YOUR_API_KEY',
    'secret': 'YOUR_SECRET',
    'enableRateLimit': True
})

REST API - Asynchronous

import ccxt.async_support as ccxt

exchange = ccxt.binance({
    'apiKey': 'YOUR_API_KEY',
    'secret': 'YOUR_SECRET',
    'enableRateLimit': True
})

# Always close when done
await exchange.close()

WebSocket API

import ccxt.pro as ccxtpro

# Public WebSocket
exchange = ccxtpro.binance()

# Private WebSocket (with authentication)
exchange = ccxtpro.binance({
    'apiKey': 'YOUR_API_KEY',
    'secret': 'YOUR_SECRET'
})

# Always close when done
await exchange.close()

Common REST Operations

Loading Markets

# Load all available trading pairs
exchange.load_markets()

# Access market information
btc_market = exchange.market('BTC/USDT')
print(btc_market['limits']['amount']['min'])  # Minimum order amount

Fetching Ticker

# Single ticker
ticker = exchange.fetch_ticker('BTC/USDT')
print(ticker['last'])      # Last price
print(ticker['bid'])       # Best bid
print(ticker['ask'])       # Best ask
print(ticker['volume'])    # 24h volume

# Multiple tickers (if supported)
tickers = exchange.fetch_tickers(['BTC/USDT', 'ETH/USDT'])

Fetching Order Book

# Full orderbook
orderbook = exchange.fetch_order_book('BTC/USDT')
print(orderbook['bids'][0])  # [price, amount]
print(orderbook['asks'][0])  # [price, amount]

# Limited depth
orderbook = exchange.fetch_order_book('BTC/USDT', 5)  # Top 5 levels

Creating Orders

Limit Order
# Buy limit order
order = exchange.create_limit_buy_order('BTC/USDT', 0.01, 50000)
print(order['id'])

# Sell limit order
order = exchange.create_limit_sell_order('BTC/USDT', 0.01, 60000)

# Generic limit order
order = exchange.create_order('BTC/USDT', 'limit', 'buy', 0.01, 50000)
Market Order
# Buy market order
order = exchange.create_market_buy_order('BTC/USDT', 0.01)

# Sell market order
order = exchange.create_market_sell_order('BTC/USDT', 0.01)

# Generic market order
order = exchange.create_order('BTC/USDT', 'market', 'sell', 0.01)

Fetching Balance

balance = exchange.fetch_balance()
print(balance['BTC']['free'])   # Available balance
print(balance['BTC']['used'])   # Balance in orders
print(balance['BTC']['total'])  # Total balance

Fetching Orders

# Open orders
open_orders = exchange.fetch_open_orders('BTC/USDT')

# Closed orders
closed_orders = exchange.fetch_closed_orders('BTC/USDT')

# All orders (open + closed)
all_orders = exchange.fetch_orders('BTC/USDT')

# Single order by ID
order = exchange.fetch_order(order_id, 'BTC/USDT')

Fetching Trades

# Recent public trades
trades = exchange.fetch_trades('BTC/USDT', limit=10)

# Your trades (requires authentication)
my_trades = exchange.fetch_my_trades('BTC/USDT')

Canceling Orders

# Cancel single order
exchange.cancel_order(order_id, 'BTC/USDT')

# Cancel all orders for a symbol
exchange.cancel_all_orders('BTC/USDT')

WebSocket Operations (Real-time)

Watching Ticker (Live Price Updates)

import asyncio
import ccxt.pro as ccxtpro

async def main():
    exchange = ccxtpro.binance()
    while True:
        ticker = await exchange.watch_ticker('BTC/USDT')
        print(ticker['last'], ticker['timestamp'])
    await exchange.close()

asyncio.run(main())

Watching Order Book (Live Depth Updates)

async def main():
    exchange = ccxtpro.binance()
    while True:
        orderbook = await exchange.watch_order_book('BTC/USDT')
        print('Best bid:', orderbook['bids'][0])
        print('Best ask:', orderbook['asks'][0])
    await exchange.close()

asyncio.run(main())

Watching Trades (Live Trade Stream)

async def main():
    exchange = ccxtpro.binance()
    while True:
        trades = await exchange.watch_trades('BTC/USDT')
        for trade in trades:
            print(trade['price'], trade['amount'], trade['side'])
    await exchange.close()

asyncio.run(main())

Watching Your Orders (Live Order Updates)

async def main():
    exchange = ccxtpro.binance({
        'apiKey': 'YOUR_API_KEY',
        'secret': 'YOUR_SECRET'
    })
    while True:
        orders = await exchange.watch_orders('BTC/USDT')
        for order in orders:
            print(order['id'], order['status'], order['filled'])
    await exchange.close()

asyncio.run(main())

Watching Balance (Live Balance Updates)

async def main():
    exchange = ccxtpro.binance({
        'apiKey': 'YOUR_API_KEY',
        'secret': 'YOUR_SECRET'
    })
    while True:
        balance = await exchange.watch_balance()
        print('BTC:', balance['BTC'])
        print('USDT:', balance['USDT'])
    await exchange.close()

asyncio.run(main())

Watching Multiple Symbols

async def main():
    exchange = ccxtpro.binance()
    symbols = ['BTC/USDT', 'ETH/USDT', 'SOL/USDT']

    while True:
        # Watch all symbols concurrently
        tickers = await exchange.watch_tickers(symbols)
        for symbol, ticker in tickers.items():
            print(symbol, ticker['last'])
    await exchange.close()

asyncio.run(main())

Complete Method Reference

Market Data Methods

Tickers & Prices
Order Books
Trades
OHLCV (Candlesticks)

Account & Balance

Trading Methods

Creating Orders
Managing Orders

Margin & Leverage

Derivatives & Futures

Positions
Funding & Settlement
Open Interest & Liquidations
Options

Fees & Limits

Deposits & Withdrawals

Transfer & Convert

Market Info

WebSocket Methods (ccxt.pro)

All REST methods have WebSocket equivalents with watch* prefix:

Real-time Market Data
Real-time Account Data (Auth Required)

Authentication Required

Methods marked with 🔒 require API credentials:

Checking Method Availability

Not all exchanges support all methods. Check before using:

// Check if method is supported
if (exchange.has['fetchOHLCV']) {
    const candles = await exchange.fetchOHLCV('BTC/USDT', '1h')
}

// Check multiple capabilities
console.log(exchange.has)
// {
//   fetchTicker: true,
//   fetchOHLCV: true,
//   fetchMyTrades: true,
//   fetchPositions: false,
//   ...
// }

Method Naming Convention

Proxy Configuration

CCXT supports HTTP, HTTPS, and SOCKS proxies for both REST and WebSocket connections.

Setting Proxy

// HTTP Proxy
exchange.httpProxy = 'http://your-proxy-host:port'

// HTTPS Proxy  
exchange.httpsProxy = 'https://your-proxy-host:port'

// SOCKS Proxy
exchange.socksProxy = 'socks://your-proxy-host:port'

// Proxy with authentication
exchange.httpProxy = 'http://user:pass@proxy-host:port'

Proxy for WebSocket

WebSocket connections also respect proxy settings:

exchange.httpsProxy = 'https://proxy:8080'
// WebSocket connections will use this proxy

Testing Proxy Connection

exchange.httpProxy = 'http://localhost:8080'
try {
    await exchange.fetchTicker('BTC/USDT')
    console.log('Proxy working!')
} catch (error) {
    console.error('Proxy connection failed:', error)
}

WebSocket-Specific Methods

Some exchanges provide WebSocket variants of REST methods for faster order placement and management. These use the *Ws suffix:

Trading via WebSocket

Creating Orders:

Managing Orders:

Fetching Data:

When to Use WebSocket Methods

Use *Ws methods when:

Use REST methods when:

Example: Order Placement Comparison

REST API (slower, more reliable):

const order = await exchange.createOrder('BTC/USDT', 'limit', 'buy', 0.01, 50000)

WebSocket API (faster, lower latency):

const order = await exchange.createOrderWs('BTC/USDT', 'limit', 'buy', 0.01, 50000)

Checking WebSocket Method Availability

Not all exchanges support WebSocket trading methods:

if (exchange.has['createOrderWs']) {
    // Exchange supports WebSocket order creation
    const order = await exchange.createOrderWs('BTC/USDT', 'limit', 'buy', 0.01, 50000)
} else {
    // Fall back to REST
    const order = await exchange.createOrder('BTC/USDT', 'limit', 'buy', 0.01, 50000)
}

Authentication

Setting API Keys

import os

# During instantiation (recommended)
exchange = ccxt.binance({
    'apiKey': os.environ.get('BINANCE_API_KEY'),
    'secret': os.environ.get('BINANCE_SECRET'),
    'enableRateLimit': True
})

# After instantiation
exchange.apiKey = os.environ.get('BINANCE_API_KEY')
exchange.secret = os.environ.get('BINANCE_SECRET')

Testing Authentication

try:
    balance = exchange.fetch_balance()
    print('Authentication successful!')
except ccxt.AuthenticationError:
    print('Invalid API credentials')

Error Handling

Exception Hierarchy

BaseError
├─ NetworkError (recoverable - retry)
│  ├─ RequestTimeout
│  ├─ ExchangeNotAvailable
│  ├─ RateLimitExceeded
│  └─ DDoSProtection
└─ ExchangeError (non-recoverable - don't retry)
   ├─ AuthenticationError
   ├─ InsufficientFunds
   ├─ InvalidOrder
   └─ NotSupported

Basic Error Handling

import ccxt

try:
    ticker = exchange.fetch_ticker('BTC/USDT')
except ccxt.NetworkError as e:
    print('Network error - retry:', str(e))
except ccxt.ExchangeError as e:
    print('Exchange error - do not retry:', str(e))
except Exception as e:
    print('Unknown error:', str(e))

Specific Exception Handling

try:
    order = exchange.create_order('BTC/USDT', 'limit', 'buy', 0.01, 50000)
except ccxt.InsufficientFunds:
    print('Not enough balance')
except ccxt.InvalidOrder:
    print('Invalid order parameters')
except ccxt.RateLimitExceeded:
    print('Rate limit hit - wait before retrying')
    exchange.sleep(1000)  # Wait 1 second
except ccxt.AuthenticationError:
    print('Check your API credentials')

Retry Logic for Network Errors

def fetch_with_retry(max_retries=3):
    for i in range(max_retries):
        try:
            return exchange.fetch_ticker('BTC/USDT')
        except ccxt.NetworkError:
            if i < max_retries - 1:
                print(f'Retry {i + 1}/{max_retries}')
                exchange.sleep(1000 * (i + 1))  # Exponential backoff
            else:
                raise

Async vs Sync

When to Use Sync

When to Use Async

Sync Example

import ccxt

exchange = ccxt.binance({'enableRateLimit': True})
ticker = exchange.fetch_ticker('BTC/USDT')
print(ticker['last'])

Async Example

import asyncio
import ccxt.async_support as ccxt

async def main():
    exchange = ccxt.binance({'enableRateLimit': True})
    ticker = await exchange.fetch_ticker('BTC/USDT')
    print(ticker['last'])
    await exchange.close()

asyncio.run(main())

Multiple Exchanges Async

async def fetch_all():
    exchanges = [
        ccxt.binance({'enableRateLimit': True}),
        ccxt.coinbase({'enableRateLimit': True}),
        ccxt.kraken({'enableRateLimit': True})
    ]

    # Fetch concurrently
    tasks = [ex.fetch_ticker('BTC/USDT') for ex in exchanges]
    tickers = await asyncio.gather(*tasks, return_exceptions=True)

    for ex, ticker in zip(exchanges, tickers):
        if isinstance(ticker, Exception):
            print(f'{ex.id}: ERROR - {ticker}')
        else:
            print(f'{ex.id}: ${ticker["last"]}')
        await ex.close()

asyncio.run(fetch_all())

Rate Limiting

Built-in Rate Limiter (Recommended)

exchange = ccxt.binance({
    'enableRateLimit': True  # Automatically throttles requests
})

Manual Delays

exchange.fetch_ticker('BTC/USDT')
exchange.sleep(1000)  # Wait 1 second (milliseconds)
exchange.fetch_ticker('ETH/USDT')

Checking Rate Limit

print(exchange.rateLimit)  # Milliseconds between requests

Common Pitfalls

Forgetting `await` in Async Mode

# Wrong - returns coroutine, not data
async def wrong():
    ticker = exchange.fetch_ticker('BTC/USDT')  # Missing await!
    print(ticker['last'])  # ERROR

# Correct
async def correct():
    ticker = await exchange.fetch_ticker('BTC/USDT')
    print(ticker['last'])  # Works!

Using Sync for WebSocket

# Wrong - WebSocket requires async
import ccxt.pro as ccxtpro
exchange = ccxtpro.binance()
ticker = exchange.watch_ticker('BTC/USDT')  # ERROR: Need await!

# Correct
import asyncio
import ccxt.pro as ccxtpro

async def main():
    exchange = ccxtpro.binance()
    ticker = await exchange.watch_ticker('BTC/USDT')
    await exchange.close()

asyncio.run(main())

Not Closing Async Exchange

# Wrong - resource leak
async def wrong():
    exchange = ccxt.binance()
    await exchange.fetch_ticker('BTC/USDT')
    # Forgot to close!

# Correct
async def correct():
    exchange = ccxt.binance()
    try:
        await exchange.fetch_ticker('BTC/USDT')
    finally:
        await exchange.close()

Using Sync in Async Code

# Wrong - blocks event loop
async def wrong():
    exchange = ccxt.binance()  # Sync import!
    ticker = exchange.fetch_ticker('BTC/USDT')  # Blocking!

# Correct
import ccxt.async_support as ccxt

async def correct():
    exchange = ccxt.binance()
    ticker = await exchange.fetch_ticker('BTC/USDT')
    await exchange.close()

Using REST for Real-time Monitoring

# Wrong - wastes rate limits
while True:
    ticker = exchange.fetch_ticker('BTC/USDT')  # REST
    print(ticker['last'])
    exchange.sleep(1000)

# Correct - use WebSocket
import ccxt.pro as ccxtpro

async def correct():
    exchange = ccxtpro.binance()
    while True:
        ticker = await exchange.watch_ticker('BTC/USDT')  # WebSocket
        print(ticker['last'])
    await exchange.close()

Troubleshooting

Common Issues

1. "ModuleNotFoundError: No module named 'ccxt'"

2. "RateLimitExceeded"

3. "AuthenticationError"

4. "InvalidNonce"

5. "InsufficientFunds"

6. "ExchangeNotAvailable"

7. SSL/Certificate errors

8. Slow performance

Debugging

# Enable verbose logging
exchange.verbose = True

# Check exchange capabilities
print(exchange.has)
# {
#   'fetchTicker': True,
#   'fetchOrderBook': True,
#   'createOrder': True,
#   ...
# }

# Check market information
print(exchange.markets['BTC/USDT'])

# Check last request/response
print(exchange.last_http_response)
print(exchange.last_json_response)

Learn More

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.