-
Notifications
You must be signed in to change notification settings - Fork 3
/
WazirX-fethcer.py
85 lines (77 loc) · 3.53 KB
/
WazirX-fethcer.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
import json
import websockets
import time
import asyncio
import requests
API_URL = "https://api.wazirx.com"
API_SYMBOLS = "/sapi/v1/exchangeInfo"
WS_URL = "wss://stream.wazirx.com/stream"
TIMEOUT = 0.01
PING_TIMEOUT = 15
async def subscribe(ws, data):
for i in data:
await ws.send(json.dumps({
"event": "subscribe",
"streams": [i]
}))
await asyncio.sleep(TIMEOUT)
async def heartbeat(ws):
while True:
await ws.send(json.dumps({"event":"ping"}))
await asyncio.sleep(PING_TIMEOUT)
async def meta(data):
for i in data:
print("@MD", i['baseAsset'].upper() + "/" +i ['quoteAsset'].upper(), "spot", i['baseAsset'].upper(), i['quoteAsset'].upper(),
i['quoteAssetPrecision'], 1, 1, 0, 0, end='\n')
print("@MDEND")
def print_trades(data):
print("!", round(time.time() * 1000), data['data']['trades'][0]["s"].upper(), data['data']['trades'][0]['S'][0].upper(),
data['data']['trades'][0]["p"], data['data']['trades'][0]['q'], end='\n')
def print_orderbook(data, isSnapshot):
if isSnapshot:
if data['data']['a'] != []:
print("$", round(time.time() * 1000), data['data']['s'].upper(), "S",
"|".join(i[1]+"@"+i[0] for i in data['data']['a']),
"R", end="\n")
if data['data']['b'] != []:
print("$", round(time.time() * 1000), data['data']['s'].upper(), "B",
"|".join(i[1] + "@" + i[0] for i in data['data']['b']),
"R", end="\n")
else:
if data['data']['a'] != []:
print("$", round(time.time() * 1000), data['data']['s'].upper(), "S",
"|".join(i[1] + "@" + i[0] for i in data['data']['a']), end="\n")
if data['data']['b'] != []:
print("$", round(time.time() * 1000), data['data']['s'].upper(), "B",
"|".join(i[1] + "@" + i[0] for i in data['data']['b']), end="\n")
async def main():
try:
response = requests.get(API_URL + API_SYMBOLS)
symbols = [i['symbol'] for i in response.json()['symbols']]
trade_channel = [i + "@trades" for i in symbols]
delta_channel = [i + "@depth10@100ms" for i in symbols]
snapshot_channel = [i + "@depth" for i in symbols]
meta_task = asyncio.create_task(meta(response.json()['symbols']))
async for ws in websockets.connect(WS_URL):
try:
subscribe_task = asyncio.create_task(subscribe(ws, snapshot_channel))
subscribe_task = asyncio.create_task(subscribe(ws, delta_channel))
trade_sub_task = asyncio.create_task(subscribe(ws, trade_channel))
heartbeat_task = asyncio.create_task(heartbeat(ws))
while True:
data = await ws.recv()
dataJSON = json.loads(data)
try:
if dataJSON["stream"] in trade_channel:
print_trades(dataJSON)
elif dataJSON["stream"] in delta_channel:
print_orderbook(dataJSON, 0)
elif dataJSON["stream"] in snapshot_channel:
print_orderbook(dataJSON, 1)
except:
continue
except Exception:
continue
except requests.exceptions.ConnectionError as conn_c:
print(f"WARNING: connection exception {conn_c} occurred")
asyncio.run(main())