-
Notifications
You must be signed in to change notification settings - Fork 3
/
aex-fetcher.py
196 lines (150 loc) · 5.5 KB
/
aex-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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import json
import requests
import websockets
import time
import asyncio
import sys
currency_url = 'https://api.aex.zone/v3/allpair.php'
answer = requests.get(currency_url)
currencies = answer.json()
list_currencies = list()
WS_URL = 'wss://aex2.yxds.net.cn/wsv3'
is_subscribed_orderbooks = {}
is_subscribed_trades = {}
for element in currencies["data"]:
list_currencies.append(element["coin"]+"_"+element["market"])
is_subscribed_trades[element["coin"]+"_"+element["market"]] = False
is_subscribed_orderbooks[element["coin"] + "_" + element["market"]] = False
#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].upper()] = 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].upper()] = 0
# get metadata about each pair of symbols
async def metadata():
for pair in currencies["data"]:
pair_data = '@MD ' + pair["coin"].upper() + '_' + pair["market"].upper() + ' spot ' + \
pair["coin"].upper() + ' ' + pair["market"].upper() + \
' ' + str(pair["limits"]['PricePrecision']) + ' 1 1 0 0'
print(pair_data, flush=True)
print('@MDEND')
async def subscribe(ws):
while True:
for key, value in is_subscribed_trades.items():
if value == False:
# create the subscription for trades
await ws.send(json.dumps({
"cmd": 1,
"action": "sub",
"symbol": f"{key}"
}))
if is_subscribed_orderbooks[key] == False:
# create the subscription for full orderbooks
await ws.send(json.dumps({
"cmd": 3,
"action": "sub",
"symbol": f"{key}"
}))
await asyncio.sleep(0.1)
else:
pass
for el in list(is_subscribed_trades):
is_subscribed_trades[el] = False
for el in list(is_subscribed_orderbooks):
is_subscribed_orderbooks[el] = False
await asyncio.sleep(2000)
def get_unix_time():
return round(time.time() * 1000)
def get_trades(var):
trade_data = var
if 'trade' in trade_data:
for elem in trade_data["trade"]:
print('!', get_unix_time(), trade_data['symbol'].upper(),
"B" if elem[3] == "buy" else "S", elem[2],
elem[1], flush=True)
symbol_trade_count_for_5_minutes[trade_data['symbol'].upper()] += 1
def get_order_books(var, update):
order_data = var
if 'asks' in order_data['depth'] and len(order_data["params"]["asks"]) != 0:
symbol_orderbook_count_for_5_minutes[order_data['symbol'].upper()] += len(order_data["params"]["asks"])
order_answer = '$ ' + str(get_unix_time()) + " " + order_data['symbol'].upper() + ' S '
pq = "|".join(el[0] + "@" + el[1] for el in order_data["depth"]["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['depth'] and len(order_data["depth"]["bids"]) != 0:
symbol_orderbook_count_for_5_minutes[order_data['symbol'].upper()] += len(order_data["depth"]["bids"])
order_answer = '$ ' + str(get_unix_time()) + " " + order_data['symbol'].upper() + ' B '
pq = "|".join(el[0] + "@" + el[1] for el in order_data["depth"]["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)
#trade and orderbook stats output
async def print_stats():
time_to_wait = (5 - ((time.time() / 60) % 5)) * 60
if time_to_wait != 300:
await asyncio.sleep(time_to_wait)
while True:
data1 = "# LOG:CAT=trades_stats:MSG= "
data2 = " ".join(
key.upper() + ":" + str(value) for key, value in symbol_trade_count_for_5_minutes.items() if value != 0)
sys.stdout.write(data1 + data2)
sys.stdout.write("\n")
for key in symbol_trade_count_for_5_minutes:
symbol_trade_count_for_5_minutes[key] = 0
data3 = "# LOG:CAT=orderbooks_stats:MSG= "
data4 = " ".join(
key.upper() + ":" + str(value) for key, value in symbol_orderbook_count_for_5_minutes.items() if
value != 0)
sys.stdout.write(data3 + data4)
sys.stdout.write("\n")
for key in symbol_orderbook_count_for_5_minutes:
symbol_orderbook_count_for_5_minutes[key] = 0
await asyncio.sleep(300)
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())
# 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:
try:
data = await ws.recv()
dataJSON = json.loads(data)
if "trade" in dataJSON or "depth" in dataJSON:
# if received data is about trades
if dataJSON["cmd"] == 1:
is_subscribed_trades[dataJSON["symbol"]] = True
get_trades(dataJSON)
# if received data is about orderbooks
if dataJSON["cmd"] == 3:
is_subscribed_orderbooks[dataJSON["symbol"]] = True
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())