Skip to content

Commit 09a9b8f

Browse files
authored
Merge pull request #162 from itsdeka/ob
added full_orderbook.py example
2 parents ab166f7 + d5370b1 commit 09a9b8f

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
1.2.1
2+
-) Added orderbook implementation example (ws)
3+
14
1.2.0
25
-) Implemented Margin Info (rest)
36
-) Implemented claim position (rest)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import os
2+
import sys
3+
import time
4+
from collections import OrderedDict
5+
sys.path.append('../../../')
6+
7+
from bfxapi import Client
8+
9+
bfx = Client(
10+
manageOrderBooks=True
11+
)
12+
13+
class OrderBook:
14+
def __init__(self, snapshot):
15+
self.bids = OrderedDict()
16+
self.asks = OrderedDict()
17+
self.load(snapshot)
18+
19+
def load(self, snapshot):
20+
for record in snapshot:
21+
if record[2] >= 0:
22+
self.bids[record[0]] = {
23+
'count': record[1],
24+
'amount': record[2]
25+
}
26+
else:
27+
self.asks[record[0]] = {
28+
'count': record[1],
29+
'amount': record[2]
30+
}
31+
32+
def update(self, record):
33+
# count is 0
34+
if record[1] == 0:
35+
if record[2] == 1:
36+
# remove from bids
37+
del self.bids[record[0]]
38+
elif record[2] == -1:
39+
# remove from asks
40+
del self.asks[record[0]]
41+
elif record[1] > 0:
42+
if record[2] > 0:
43+
# update bids
44+
if record[0] not in self.bids:
45+
self.bids[record[0]] = {}
46+
self.bids[record[0]]['count'] = record[1]
47+
self.bids[record[0]]['amount'] = record[2]
48+
elif record[2] < 0:
49+
# update asks
50+
if record[0] not in self.asks:
51+
self.asks[record[0]] = {}
52+
self.asks[record[0]]['count'] = record[1]
53+
self.asks[record[0]]['amount'] = record[2]
54+
55+
obs = {}
56+
57+
@bfx.ws.on('error')
58+
def log_error(err):
59+
print ("Error: {}".format(err))
60+
61+
@bfx.ws.on('order_book_update')
62+
def log_update(data):
63+
obs[data['symbol']].update(data['data'])
64+
65+
@bfx.ws.on('order_book_snapshot')
66+
def log_snapshot(data):
67+
obs[data['symbol']] = OrderBook(data['data'])
68+
69+
async def start():
70+
await bfx.ws.subscribe('book', 'tBTCUSD')
71+
72+
bfx.ws.on('connected', start)
73+
bfx.ws.run()
74+
75+
for n in range(0, 10):
76+
time.sleep(2)
77+
for key in obs:
78+
print(f"Printing {key} orderbook...")
79+
print(f"{obs[key].bids}\n")
80+
print(f"{obs[key].asks}\n")

bfxapi/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
This module contains the current version of the bfxapi lib
33
"""
44

5-
__version__ = '1.2.0'
5+
__version__ = '1.2.1'

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
here = path.abspath(path.dirname(__file__))
1212
setup(
1313
name='bitfinex-api-py',
14-
version='1.2.0',
14+
version='1.2.1',
1515
description='Official Bitfinex Python API',
1616
long_description='A Python reference implementation of the Bitfinex API for both REST and websocket interaction',
1717
long_description_content_type='text/markdown',

0 commit comments

Comments
 (0)