### Basic FeedHandler Setup and Subscription Source: https://github.com/bmoscon/cryptofeed/blob/master/README.md Demonstrates how to initialize FeedHandler, add subscriptions for different exchanges and channels, and start the feed. Requires user-defined callback functions for data events. ```python from cryptofeed import FeedHandler # not all imports shown for clarity fh = FeedHandler() # ticker, trade, and book are user defined functions that # will be called when ticker, trade and book updates are received ticker_cb = {TICKER: ticker} trade_cb = {TRADES: trade} gemini_cb = {TRADES: trade, L2_BOOK: book} fh.add_feed(Coinbase(symbols=['BTC-USD'], channels=[TICKER], callbacks=ticker_cb)) fh.add_feed(Bitfinex(symbols=['BTC-USD'], channels=[TICKER], callbacks=ticker_cb)) fh.add_feed(Poloniex(symbols=['BTC-USDT'], channels=[TRADES], callbacks=trade_cb)) fh.add_feed(Gemini(symbols=['BTC-USD', 'ETH-USD'], channels=[TRADES, L2_BOOK], callbacks=gemini_cb)) fh.run() ``` -------------------------------- ### Install Cryptofeed with Kafka Backend Source: https://github.com/bmoscon/cryptofeed/blob/master/INSTALL.md Install Cryptofeed with support for the Kafka backend. ```bash pip install --user --upgrade cryptofeed[kafka] ``` -------------------------------- ### Install Cryptofeed with Arctic Backend Source: https://github.com/bmoscon/cryptofeed/blob/master/INSTALL.md Install Cryptofeed with support for the Arctic backend. ```bash pip install --user --upgrade cryptofeed[arctic] ``` -------------------------------- ### Install Cryptofeed with All Optional Dependencies Source: https://github.com/bmoscon/cryptofeed/blob/master/INSTALL.md Install Cryptofeed along with all its optional backend dependencies in a single bundle. ```bash pip install --user --upgrade cryptofeed[all] ``` -------------------------------- ### Install Cryptofeed with ZeroMQ Backend Source: https://github.com/bmoscon/cryptofeed/blob/master/INSTALL.md Install Cryptofeed with support for the ZeroMQ backend. ```bash pip install --user --upgrade cryptofeed[zmq] ``` -------------------------------- ### Install Cryptofeed with RabbitMQ Backend Source: https://github.com/bmoscon/cryptofeed/blob/master/INSTALL.md Install Cryptofeed with support for the RabbitMQ backend. ```bash pip install --user --upgrade cryptofeed[rabbit] ``` -------------------------------- ### Install Cryptofeed with Redis Backend Source: https://github.com/bmoscon/cryptofeed/blob/master/INSTALL.md Install Cryptofeed with support for the Redis backend. ```bash pip install --user --upgrade cryptofeed[redis] ``` -------------------------------- ### Get Futures Positions with BinanceFutures Source: https://context7.com/bmoscon/cryptofeed/llms.txt Initialize the Binance Futures client and retrieve current positions. Prints details for positions with a non-zero amount. ```python # Futures - Get positions from cryptofeed.exchanges import BinanceFutures futures = BinanceFutures(config='config.yaml') positions = futures.positions_sync() for pos in positions: if float(pos['positionAmt']) != 0: print(f"{pos['symbol']}: {pos['positionAmt']} @ {pos['entryPrice']}") ``` -------------------------------- ### Cryptofeed Configuration File Example Source: https://context7.com/bmoscon/cryptofeed/llms.txt Example YAML configuration file for Cryptofeed, specifying logging settings, performance options (uvloop), error handling for invalid instruments, multiprocessing for backend writes, and API credentials for various exchanges. ```yaml # config.yaml log: filename: feedhandler.log level: WARNING # Enable UVLoop for better performance (Linux/Mac) uvloop: True # Ignore invalid/delisted symbols instead of raising errors ignore_invalid_instruments: False # Use multiprocessing for backend writes backend_multiprocessing: False # Exchange API credentials binance: key_id: "your_api_key" key_secret: "your_api_secret" binance_futures: key_id: "your_api_key" key_secret: "your_api_secret" coinbase: key_id: "your_api_key" key_secret: "your_api_secret" key_passphrase: "your_passphrase" kraken: key_id: "your_api_key" key_secret: "your_api_secret" okx: key_id: "your_api_key" key_secret: "your_api_secret" key_passphrase: "your_passphrase" deribit: key_id: "your_api_key" key_secret: "your_api_secret" ``` -------------------------------- ### Install Cryptofeed with PostgreSQL Backend Source: https://github.com/bmoscon/cryptofeed/blob/master/INSTALL.md Install Cryptofeed with support for the PostgreSQL backend. ```bash pip install --user --upgrade cryptofeed[postgres] ``` -------------------------------- ### Initialize Exchange Clients and Get Balances Source: https://context7.com/bmoscon/cryptofeed/llms.txt Initialize exchange clients using a configuration file and retrieve account balances. Iterate through balances to print assets with a free amount greater than zero. ```python from cryptofeed.exchanges import Binance, Coinbase from decimal import Decimal # Initialize with API credentials binance = Binance(config='config.yaml') coinbase = Coinbase(config='config.yaml') # Get account balances balances = binance.balances_sync() for balance in balances: if float(balance['free']) > 0: print(f"{balance['asset']}: {balance['free']} (locked: {balance['locked']})") ``` -------------------------------- ### Install Cryptofeed with QuasarDB Backend Source: https://github.com/bmoscon/cryptofeed/blob/master/INSTALL.md Install Cryptofeed with support for the QuasarDB backend. ```bash pip install --user --upgrade cryptofeed[quasardb] ``` -------------------------------- ### Install Cryptofeed with Google Cloud Pub/Sub Backend Source: https://github.com/bmoscon/cryptofeed/blob/master/INSTALL.md Install Cryptofeed with support for the Google Cloud Pub/Sub backend. ```bash pip install --user --upgrade cryptofeed[gcp_pubsub] ``` -------------------------------- ### Install Cryptofeed with MongoDB Backend Source: https://github.com/bmoscon/cryptofeed/blob/master/INSTALL.md Install Cryptofeed with support for the MongoDB backend. ```bash pip install --user --upgrade cryptofeed[mongo] ``` -------------------------------- ### Configure Feeds with Subscription Dictionaries Source: https://context7.com/bmoscon/cryptofeed/llms.txt Shows how to use subscription dictionaries for granular control, specifying different symbols per channel within a single feed. Includes examples for Coinbase and Binance Futures, covering trades, order books, tickers, funding rates, and open interest. ```python from cryptofeed import FeedHandler from cryptofeed.defines import TRADES, L2_BOOK, L3_BOOK, TICKER, FUNDING, OPEN_INTEREST from cryptofeed.exchanges import Coinbase, BinanceFutures async def trade_handler(t, ts): print(f"[{t.timestamp}] {t.exchange} {t.symbol}: {t.side} {t.amount} @ {t.price}") async def book_handler(book, ts): bid = book.book.bids.index(0) ask = book.book.asks.index(0) print(f"{book.symbol} Spread: {ask[0] - bid[0]}") async def funding_handler(f, ts): print(f"Funding: {f.symbol} Rate: {f.rate} Next: {f.next_funding_time}") async def oi_handler(oi, ts): print(f"Open Interest: {oi.symbol} = {oi.open_interest}") fh = FeedHandler() # Use subscription dict for different symbols per channel fh.add_feed(Coinbase( subscription={ L2_BOOK: ['BTC-USD', 'ETH-USD'], L3_BOOK: ['LTC-USD'], TRADES: ['BTC-USD', 'ETH-USD', 'LTC-USD'], TICKER: ['BTC-USD'] }, callbacks={ TRADES: trade_handler, L2_BOOK: book_handler, L3_BOOK: book_handler, TICKER: lambda t, ts: print(f"Ticker: {t.bid}/{t.ask}") } )) # Futures exchange with derivatives-specific channels fh.add_feed(BinanceFutures( symbols=['BTC-USDT-PERP', 'ETH-USDT-PERP'], channels=[TRADES, FUNDING, OPEN_INTEREST], callbacks={ TRADES: trade_handler, FUNDING: funding_handler, OPEN_INTEREST: oi_handler } )) fh.run() ``` -------------------------------- ### Install Cryptofeed with Pip Source: https://github.com/bmoscon/cryptofeed/blob/master/INSTALL.md Use this command to install or upgrade the Cryptofeed library via pip. ```bash pip install --user --upgrade cryptofeed ``` -------------------------------- ### Example Subscription Dictionary Source: https://github.com/bmoscon/cryptofeed/blob/master/docs/high_level.md Use this dictionary format to specify granular subscriptions for different data channels and symbols. Ensure you do not use 'channels' and 'symbols' arguments if 'subscription' is provided. ```python { TRADES: ['BTC-USD', 'BTC-USDT', 'ETH-USD'], L2_BOOK: ['BTC-USD'] } ``` -------------------------------- ### Setup Simple Feedhandler with Coinbase Source: https://github.com/bmoscon/cryptofeed/blob/master/docs/high_level.md Use this snippet to set up a basic feed handler and subscribe to trades and ticker data for a specific symbol on Coinbase. Ensure you have the necessary imports. ```python from cryptofeed import FeedHandler from cryptofeed.exchanges import Coinbase from cryptofeed.defines import TRADES, TICKER async def ticker(t, receipt_timestamp): print(t) async def trade(t, receipt_timestamp): print(t) def main(): f = FeedHandler() f.add_feed(Coinbase(symbols=['BTC-USD'], channels=[TRADES, TICKER], callbacks={TICKER: ticker, TRADES: trade})) f.run() if __name__ == '__main__': main() ``` -------------------------------- ### ZeroMQ Backend Configuration Source: https://context7.com/bmoscon/cryptofeed/llms.txt Configure ZeroMQ backends to publish L2 book and ticker data over ZeroMQ sockets. This example includes a receiver process to demonstrate data reception. ```python from multiprocessing import Process from yapic import json from cryptofeed import FeedHandler from cryptofeed.backends.zmq import BookZMQ, TickerZMQ from cryptofeed.defines import L2_BOOK, TICKER from cryptofeed.exchanges import Coinbase, Kraken def zmq_receiver(port): """Subscriber process to receive ZMQ messages""" import zmq ctx = zmq.Context.instance() socket = ctx.socket(zmq.SUB) socket.setsockopt(zmq.SUBSCRIBE, b'') # Subscribe to all topics socket.bind(f'tcp://127.0.0.1:{port}') while True: data = socket.recv_string() key, msg = data.split(" ", 1) print(f"Topic: {key}") print(f"Data: {json.loads(msg)}") def main(): # Start receiver in separate process receiver = Process(target=zmq_receiver, args=(5678,)) receiver.start() try: fh = FeedHandler() fh.add_feed(Kraken( max_depth=2, symbols=['BTC-USD', 'ETH-USD'], channels=[L2_BOOK], callbacks={ L2_BOOK: BookZMQ( snapshots_only=False, snapshot_interval=2, port=5678 ) } )) fh.add_feed(Coinbase( symbols=['BTC-USD'], channels=[TICKER], callbacks={TICKER: TickerZMQ(port=5678)} )) fh.run() finally: receiver.terminate() if __name__ == '__main__': main() ``` -------------------------------- ### NBBO Feed Handler Setup Source: https://github.com/bmoscon/cryptofeed/blob/master/README.md Shows how to set up and run a synthetic National Best Bid/Offer (NBBO) feed using FeedHandler. This aggregates best bids and asks from specified exchanges and symbols, using a provided callback function for updates. ```python from cryptofeed import FeedHandler from cryptofeed.exchanges import Coinbase, Gemini, Kraken def nbbo_update(symbol, bid, bid_size, ask, ask_size, bid_feed, ask_feed): print(f'Pair: {symbol} Bid Price: {bid:.2f} Bid Size: {bid_size:.6f} Bid Feed: {bid_feed} Ask Price: {ask:.2f} Ask Size: {ask_size:.6f} Ask Feed: {ask_feed}') def main(): f = FeedHandler() f.add_nbbo([Coinbase, Kraken, Gemini], ['BTC-USD'], nbbo_update) f.run() ``` -------------------------------- ### Get All Trading Symbols from Binance Source: https://context7.com/bmoscon/cryptofeed/llms.txt Retrieve all available trading symbols for the Binance exchange. The number of symbols and a sample list are printed. ```python from cryptofeed.exchanges import Binance, Coinbase, Kraken, Deribit # Get all available trading symbols binance_symbols = Binance.symbols() print(f"Binance has {len(binance_symbols)} symbols") print(f"Sample symbols: {binance_symbols[:5]}") ``` -------------------------------- ### Implement Huobi Subscription Logic Source: https://github.com/bmoscon/cryptofeed/blob/master/docs/exchange.md Customize the subscribe method to handle exchange-specific subscription messages. This example shows how to format subscription requests based on channel and symbol. ```python async def subscribe(self, conn: AsyncConnection): self.__reset() client_id = 0 for chan, symbols in self.subscription.items(): for symbol in symbols: client_id += 1 await conn.write(json.dumps( { "sub": f"market.{symbol}.{chan}", "id": client_id } )) ``` -------------------------------- ### Subscribe to Candlestick Data Source: https://context7.com/bmoscon/cryptofeed/llms.txt Subscribe to candlestick (K-line) data, with an option to filter for closed candles only. This example shows how to configure feeds for Binance and Bybit, specifying symbols, channels, and callbacks. ```python from cryptofeed import FeedHandler from cryptofeed.defines import CANDLES from cryptofeed.exchanges import Binance, Bybit, OKX async def candle_handler(candle, receipt_timestamp): print(f"Candle {candle.exchange} {candle.symbol} [{candle.interval}]:") print(f" Time: {candle.start} - {candle.stop}") print(f" OHLCV: {candle.open}/{candle.high}/{candle.low}/{candle.close} Vol: {candle.volume}") print(f" Trades: {candle.trades}, Closed: {candle.closed}") fh = FeedHandler() # Binance candles - only emit when candle closes fh.add_feed(Binance( candle_closed_only=True, symbols=['BTC-USDT', 'ETH-USDT'], channels=[CANDLES], callbacks={CANDLES: candle_handler} )) # Bybit candles with closed_only option fh.add_feed(Bybit( candle_closed_only=True, symbols=['BTC-USDT-PERP'], channels=[CANDLES], callbacks={CANDLES: candle_handler} )) fh.run() ``` -------------------------------- ### Get Open Orders and Check Order Status Source: https://context7.com/bmoscon/cryptofeed/llms.txt Retrieve a list of open orders for a given symbol and check the status of a specific order using its ID. ```python # Get open orders orders = binance.orders_sync(symbol='BTC-USDT') for order in orders: print(f"Open order: {order}") # Check order status status = binance.order_status_sync(order_id='12345') print(f"Order status: {status}") ``` -------------------------------- ### Get Trade History from Coinbase Source: https://context7.com/bmoscon/cryptofeed/llms.txt Retrieve the trade history for a specified symbol from Coinbase. Each trade entry includes the side, amount, and price. ```python # Get trade history trades = coinbase.trade_history_sync(symbol='BTC-USD') for trade in trades: print(f"Fill: {trade['side']} {trade['amount']} @ {trade['price']}") ``` -------------------------------- ### Filter Symbols by Prefix on Coinbase Source: https://context7.com/bmoscon/cryptofeed/llms.txt Retrieve all trading symbols for Coinbase and filter them to find pairs starting with 'BTC-'. This is useful for focusing on specific trading pairs. ```python # Filter symbols by type coinbase_symbols = Coinbase.symbols() btc_pairs = [s for s in coinbase_symbols if s.startswith('BTC-')] print(f"BTC pairs on Coinbase: {btc_pairs}") ``` -------------------------------- ### Get Exchange Information from Binance Source: https://context7.com/bmoscon/cryptofeed/llms.txt Retrieve detailed exchange information for Binance, including supported websocket and REST API channels. This helps in understanding the exchange's capabilities. ```python # Get detailed exchange info info = Binance.info() print(f"Supported channels (websocket): {info['channels']['websocket']}") print(f"Supported channels (rest): {info['channels']['rest']}") ``` -------------------------------- ### Persist Data to Redis Source: https://context7.com/bmoscon/cryptofeed/llms.txt Persist various types of trading data directly to Redis using different backend configurations like sorted sets or streams. This example demonstrates storing trades, order books, and book snapshots. ```python from cryptofeed import FeedHandler from cryptofeed.backends.redis import ( TradeRedis, BookRedis, BookStream, TickerRedis, FundingRedis, OpenInterestRedis, CandlesRedis, BookSnapshotRedisKey ) from cryptofeed.defines import TRADES, L2_BOOK, TICKER, FUNDING, OPEN_INTEREST, CANDLES from cryptofeed.exchanges import Coinbase, Binance, Bitmex fh = FeedHandler(config={'backend_multiprocessing': True}) # Trades to Redis sorted set fh.add_feed(Coinbase( symbols=['BTC-USD'], channels=[TRADES], callbacks={TRADES: TradeRedis()} )) # Order book to Redis stream fh.add_feed(Coinbase( symbols=['BTC-USD'], channels=[L2_BOOK], callbacks={L2_BOOK: BookStream()} )) # Book snapshots only (no deltas) fh.add_feed(Binance( max_depth=10, symbols=['BTC-USDT'], channels=[L2_BOOK], callbacks={L2_BOOK: BookRedis(snapshots_only=True)} )) ``` -------------------------------- ### Synchronous REST API Data Access Source: https://context7.com/bmoscon/cryptofeed/llms.txt Access historical and real-time market data using synchronous REST API calls. Examples include fetching tickers, order books, trades, and candles. ```python from cryptofeed.exchanges import Coinbase, Binance from decimal import Decimal # Initialize exchange (REST methods available without websocket) coinbase = Coinbase() binance = Binance() # Get current ticker (synchronous) ticker = coinbase.ticker_sync('BTC-USD') print(f"Ticker: Bid={ticker.bid} Ask={ticker.ask}") # Get L2 order book book = coinbase.l2_book_sync('BTC-USD') print(f"Best Bid: {book.book.bids.index(0)}") print(f"Best Ask: {book.book.asks.index(0)}") # Get L3 order book (full order details) l3_book = coinbase.l3_book_sync('LTC-USD') # Get recent trades for trades in coinbase.trades_sync('BTC-USD'): for trade in trades: print(f"Trade: {trade.side} {trade.amount} @ {trade.price}") # Get historical trades with time range from datetime import datetime, timedelta end = datetime.utcnow() start = end - timedelta(hours=1) for trades in binance.trades_sync('BTC-USDT', start=start, end=end): for trade in trades: print(f"{trade['timestamp']}: {trade['side']} {trade['amount']} @ {trade['price']}") # Get candles/OHLCV data for candles in coinbase.candles_sync('BTC-USD', start=start, end=end, interval='1h'): for candle in candles: print(f"Candle: O={candle.open} H={candle.high} L={candle.low} C={candle.close}") # Async versions also available import asyncio async def get_data(): ticker = await coinbase.ticker('ETH-USD') book = await coinbase.l2_book('ETH-USD') async for trades in coinbase.trades('ETH-USD', start=start, end=end): for trade in trades: print(trade) asyncio.run(get_data()) ``` -------------------------------- ### Initialize and Run FeedHandler with Multiple Exchanges Source: https://context7.com/bmoscon/cryptofeed/llms.txt Demonstrates initializing the FeedHandler with custom configuration and adding feeds from different exchanges (Coinbase, Binance, Kraken) with specified symbols and channels. Includes definitions for trade, book, and ticker callback functions. ```python from cryptofeed import FeedHandler from cryptofeed.defines import TRADES, L2_BOOK, TICKER from cryptofeed.exchanges import Coinbase, Binance, Kraken # Define callback functions to handle incoming data async def trade_callback(trade, receipt_timestamp): print(f"Trade: {trade.exchange} {trade.symbol} {trade.side} " f"{trade.amount} @ {trade.price} - ID: {trade.id}") async def book_callback(book, receipt_timestamp): print(f"Book: {book.exchange} {book.symbol} " f"Best Bid: {book.book.bids.index(0)[0]} " f"Best Ask: {book.book.asks.index(0)[0]}") async def ticker_callback(ticker, receipt_timestamp): print(f"Ticker: {ticker.exchange} {ticker.symbol} " f"Bid: {ticker.bid} Ask: {ticker.ask}") # Initialize FeedHandler with optional configuration config = { 'log': {'filename': 'cryptofeed.log', 'level': 'INFO'}, 'uvloop': True, 'backend_multiprocessing': False } fh = FeedHandler(config=config) # Add feeds using exchange classes with symbols and channels fh.add_feed(Coinbase( symbols=['BTC-USD', 'ETH-USD'], channels=[TRADES, L2_BOOK], callbacks={TRADES: trade_callback, L2_BOOK: book_callback} )) fh.add_feed(Binance( symbols=['BTC-USDT'], channels=[TICKER, TRADES], callbacks={TICKER: ticker_callback, TRADES: trade_handler} )) # Alternative: Add feed using string name fh.add_feed('KRAKEN', symbols=['BTC-USD'], channels=[TRADES], callbacks={TRADES: trade_callback}) # Start the feed handler (blocks until interrupted) fh.run() ``` -------------------------------- ### Configure PostgreSQL Backend for Trades and Ticker Source: https://context7.com/bmoscon/cryptofeed/llms.txt Set up Cryptofeed to store trades and ticker data in PostgreSQL using standard configurations. ```python fh.add_feed(Binance( symbols=['BTC-USDT'], channels=[TRADES, TICKER], callbacks={ TRADES: TradePostgres(**postgres_cfg), TICKER: TickerPostgres(**postgres_cfg) } )) ``` -------------------------------- ### Create and Serialize OrderBook Object Source: https://context7.com/bmoscon/cryptofeed/llms.txt Instantiate an OrderBook object and populate its bids and asks. Then, serialize the full order book to a dictionary for storage or transmission. ```python book = OrderBook('COINBASE', 'BTC-USD') book.book.bids = {Decimal('41000'): Decimal('1.5'), Decimal('40900'): Decimal('2.0')} book.book.asks = {Decimal('41100'): Decimal('1.0'), Decimal('41200'): Decimal('0.5')} book.timestamp = 1704067200.0 book_dict = book.to_dict() ``` -------------------------------- ### Place Market Order with Binance Source: https://context7.com/bmoscon/cryptofeed/llms.txt Place a market order on Binance for a specified symbol, side, and amount. Market orders are executed immediately at the best available price. ```python # Place a market order order = binance.place_order_sync( symbol='ETH-USDT', side=SELL, order_type=MARKET, amount=Decimal('0.1') ) ``` -------------------------------- ### MongoDB Backend Configuration Source: https://context7.com/bmoscon/cryptofeed/llms.txt Configure and add MongoDB backends for storing L2 book, trades, and ticker data. Ensure MongoDB is running and accessible. ```python from cryptofeed import FeedHandler from cryptofeed.backends.mongo import BookMongo, TradeMongo, TickerMongo from cryptofeed.defines import L2_BOOK, TRADES, TICKER from cryptofeed.exchanges import Coinbase fh = FeedHandler() fh.add_feed(Coinbase( max_depth=10, symbols=['BTC-USD'], channels=[L2_BOOK, TRADES, TICKER], callbacks={ TRADES: TradeMongo('coinbase', collection='trades'), L2_BOOK: BookMongo('coinbase', collection='l2_book'), TICKER: TickerMongo('coinbase', collection='ticker') } )) fh.run() ``` -------------------------------- ### Place Limit Order with Binance Source: https://context7.com/bmoscon/cryptofeed/llms.txt Place a limit order on Binance for a specified symbol, side, amount, and price. The order is set to be good until canceled. ```python # Place a limit order order = binance.place_order_sync( symbol='BTC-USDT', side=BUY, order_type=LIMIT, amount=Decimal('0.001'), price=Decimal('40000'), time_in_force=GOOD_TIL_CANCELED ) print(f"Order placed: {order}") ``` -------------------------------- ### Configure InfluxDB Backend for Funding Rates Source: https://context7.com/bmoscon/cryptofeed/llms.txt Store funding rate data in InfluxDB v2. ```python fh.add_feed(Bitmex( symbols=['BTC-USD-PERP'], channels=[FUNDING], callbacks={FUNDING: FundingInflux(INFLUX_ADDR, ORG, BUCKET, TOKEN)} )) ``` -------------------------------- ### Kafka Backend Configuration Source: https://context7.com/bmoscon/cryptofeed/llms.txt Configure and add a Kafka backend to the FeedHandler for publishing trades and L2 book data. Ensure Kafka is running and accessible at the specified bootstrap servers. ```python kafka_config = { 'bootstrap_servers': '127.0.0.1:9092', 'acks': 1, 'request_timeout_ms': 10000, 'connections_max_idle_ms': 20000, } fh = FeedHandler({'log': {'filename': 'kafka-demo.log', 'level': 'INFO'}}) fh.add_feed(Coinbase( max_depth=10, symbols=['BTC-USD'], channels=[TRADES, L2_BOOK], callbacks={ TRADES: CustomTradeKafka(client_id='Coinbase Trades', **kafka_config), L2_BOOK: BookKafka(client_id='Coinbase Book', **kafka_config) } )) fh.run() ``` -------------------------------- ### Configure PostgreSQL Backend for Order Book Source: https://context7.com/bmoscon/cryptofeed/llms.txt Store L2 order book data in PostgreSQL with a specified snapshot interval and table name. ```python fh.add_feed(Binance( symbols=['ETH-USDT'], channels=[L2_BOOK], callbacks={ L2_BOOK: BookPostgres(snapshot_interval=100, table='l2_book', **postgres_cfg) } )) ``` -------------------------------- ### Configure InfluxDB Backend for Trades Source: https://context7.com/bmoscon/cryptofeed/llms.txt Set up Cryptofeed to write trade data to InfluxDB v2. ```python fh.add_feed(Coinbase( symbols=['BTC-USD'], channels=[TRADES], callbacks={TRADES: TradeInflux(INFLUX_ADDR, ORG, BUCKET, TOKEN)} )) ``` -------------------------------- ### Configure PostgreSQL Backend for Candles with Custom Mappings Source: https://context7.com/bmoscon/cryptofeed/llms.txt Store candle data in PostgreSQL using custom column mappings for specific fields and a custom table name. ```python fh.add_feed(Binance( symbols=['LTC-USDT'], channels=[CANDLES], callbacks={ CANDLES: CandlesPostgres( **postgres_cfg, custom_columns=column_mappings, table='custom_candles' ) } )) ``` -------------------------------- ### Accessing OrderBook Bids and Asks Source: https://github.com/bmoscon/cryptofeed/blob/master/docs/dtypes.md Demonstrates how to access and iterate through the bid and ask levels of an OrderBook object's internal book structure. Ensure the OrderBook object is properly initialized. ```python ob = OrderBook(.......) # Assuming OrderBook is initialized ob.book.bids.to_dict() # returns dict of this side for price in ob.book.bids: print(f"Price: {price} Size: {ob.book.bids[price]}") for price in ob.book.asks: print(f"Price: {price} Size: {ob.book.asks[price]}") ``` -------------------------------- ### Add Redis Backend Feeds Source: https://context7.com/bmoscon/cryptofeed/llms.txt Configure feeds to use Redis for storing book snapshots and candle data with custom keys. ```python fh.add_feed(Binance( max_depth=10, symbols=['ETH-USDT'], channels=[L2_BOOK], callbacks={L2_BOOK: BookSnapshotRedisKey()} )) ``` ```python fh.add_feed(Binance( candle_closed_only=True, symbols=['BTC-USDT'], channels=[CANDLES], callbacks={CANDLES: CandlesRedis(score_key='start')} )) ``` ```python fh.add_feed(Bitmex( symbols=['BTC-USD-PERP'], channels=[TRADES, FUNDING, OPEN_INTEREST], callbacks={ TRADES: TradeRedis(), FUNDING: FundingRedis(), OPEN_INTEREST: OpenInterestRedis() } )) ``` -------------------------------- ### Configure PostgreSQL Backend for Derivatives Data Source: https://context7.com/bmoscon/cryptofeed/llms.txt Store various derivatives data types including trades, funding rates, open interest, liquidations, and index data in PostgreSQL. ```python fh.add_feed(Bybit( symbols=['BTC-USD-PERP'], channels=[TRADES, FUNDING, OPEN_INTEREST, LIQUIDATIONS, INDEX], callbacks={ TRADES: TradePostgres(**postgres_cfg), FUNDING: FundingPostgres(**postgres_cfg), OPEN_INTEREST: OpenInterestPostgres(**postgres_cfg), LIQUIDATIONS: LiquidationsPostgres(**postgres_cfg), INDEX: IndexPostgres(**postgres_cfg) } )) ``` -------------------------------- ### Create and Serialize Trade Object Source: https://context7.com/bmoscon/cryptofeed/llms.txt Instantiate a Trade object with specific details and serialize it to a dictionary. This is useful for data storage or transmission. ```python trade = Trade( exchange='BINANCE', symbol='BTC-USDT', side='buy', amount=Decimal('0.5'), price=Decimal('42000.50'), timestamp=1704067200.0, id='12345', type='limit' ) trade_dict = trade.to_dict() print(trade_dict) ``` -------------------------------- ### Custom Kafka Callback for Trades Source: https://context7.com/bmoscon/cryptofeed/llms.txt Implement a custom Kafka callback for trades to define a dynamic topic based on exchange and a partition key based on the symbol. ```python class CustomTradeKafka(TradeKafka): def topic(self, data: dict) -> str: return f"{self.key}-{data['exchange']}" def partition_key(self, data: dict) -> Optional[bytes]: return f"{data['symbol']}".encode('utf-8') ``` -------------------------------- ### Configure InfluxDB Backend for Order Books Source: https://context7.com/bmoscon/cryptofeed/llms.txt Store L2 order book data in InfluxDB v2. ```python fh.add_feed(Coinbase( symbols=['BTC-USD'], channels=[L2_BOOK], callbacks={L2_BOOK: BookInflux(INFLUX_ADDR, ORG, BUCKET, TOKEN)} )) ``` -------------------------------- ### Accessing Specific OrderBook Levels Source: https://github.com/bmoscon/cryptofeed/blob/master/docs/dtypes.md Shows how to retrieve specific price and size levels from the order book using the `index` method. The `index` method returns a tuple of (price, size). ```python print("The top level of the order book is:", ob.book.bids.index(0), ob.book.asks.index(0) ``` -------------------------------- ### Access Authenticated Channels (Orders, Balances, Positions) Source: https://context7.com/bmoscon/cryptofeed/llms.txt Access private account data by configuring API credentials and subscribing to authenticated channels like balances, order info, and positions. Ensure your API keys are correctly set up in a config file or passed directly. ```python from cryptofeed import FeedHandler from cryptofeed.defines import BALANCES, ORDER_INFO, POSITIONS, FILLS from cryptofeed.exchanges import Binance, BinanceFutures, Deribit async def balance_handler(balance, receipt_timestamp): print(f"Balance Update: {balance.currency} = {balance.balance} " f"(Reserved: {balance.reserved})") async def order_handler(order, receipt_timestamp): print(f"Order Update: {order.symbol} {order.side} {order.type}") print(f" ID: {order.id} Status: {order.status}") print(f" Price: {order.price} Amount: {order.amount} Remaining: {order.remaining}") async def position_handler(position, receipt_timestamp): print(f"Position: {position.symbol} {position.side}") print(f" Size: {position.position} Entry: {position.entry_price}") print(f" Unrealized PnL: {position.unrealised_pnl}") async def fills_handler(fill, receipt_timestamp): print(f"Fill: {fill.symbol} {fill.side} {fill.amount} @ {fill.price}") print(f" Fee: {fill.fee} Liquidity: {fill.liquidity}") # Create config file (config.yaml) or pass credentials directly # config.yaml: # binance: # key_id: "your_api_key" # key_secret: "your_api_secret" # binance_futures: # key_id: "your_api_key" # key_secret: "your_api_secret" fh = FeedHandler() # Spot account - balances and order updates fh.add_feed(Binance( config='config.yaml', subscription={BALANCES: [], ORDER_INFO: []}, callbacks={BALANCES: balance_handler, ORDER_INFO: order_handler}, timeout=-1 # Disable timeout for auth streams )) # Futures account - includes positions fh.add_feed(BinanceFutures( config='config.yaml', subscription={BALANCES: [], POSITIONS: [], ORDER_INFO: []}, callbacks={ BALANCES: balance_handler, POSITIONS: position_handler, ORDER_INFO: order_handler }, timeout=-1 )) fh.run() ``` -------------------------------- ### Enable Order Book Checksum Validation on Bitget Source: https://context7.com/bmoscon/cryptofeed/llms.txt Integrate Bitget into the FeedHandler with checksum validation enabled for L2 order books. This ensures that the received order book data is accurate and has not been corrupted. ```python # Bitget with checksum validation fh.add_feed(Bitget( checksum_validation=True, symbols=['BTC-USDT'], channels=[L2_BOOK], callbacks={L2_BOOK: book_handler} )) fh.run() ``` -------------------------------- ### Trade Object Hashing and Comparison Source: https://context7.com/bmoscon/cryptofeed/llms.txt Demonstrates that Trade objects are hashable and comparable, allowing them to be used in sets and compared for equality. This is useful for managing unique trades or verifying data integrity. ```python trades = {trade} # Can be used in sets print(trade == trade_restored) ``` -------------------------------- ### Aggregate NBBO Data Across Exchanges Source: https://context7.com/bmoscon/cryptofeed/llms.txt Use this to create a synthetic NBBO feed by aggregating best bid and ask prices from multiple exchanges for a given trading pair. Ensure all necessary exchange classes are imported. ```python from cryptofeed import FeedHandler from cryptofeed.exchanges import Coinbase, Gemini, Kraken, Bitstamp def nbbo_callback(symbol, bid, bid_size, ask, ask_size, bid_feed, ask_feed): """Called when NBBO updates across configured exchanges""" spread = ask - bid print(f"NBBO {symbol}:") print(f" Best Bid: {bid:.2f} ({bid_size:.6f}) from {bid_feed}") print(f" Best Ask: {ask:.2f} ({ask_size:.6f}) from {ask_feed}") print(f" Spread: {spread:.2f} ({(spread/bid)*100:.4f}%)") fh = FeedHandler() # Add NBBO feed across multiple exchanges fh.add_nbbo( feeds=[Coinbase, Kraken, Gemini, Bitstamp], symbols=['BTC-USD', 'ETH-USD'], callback=nbbo_callback ) fh.run() ``` -------------------------------- ### Configure InfluxDB Backend for Candles Source: https://context7.com/bmoscon/cryptofeed/llms.txt Stream candle data to InfluxDB v2, with an option to receive non-closed candles. ```python fh.add_feed(Binance( candle_closed_only=False, symbols=['BTC-USDT'], channels=[CANDLES], callbacks={CANDLES: CandlesInflux(INFLUX_ADDR, ORG, BUCKET, TOKEN)} )) ``` -------------------------------- ### Map Trade Channel to Huobi Source: https://github.com/bmoscon/cryptofeed/blob/master/docs/exchange.md Update the standards.py file to map the generic TRADES channel to the exchange-specific 'trade.detail' format for Huobi. ```python _feed_to_exchange_map = { ... TRADES: { ... HUOBI: 'trade.detail' }, ``` -------------------------------- ### Place Limit Order with Coinbase Source: https://context7.com/bmoscon/cryptofeed/llms.txt Place a limit order on Coinbase with a unique client order ID for tracking purposes. Requires specifying symbol, side, order type, amount, and price. ```python # Coinbase trading coinbase_order = coinbase.place_order_sync( symbol='BTC-USD', side=BUY, order_type=LIMIT, amount=Decimal('0.001'), price=Decimal('40000'), client_order_id='my-order-001' ) ``` -------------------------------- ### Cryptofeed Data Types Source: https://context7.com/bmoscon/cryptofeed/llms.txt Demonstrates the import of various data types provided by Cryptofeed, including Trade, Ticker, OrderBook, Candle, Funding, and OrderInfo. These types are optimized and support serialization. ```python from decimal import Decimal from cryptofeed.types import Trade, Ticker, OrderBook, Candle, Funding, OrderInfo ``` -------------------------------- ### Configure InfluxDB Backend for Ticker Data Source: https://context7.com/bmoscon/cryptofeed/llms.txt Write ticker data to InfluxDB v2. ```python fh.add_feed(Coinbase( symbols=['ETH-USD'], channels=[TICKER], callbacks={TICKER: TickerInflux(INFLUX_ADDR, ORG, BUCKET, TOKEN)} )) ``` -------------------------------- ### Import Huobi Exchange Class Source: https://github.com/bmoscon/cryptofeed/blob/master/docs/exchange.md Add an import statement in exchanges.py to make the newly defined Huobi exchange class available. ```python from cryptofeed.exchanges.huobi import Huobi ``` -------------------------------- ### Accessing Trade Object Fields Source: https://github.com/bmoscon/cryptofeed/blob/master/docs/dtypes.md Demonstrates how to access fields of a Trade object directly as attributes. Ensure the Trade object is instantiated correctly. ```python trade = Trade('COINBASE', 'BTC-USD', 'buy', 1.2, 64342.12, 1634865952.143, id='23454323', type='limit') assert trade.symbol == 'BTC-USD' ``` -------------------------------- ### Converting OrderBook to Dictionary Source: https://github.com/bmoscon/cryptofeed/blob/master/docs/dtypes.md Demonstrates converting the entire OrderBook object to a dictionary using `to_dict()`. The `numeric_type` argument can be used for type conversion of numeric values. ```python ob.to_dict(numeric_type=float) ``` -------------------------------- ### Converting Trade Object to Dictionary Source: https://github.com/bmoscon/cryptofeed/blob/master/docs/dtypes.md Shows how to convert a Trade object into a dictionary representation using the `to_dict()` method. This is useful for serialization or inspection. ```python print(trade.to_dict()) ``` -------------------------------- ### Refresh Symbol List from Binance Source: https://context7.com/bmoscon/cryptofeed/llms.txt Force a refresh of the symbol list from the Binance exchange. This ensures you have the most up-to-date list of available trading pairs. ```python # Refresh symbol list from exchange fresh_symbols = Binance.symbols(refresh=True) ``` -------------------------------- ### Enable Order Book Checksum Validation on Kraken Source: https://context7.com/bmoscon/cryptofeed/llms.txt Configure the FeedHandler to use Kraken with checksum validation enabled for L2 order books. This ensures data integrity by verifying checksums. ```python from cryptofeed import FeedHandler from cryptofeed.defines import L2_BOOK from cryptofeed.exchanges import Kraken, OKX, Bitget async def book_handler(book, receipt_timestamp): print(f"{book.exchange} {book.symbol} checksum verified") fh = FeedHandler() # Kraken with checksum validation fh.add_feed(Kraken( checksum_validation=True, symbols=['BTC-USD'], channels=[L2_BOOK], callbacks={L2_BOOK: book_handler} )) ``` -------------------------------- ### Enable Order Book Checksum Validation on OKX Source: https://context7.com/bmoscon/cryptofeed/llms.txt Add OKX to the FeedHandler with checksum validation enabled for L2 order books. This is crucial for maintaining data integrity during high-frequency trading. ```python # OKX with checksum validation fh.add_feed(OKX( checksum_validation=True, symbols=['BTC-USDT-PERP'], channels=[L2_BOOK], callbacks={L2_BOOK: book_handler} )) ``` -------------------------------- ### Handle Huobi Order Book Updates Source: https://github.com/bmoscon/cryptofeed/blob/master/docs/exchange.md Processes incoming order book data from Huobi, updating the internal representation of bids and asks. It then calls the `book_callback` to deliver the update. ```python elif 'ch' in msg: .... elif 'depth' in msg['ch']: await self._book(msg) async def _book(self, msg): symbol = self.exchange_symbol_to_std_symbol(msg['ch'].split('.')[1]) data = msg['tick'] self._l2_book[symbol] = { BID: sd({ Decimal(price): Decimal(amount) for price, amount in data['bids'] }), ASK: sd({ Decimal(price): Decimal(amount) for price, amount in data['asks'] }) } await self.book_callback(symbol, L2_BOOK, False, False, msg['ts']) ``` -------------------------------- ### Define Huobi Feed Class Source: https://github.com/bmoscon/cryptofeed/blob/master/docs/exchange.md Extend the base Feed class to create a new exchange-specific feed. This involves setting the exchange ID, initializing the websocket connection, and defining reset and subscribe methods. ```python import logging from cryptofeed.feed import Feed from cryptofeed.defines import HUOBI LOG = logging.getLogger('feedhandler') class Huobi(Feed): id = HUOBI def __init__(self, **kwargs): super().__init__('wss://api.huobi.pro/hbus/ws', **kwargs) self.__reset() def __reset(self): pass async def subscribe(self, websocket): self.__reset() ``` -------------------------------- ### Cancel Order with Binance Source: https://context7.com/bmoscon/cryptofeed/llms.txt Cancel a specific order on Binance by providing the order ID and symbol. The result of the cancellation is printed. ```python # Cancel an order result = binance.cancel_order_sync(order_id='12345', symbol='BTC-USDT') print(f"Cancelled: {result}") ``` -------------------------------- ### Serialize Trade to Dictionary with Float Conversion Source: https://context7.com/bmoscon/cryptofeed/llms.txt Serialize a Trade object to a dictionary, converting numeric types to floats. This can be useful for compatibility with systems that expect standard float types. ```python trade_float = trade.to_dict(numeric_type=float) print(trade_float['price']) ``` -------------------------------- ### Define HUOBI Constant Source: https://github.com/bmoscon/cryptofeed/blob/master/docs/exchange.md Add the exchange identifier to the defines module. Exchange names in defines.py are conventionally all uppercase. ```python HUOBI = 'HUOBI' ``` -------------------------------- ### Map L2_BOOK to HUOBI Exchange Source: https://github.com/bmoscon/cryptofeed/blob/master/docs/exchange.md Defines the exchange-specific channel for L2 order book data on Huobi. This is part of the `_feed_to_exchange_map` configuration. ```python _feed_to_exchange_map = { L2_BOOK: { ... HUOBI: 'depth.step0' ``` -------------------------------- ### Converting Trade Object to Dictionary with Numeric Type Conversion Source: https://github.com/bmoscon/cryptofeed/blob/master/docs/dtypes.md Illustrates using the `numeric_type` argument in `to_dict()` to convert numeric fields to strings. This can be useful for specific output formats. ```python print(trade.to_dict(numeric_type=str)) ``` -------------------------------- ### Deserialize Trade Object from Dictionary Source: https://context7.com/bmoscon/cryptofeed/llms.txt Recreate a Trade object from its dictionary representation. This is essential for loading previously saved trade data. ```python trade_restored = Trade.from_dict(trade_dict) ``` -------------------------------- ### Handle Huobi Websocket Messages Source: https://github.com/bmoscon/cryptofeed/blob/master/docs/exchange.md Decompresses incoming messages, handles ping requests, and routes messages for trades or other data. Requires `zlib` and `json` imports. Logs invalid message types. ```python async def _trade(self, msg): """ { 'ch': 'market.btcusd.trade.detail', 'ts': 1549773923965, 'tick': { 'id': 100065340982, 'ts': 1549757127140, 'data': [{'id': '10006534098224147003732', 'amount': Decimal('0.0777'), 'price': Decimal('3669.69'), 'direction': 'buy', 'ts': 1549757127140}]} } """ for trade in msg['tick']['data']: await self.callback(TRADES, feed=self.id, symbol=self.exchange_symbol_to_std_symbol(msg['ch'].split('.')[1]), order_id=trade['id'], side=BUY if trade['direction'] == 'buy' else SELL, amount=Decimal(trade['amount']), price=Decimal(trade['price']), timestamp=trade['ts'] ) async def message_handler(self, msg, conn, timestamp): # unzip message msg = zlib.decompress(msg, 16+zlib.MAX_WBITS) msg = json.loads(msg, parse_float=Decimal) # Huobi sends a ping evert 5 seconds and will disconnect us if we do not respond to it if 'ping' in msg: await conn.write(json.dumps({'pong': msg['ping']})) elif 'status' in msg and msg['status'] == 'ok': return elif 'ch' in msg: if 'trade' in msg['ch']: await self._trade(msg) else: LOG.warning("%s: Invalid message type %s", self.id, msg) ``` -------------------------------- ### Filter Deribit Symbols for Perps and Options Source: https://context7.com/bmoscon/cryptofeed/llms.txt Retrieve symbols from Deribit and filter them to identify perpetual futures ('PERP') and options contracts (containing 'CALL' or 'PUT'). ```python # Deribit perpetuals and futures deribit_symbols = Deribit.symbols() perps = [s for s in deribit_symbols if 'PERP' in s] options = [s for s in deribit_symbols if any(x in s for x in ['CALL', 'PUT', '-C', '-P'])] print(f"Deribit perpetuals: {perps}") ``` -------------------------------- ### Serialize Trade Replacing None Values Source: https://context7.com/bmoscon/cryptofeed/llms.txt Serialize a Trade object to a dictionary, replacing any None values with a specified string. This ensures all fields have a value, useful for data consistency. ```python trade_clean = trade.to_dict(none_to='N/A') ``` -------------------------------- ### REST API Trading Operations Source: https://context7.com/bmoscon/cryptofeed/llms.txt Execute trades and manage orders using authenticated REST endpoints. This requires proper API key configuration and understanding of order parameters. ```python from cryptofeed.exchanges import Binance, BinanceFutures, Coinbase from cryptofeed.defines import BUY, SELL, LIMIT, MARKET, GOOD_TIL_CANCELED from decimal import Decimal ``` -------------------------------- ### Parse Huobi Symbol Data Source: https://github.com/bmoscon/cryptofeed/blob/master/docs/exchange.md Implement the `_parse_symbol_data` class method to convert the raw data from the symbol API endpoint into the normalized symbol mapping required by cryptofeed. This method handles symbol normalization and filters out offline trading pairs. ```python @classmethod def _parse_symbol_data(cls, data: dict, symbol_separator: str) -> Tuple[Dict, Dict]: ret = {} for e in data['data']: if e['state'] == 'offline': continue normalized = f"{e['base-currency'].upper()}{symbol_separator}{e['quote-currency'].upper()}" symbol = f"{e['base-currency']}{e['quote-currency']}" ret[normalized] = symbol return ret, {} ``` -------------------------------- ### Serialize OrderBook Delta for Updates Source: https://context7.com/bmoscon/cryptofeed/llms.txt Serialize only the changes (delta) of an OrderBook object. This is efficient for sending incremental updates rather than the entire order book. ```python book.delta = {'bid': [(Decimal('41000'), Decimal('1.6'))], 'ask': []} delta_dict = book.to_dict(delta=True) ``` -------------------------------- ### Define Huobi Symbol Endpoint Source: https://github.com/bmoscon/cryptofeed/blob/master/docs/exchange.md Set the `symbol_endpoint` class variable to the REST API endpoint used to retrieve trading symbols for the exchange. ```python symbol_endpoint = 'https://poloniex.com/public?command=returnTicker' ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.