Skip to content

Instantly share code, notes, and snippets.

@ton77v
Created March 22, 2023 05:27
Show Gist options
  • Save ton77v/7fde0a58e534447f50e58c777a9834e6 to your computer and use it in GitHub Desktop.
Save ton77v/7fde0a58e534447f50e58c777a9834e6 to your computer and use it in GitHub Desktop.
figuring out how to work with cryptofeed books
import asyncio
from cryptofeed import FeedHandler
from cryptofeed.defines import L2_BOOK
from cryptofeed.exchanges import Binance, Huobi
def add_feed_test():
# following example from https://github.com/bmoscon/cryptofeed/blob/master/examples/demo_loop.py
async def orderbook_watch(book, receipt_timestamp):
print(f'{receipt_timestamp}: {book.exchange} - {book.symbol}, {len(book.book)} entries')
def stop():
loop.stop()
loop = asyncio.get_event_loop()
book_cb = {L2_BOOK: orderbook_watch}
ticker_code = 'USDC-USDT'
conf = {'log': {'filename': 'cryptofeed.log', 'level': 'DEBUG', 'disabled': False}}
f = FeedHandler(config=conf)
f.run(start_loop=False)
f.add_feed(Binance(symbols=[ticker_code], channels=[L2_BOOK], callbacks=book_cb), loop=loop)
loop.call_later(3, stop)
loop.run_forever()
f.stop(loop=loop)
@ton77v
Copy link
Author

ton77v commented Mar 22, 2023

log after the first run:

2023-03-22 12:19:03,960 : INFO : Config: use dict containing the following main keys: log
2023-03-22 12:19:03,960 : DEBUG : BINANCE: reading symbol information from https://api.binance.com/api/v3/exchangeInfo
2023-03-22 12:19:03,960 : DEBUG : HTTPSync: requesting data from https://api.binance.com/api/v3/exchangeInfo
2023-03-22 12:19:04,246 : DEBUG : BINANCE.ws.1: connecting to wss://stream.binance.com:9443/stream?streams=usdcusdt@depth@100ms
2023-03-22 12:19:05,680 : DEBUG : BINANCE.http.0: create HTTP session
2023-03-22 12:19:05,680 : DEBUG : BINANCE.http.0: requesting data from https://api.binance.com/api/v3/depth?symbol=USDCUSDT&limit=1000
1679462345.8633428: BINANCE - USDC-USDT, 287 entries
....

after the second one:

2023-03-22 12:19:24,230 : INFO : Config: use dict containing the following main keys: log
2023-03-22 12:19:24,230 : INFO : Config: use dict containing the following main keys: log
2023-03-22 12:19:24,230 : DEBUG : BINANCE.ws.3: connecting to wss://stream.binance.com:9443/stream?streams=usdcusdt@depth@100ms
2023-03-22 12:19:24,230 : DEBUG : BINANCE.ws.3: connecting to wss://stream.binance.com:9443/stream?streams=usdcusdt@depth@100ms
2023-03-22 12:19:25,002 : DEBUG : BINANCE.http.2: create HTTP session
2023-03-22 12:19:25,002 : DEBUG : BINANCE.http.2: create HTTP session
2023-03-22 12:19:25,002 : DEBUG : BINANCE.http.2: requesting data from https://api.binance.com/api/v3/depth?symbol=USDCUSDT&limit=1000
2023-03-22 12:19:25,002 : DEBUG : BINANCE.http.2: requesting data from https://api.binance.com/api/v3/depth?symbol=USDCUSDT&limit=1000
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment