diff --git a/CHANGELOG b/CHANGELOG index 74e320ed..90b8884c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +1.2.1 +-) Added orderbook implementation example (ws) + 1.2.0 -) Implemented Margin Info (rest) -) Implemented claim position (rest) diff --git a/bfxapi/examples/ws/full_orderbook.py b/bfxapi/examples/ws/full_orderbook.py new file mode 100644 index 00000000..d8678e36 --- /dev/null +++ b/bfxapi/examples/ws/full_orderbook.py @@ -0,0 +1,80 @@ +import os +import sys +import time +from collections import OrderedDict +sys.path.append('../../../') + +from bfxapi import Client + +bfx = Client( + manageOrderBooks=True +) + +class OrderBook: + def __init__(self, snapshot): + self.bids = OrderedDict() + self.asks = OrderedDict() + self.load(snapshot) + + def load(self, snapshot): + for record in snapshot: + if record[2] >= 0: + self.bids[record[0]] = { + 'count': record[1], + 'amount': record[2] + } + else: + self.asks[record[0]] = { + 'count': record[1], + 'amount': record[2] + } + + def update(self, record): + # count is 0 + if record[1] == 0: + if record[2] == 1: + # remove from bids + del self.bids[record[0]] + elif record[2] == -1: + # remove from asks + del self.asks[record[0]] + elif record[1] > 0: + if record[2] > 0: + # update bids + if record[0] not in self.bids: + self.bids[record[0]] = {} + self.bids[record[0]]['count'] = record[1] + self.bids[record[0]]['amount'] = record[2] + elif record[2] < 0: + # update asks + if record[0] not in self.asks: + self.asks[record[0]] = {} + self.asks[record[0]]['count'] = record[1] + self.asks[record[0]]['amount'] = record[2] + +obs = {} + +@bfx.ws.on('error') +def log_error(err): + print ("Error: {}".format(err)) + +@bfx.ws.on('order_book_update') +def log_update(data): + obs[data['symbol']].update(data['data']) + +@bfx.ws.on('order_book_snapshot') +def log_snapshot(data): + obs[data['symbol']] = OrderBook(data['data']) + +async def start(): + await bfx.ws.subscribe('book', 'tBTCUSD') + +bfx.ws.on('connected', start) +bfx.ws.run() + +for n in range(0, 10): + time.sleep(2) + for key in obs: + print(f"Printing {key} orderbook...") + print(f"{obs[key].bids}\n") + print(f"{obs[key].asks}\n") diff --git a/bfxapi/version.py b/bfxapi/version.py index f46ec8e6..f00d03ff 100644 --- a/bfxapi/version.py +++ b/bfxapi/version.py @@ -2,4 +2,4 @@ This module contains the current version of the bfxapi lib """ -__version__ = '1.2.0' +__version__ = '1.2.1' diff --git a/setup.py b/setup.py index 4a50b950..b008d751 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ here = path.abspath(path.dirname(__file__)) setup( name='bitfinex-api-py', - version='1.2.0', + version='1.2.1', description='Official Bitfinex Python API', long_description='A Python reference implementation of the Bitfinex API for both REST and websocket interaction', long_description_content_type='text/markdown',