Cryptocurrency Exchange Websocket Data Feed Handler for normalized market data across 30+ exchanges
npx @tessl/cli install tessl/pypi-cryptofeed@2.4.0A comprehensive Python library for cryptocurrency exchange data feeds that provides normalized and standardized market data from 30+ major cryptocurrency exchanges through websockets and REST APIs. Supports real-time data streaming, historical data access, multi-exchange NBBO calculation, and flexible data storage backends.
pip install cryptofeedpip install cryptofeed[all] for all backend supportfrom cryptofeed import FeedHandlerFor exchanges and data types:
from cryptofeed.exchanges import Coinbase, Binance, Kraken, Bitfinex
from cryptofeed.defines import L1_BOOK, L2_BOOK, L3_BOOK, TRADES, TICKER, FUNDING
from cryptofeed.types import Trade, Ticker, OrderBook, CandleFor backends:
from cryptofeed.backends.redis import TradeRedis, BookRedis
from cryptofeed.backends.mongo import TradeMongo
from cryptofeed.backends.postgres import TradePostgresfrom cryptofeed import FeedHandler
from cryptofeed.exchanges import Coinbase, Binance
from cryptofeed.defines import TRADES, TICKER, L2_BOOK
def trade_handler(trade):
print(f'Trade: {trade.exchange} {trade.symbol} {trade.side} {trade.amount} @ {trade.price}')
def ticker_handler(ticker):
print(f'Ticker: {ticker.exchange} {ticker.symbol} bid: {ticker.bid} ask: {ticker.ask}')
def book_handler(book):
print(f'Book: {book.exchange} {book.symbol} best bid: {list(book.book.bids.keys())[0]}')
# Create feed handler
fh = FeedHandler()
# Add exchange feeds with callbacks
fh.add_feed(Coinbase(
symbols=['BTC-USD', 'ETH-USD'],
channels=[TRADES, TICKER],
callbacks={TRADES: trade_handler, TICKER: ticker_handler}
))
fh.add_feed(Binance(
symbols=['BTCUSDT', 'ETHUSDT'],
channels=[L2_BOOK],
callbacks={L2_BOOK: book_handler}
))
# Start processing feeds
fh.run()Cryptofeed uses an event-driven architecture with these key components:
The architecture enables high-throughput data processing with minimal latency while abstracting away exchange-specific details.
Core functionality for managing cryptocurrency data feeds from multiple exchanges, including feed lifecycle management, connection handling, and event processing.
class FeedHandler:
def __init__(self, config=None, raw_data_collection=None): ...
def add_feed(self, feed, loop=None, **kwargs): ...
def run(self, start_loop=True, install_signal_handlers=True, exception_handler=None): ...
def stop(self, loop=None): ...
def add_nbbo(self, feeds, symbols, callback, config=None): ...Pre-built exchange implementations supporting websocket and REST API access for major cryptocurrency exchanges, with unified interfaces and automatic data normalization.
# Example exchange classes
class Coinbase(Feed): ...
class Binance(Feed): ...
class Kraken(Feed): ...
class Bitfinex(Feed): ...
# ... 30+ more exchangesFast Cython-based data structures representing different types of market data with standardized formats across all exchanges.
class Trade:
exchange: str
symbol: str
side: str
amount: float
price: float
timestamp: float
id: str
class Ticker:
exchange: str
symbol: str
bid: float
ask: float
timestamp: float
class OrderBook:
exchange: str
symbol: str
book: dict
timestamp: floatBuilt-in backend implementations for storing and streaming data to various databases, message queues, and other systems with minimal setup.
# Redis backends
class TradeRedis(BackendCallback): ...
class BookRedis(BackendCallback): ...
# Database backends
class TradeMongo(BackendCallback): ...
class TradePostgres(BackendCallback): ...
class TradeInflux(BackendCallback): ...
# Streaming backends
class TradeKafka(BackendCallback): ...
class TradeZMQ(BackendCallback): ...Synchronous and asynchronous REST API methods for historical data retrieval, order management, and account operations across all supported exchanges.
# Market Data REST Methods
def ticker_sync(self, symbol, **kwargs): ...
async def ticker(self, symbol, **kwargs): ...
def trades_sync(self, symbol, start=None, end=None, **kwargs): ...
async def trades(self, symbol, start=None, end=None, **kwargs): ...
def candles_sync(self, symbol, start=None, end=None, interval='1m', **kwargs): ...
async def candles(self, symbol, start=None, end=None, interval='1m', **kwargs): ...
def l2_book_sync(self, symbol, **kwargs): ...
async def l2_book(self, symbol, **kwargs): ...
# Trading REST Methods
def place_order_sync(self, symbol, side, order_type, amount, price=None, **kwargs): ...
async def place_order(self, symbol, side, order_type, amount, price=None, **kwargs): ...
def cancel_order_sync(self, order_id, symbol=None, **kwargs): ...
async def cancel_order(self, order_id, symbol=None, **kwargs): ...
def order_status_sync(self, order_id, symbol=None, **kwargs): ...
async def order_status(self, order_id, symbol=None, **kwargs): ...
def balances_sync(self, **kwargs): ...
async def balances(self, **kwargs): ...Standard constants for exchanges, channels, trading sides, and other enumerations, plus configuration management for feeds and authentication.
# Channel constants
L1_BOOK: str
L2_BOOK: str
L3_BOOK: str
TRADES: str
TICKER: str
FUNDING: str
OPEN_INTEREST: str
LIQUIDATIONS: str
# Exchange constants
COINBASE: str
BINANCE: str
KRAKEN: str
# ... all supported exchanges
# Trading constants
BUY: str
SELL: str
MAKER: str
TAKER: str