-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbooks_np.py
70 lines (54 loc) · 2.81 KB
/
books_np.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import asyncio, time, os, json
import websockets, requests, bisect
import numpy as np
order_book = {'bids': [], 'asks': []}
async def update_order_book(side, changes):
for change in changes:
price, quantity = map(float, change)
# Find the index of any existing bid/ask with the same price
index = next((i for i, item in enumerate(order_book[side]) if item[0] == price), None)
if quantity == 0:
if index is not None:
# Remove the bid/ask if it exists
order_book[side].pop(index)
else:
if index is not None:
# Update the quantity of the existing bid/ask
order_book[side][index] = (price, quantity)
else:
# Add a new bid/ask
order_book[side].append((price, quantity))
order_book[side].sort(key=lambda x: x[0], reverse=(side == 'bids'))
order_book[side] = order_book[side][:500]
async def manage_order_book():
uri = "wss://fstream.binance.com/stream?streams=btcusdt@depth@100ms"
async with websockets.connect(uri) as websocket:
response = requests.get("https://fapi.binance.com/fapi/v1/depth?symbol=BTCUSDT&limit=500")
data = response.json()
lastUpdateId = data['lastUpdateId']
await asyncio.gather(update_order_book('bids', data['bids']), update_order_book('asks', data['asks']))
while True:
event = await websocket.recv()
event_data = json.loads(event)
stream = event_data['data']
final_id = stream['u']
first_id = stream['U']
previous_final_id = stream['pu']
if final_id < lastUpdateId:
continue
if first_id <= lastUpdateId and final_id >= lastUpdateId:
await asyncio.gather(update_order_book('bids', stream['b']), update_order_book('asks', stream['a']))
lastUpdateId = final_id
continue
if previous_final_id != lastUpdateId:
response = requests.get("https://fapi.binance.com/fapi/v1/depth?symbol=BTCUSDT&limit=500")
data = response.json()
await asyncio.gather(update_order_book('bids', data['bids']), update_order_book('asks', data['asks']))
lastUpdateId = data['lastUpdateId']
await asyncio.gather(update_order_book('bids', stream['b']), update_order_book('asks', stream['a']))
lastUpdateId = final_id
asyncio.create_task(calculate_liquidity(order_book))
async def calculate_liquidity(order_book):
#print("\n", order_book['asks'][25], order_book['bids'][25])
print("Sum of asks: ", round(np.sum([x[1] for x in order_book['asks']])), "Sum of bids: ", round(np.sum([x[1] for x in order_book['bids']])))
asyncio.run(manage_order_book())