forked from rDrayBen/Algohouse-fetchers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexbito-fetcher.py
134 lines (102 loc) · 3.63 KB
/
exbito-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
import json
import requests
import websockets
import time
import asyncio
currency_url = 'https://api.exbito.com/apiv2/markets'
answer = requests.get(currency_url)
currencies = answer.json()
list_currencies = list()
WS_URL = 'wss://wsapi.exbito.com/wsapiv2'
for element in currencies:
list_currencies.append(element["name"])
# get metadata about each pair of symbols
async def metadata():
for pair in currencies:
pair_data = '@MD ' + pair["baseCurrencySymbol"] + '_' + pair["quoteCurrencySymbol"] + ' spot ' + \
pair["baseCurrencySymbol"] + ' ' + pair["quoteCurrencySymbol"] + \
' ' + str(pair['moneyPrec']) + ' 1 1 0 0'
print(pair_data, flush=True)
print('@MDEND')
def get_unix_time():
return round(time.time() * 1000)
def get_trades(var):
trade_data = var
if 'deals' in trade_data["body"]:
for elem in trade_data["body"]["deals"]:
print('!', get_unix_time(), trade_data["body"]['market'],
"S" if elem["type"] == "sell" else "B", elem['price'],
elem["amount"], flush=True)
def get_order_books(var, update):
order_data = var
if 'asks' in order_data['body'] and len(order_data["body"]["asks"]) != 0:
order_answer = '$ ' + str(get_unix_time()) + " " + order_data['body']['market'] + ' S '
pq = "|".join(el[1] + "@" + el[0] for el in order_data["body"]["asks"])
answer = order_answer + pq
# checking if the input data is full orderbook or just update
if (update == True):
print(answer)
else:
print(answer + " R")
if 'bids' in order_data['body'] and len(order_data["body"]["bids"]) != 0:
order_answer = '$ ' + str(get_unix_time()) + " " + order_data['body']['market'] + ' B '
pq = "|".join(el[1] + "@" + el[0] for el in order_data["body"]["bids"])
answer = order_answer + pq
# checking if the input data is full orderbook or just update
if (update == True):
print(answer)
else:
print(answer + " R")
async def heartbeat(ws):
while True:
await ws.send(json.dumps({
"event": "ping"
}))
await asyncio.sleep(5)
async def main():
# create connection with server via base ws url
async for ws in websockets.connect(WS_URL, ping_interval=None):
try:
# create task to keep connection alive
pong = asyncio.create_task(heartbeat(ws))
# create task to get metadata about each pair of symbols
meta_data = asyncio.create_task(metadata())
for i in range(len(list_currencies)):
# create the subscription for trades
await ws.send(json.dumps({
"action": "subscribe",
"channel": "market.deals",
"params": {
"market": f"{list_currencies[i]}",
}
}))
# create the subscription for full orderbooks and updates
await ws.send(json.dumps({
"action": "subscribe",
"channel": "market.depth",
"params": {
"market": f"{list_currencies[i]}",
"interval": "0"
}
}))
while True:
data = await ws.recv()
dataJSON = json.loads(data)
if "event" in dataJSON and dataJSON["event"]!="subscribed" and dataJSON["event"]!="error":
try:
# if received data is about trades
if dataJSON['channel'] == 'market.deals' and dataJSON['event'] == 'insert':
get_trades(dataJSON)
# if received data is about updates
if dataJSON['channel'] == 'market.depth' and dataJSON['event'] == 'update':
get_order_books(dataJSON, update=True)
# if received data is about orderbooks
if dataJSON['channel'] == 'market.depth' and dataJSON['event'] == 'init':
get_order_books(dataJSON, update=False)
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())