-
Notifications
You must be signed in to change notification settings - Fork 3
/
bib-fetcher.py
161 lines (131 loc) · 5.13 KB
/
bib-fetcher.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import json
import requests
import websockets
import time
import asyncio
from CommonFunctions.CommonFunctions import get_unix_time, stats
import os
currency_url = 'https://quotation.bibvip.com/api/v1/allticker'
answer = requests.get(currency_url)
currencies = answer.json()
list_currencies = list()
list_currencies_meta = list()
WS_URL = 'wss://ws.bibvip5.com/kline-api/ws'
for elem in currencies["ticker"]:
list_currencies.append(elem["symbol"].split("_")[0].upper() + elem["symbol"].split("_")[1].upper())
list_currencies_meta.append(elem["symbol"].upper())
# for trades count stats
symbol_trade_count_for_5_minutes = {}
for i in range(len(list_currencies)):
symbol_trade_count_for_5_minutes[list_currencies[i]] = 0
# for orderbooks count stats
symbol_orderbook_count_for_5_minutes = {}
for i in range(len(list_currencies)):
symbol_orderbook_count_for_5_minutes[list_currencies[i]] = 0
# get metadata about each pair of symbols
async def metadata():
for pair in list_currencies_meta:
pair_data = '@MD ' + pair.split("_")[0] + pair.split("_")[1] + ' spot ' + \
pair.split("_")[0] + ' ' + pair.split("_")[1] + \
' -1 1 1 0 0'
print(pair_data, flush=True)
print('@MDEND')
async def subscribe(ws):
for i in range(len(list_currencies)):
# create the subscription for trades
await ws.send(json.dumps({
"event": "sub",
"params": {
"channel": f"market_{list_currencies[i].lower()}_trade_ticker",
"cb_id": f"{list_currencies[i].lower()}",
"top": 100,
"id": "7330d06bf0574236b3bc5cdcb7a58c2f",
"u": "9600251",
"t": 1700218962161
}
}))
# resubscribe if orderbook subscription is not active + possibility to not subscribe or report orderbook changes:
if os.getenv("SKIP_ORDERBOOKS") == None:
# create the subscription for full orderbooks and updates
await ws.send(json.dumps({
"event": "sub",
"params": {
"channel": f"market_{list_currencies[i].lower()}_depth_step0_diff",
"cb_id": f"{list_currencies[i].lower()}",
"id": "4548deb895204237bdf5a90d3058875d",
"u": "2532451",
"t": 1700218962162
}
}))
await asyncio.sleep(2000)
def get_trades(var):
trade_data = var
if 'data' in trade_data["tick"]:
for elem in trade_data["tick"]["data"]:
print('!', get_unix_time(), trade_data["channel"].split("_")[1].upper(),
"B" if elem["side"] == "BUY" else "S", str(elem['price']),
str(elem["amount"]), flush=True)
symbol_trade_count_for_5_minutes[trade_data["channel"].split("_")[1].upper()] += 1
def get_order_books(var):
order_data = var
if 'asks' in order_data['tick'] and len(order_data["tick"]["asks"]) != 0:
symbol_orderbook_count_for_5_minutes[order_data['cb_id'].upper()] += len(order_data["tick"]["asks"])
order_answer = '$ ' + str(get_unix_time()) + " " + order_data['cb_id'].upper() + ' S '
pq = "|".join(str(el[1]) + "@" + str(el[0]) for el in order_data["tick"]["asks"])
answer = order_answer + pq
print(answer + " R")
if 'buys' in order_data['tick'] and len(order_data["tick"]["buys"]) != 0:
symbol_orderbook_count_for_5_minutes[order_data['cb_id'].upper()] += len(order_data["tick"]["buys"])
order_answer = '$ ' + str(get_unix_time()) + " " + order_data['cb_id'].upper() + ' B '
pq = "|".join(str(el[1]) + "@" + str(el[0]) for el in order_data["tick"]["buys"])
answer = order_answer + pq
print(answer + " R")
# trade and orderbook stats output
async def print_stats(symbol_trade_count_for_5_minutes, symbol_orderbook_count_for_5_minutes):
time_to_wait = (5 - ((time.time() / 60) % 5)) * 60
if time_to_wait != 300:
await asyncio.sleep(time_to_wait)
while True:
stats(symbol_trade_count_for_5_minutes, symbol_orderbook_count_for_5_minutes)
time_to_wait = (5 - ((time.time() / 60) % 5)) * 60
await asyncio.sleep(time_to_wait)
# process the situations when the server awaits "ping" request
async def heartbeat(ws):
while True:
await ws.send(json.dumps({
"pong": f"{get_unix_time()}"
}))
await ws.send(json.dumps({
"pong": f"{get_unix_time()}"
}))
await asyncio.sleep(5)
async def main():
# create task to get metadata about each pair of symbols
meta_data = asyncio.create_task(metadata())
# create task to get trades and orderbooks stats output
stats_task = asyncio.create_task(print_stats(symbol_trade_count_for_5_minutes, symbol_orderbook_count_for_5_minutes))
# create connection with server via base ws url
async for ws in websockets.connect(WS_URL, ping_interval=None):
try:
# create task to subscribe to symbols` pair
subscription = asyncio.create_task(subscribe(ws))
# create task to keep connection alive
pong = asyncio.create_task(heartbeat(ws))
while True:
data = await ws.recv()
dataJSON = json.loads(data)
if "channel" in dataJSON:
try:
# if received data is about trades
if dataJSON['channel'].split("_")[2] == 'trade':
get_trades(dataJSON)
# if received data is about full orderbooks
if dataJSON['channel'].split("_")[2] == 'depth':
get_order_books(dataJSON)
else:
pass
except Exception as ex:
print(f"Exception {ex} occurred")
except Exception as conn_ex:
print(f"Connection exception {conn_ex} occurred")
asyncio.run(main())