mirror of
https://github.com/aljazceru/bitfinex-api-py.git
synced 2025-12-19 14:54:21 +01:00
Rewrite, edit and organize examples/websocket demos.
This commit is contained in:
65
examples/websocket/public/raw_order_book.py
Normal file
65
examples/websocket/public/raw_order_book.py
Normal file
@@ -0,0 +1,65 @@
|
||||
# python -c "import examples.websocket.public.raw_order_book"
|
||||
|
||||
from collections import OrderedDict
|
||||
|
||||
from typing import List
|
||||
|
||||
from bfxapi import Client, PUB_WSS_HOST
|
||||
|
||||
from bfxapi.websocket import subscriptions
|
||||
from bfxapi.websocket.enums import Channel, Error
|
||||
from bfxapi.websocket.types import TradingPairRawBook
|
||||
|
||||
class RawOrderBook(object):
|
||||
def __init__(self, symbols: List[str]):
|
||||
self.__raw_order_book = {
|
||||
symbol: {
|
||||
"bids": OrderedDict(), "asks": OrderedDict()
|
||||
} for symbol in symbols
|
||||
}
|
||||
|
||||
def update(self, symbol: str, data: TradingPairRawBook) -> None:
|
||||
order_id, price, amount = data.order_id, data.price, data.amount
|
||||
|
||||
kind = (amount > 0) and "bids" or "asks"
|
||||
|
||||
if price > 0:
|
||||
self.__raw_order_book[symbol][kind][order_id] = {
|
||||
"order_id": order_id,
|
||||
"price": price,
|
||||
"amount": amount
|
||||
}
|
||||
|
||||
if price == 0:
|
||||
if order_id in self.__raw_order_book[symbol][kind]:
|
||||
del self.__raw_order_book[symbol][kind][order_id]
|
||||
|
||||
SYMBOLS = [ "tBTCUSD", "tLTCUSD", "tLTCBTC", "tETHUSD", "tETHBTC" ]
|
||||
|
||||
raw_order_book = RawOrderBook(symbols=SYMBOLS)
|
||||
|
||||
bfx = Client(WSS_HOST=PUB_WSS_HOST)
|
||||
|
||||
@bfx.wss.on("wss-error")
|
||||
def on_wss_error(code: Error, msg: str):
|
||||
print(code, msg)
|
||||
|
||||
@bfx.wss.on("open")
|
||||
async def on_open():
|
||||
for symbol in SYMBOLS:
|
||||
await bfx.wss.subscribe(Channel.BOOK, symbol=symbol, prec="R0")
|
||||
|
||||
@bfx.wss.on("subscribed")
|
||||
def on_subscribed(subscription):
|
||||
print(f"Subscription successful for pair <{subscription['pair']}>")
|
||||
|
||||
@bfx.wss.on("t_raw_book_snapshot")
|
||||
def on_t_raw_book_snapshot(subscription: subscriptions.Book, snapshot: List[TradingPairRawBook]):
|
||||
for data in snapshot:
|
||||
raw_order_book.update(subscription["symbol"], data)
|
||||
|
||||
@bfx.wss.on("t_raw_book_update")
|
||||
def on_t_raw_book_update(subscription: subscriptions.Book, data: TradingPairRawBook):
|
||||
raw_order_book.update(subscription["symbol"], data)
|
||||
|
||||
bfx.wss.run()
|
||||
Reference in New Issue
Block a user