-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlevel-2-streaming.py
More file actions
64 lines (56 loc) · 1.93 KB
/
level-2-streaming.py
File metadata and controls
64 lines (56 loc) · 1.93 KB
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
from lomond import WebSocket
from lomond.persist import persist
def parse_snapshot(message):
sub_sections = message.split(':');
message_key = sub_sections[0];
bids_str = sub_sections[1];
asks_str = sub_sections[2];
full_OB = {}
full_OB["bids"] = [[float(item) for item in position.split('~')] for position in bids_str.split(',')]
full_OB["asks"] = [[float(item) for item in position.split('~')] for position in asks_str.split(',')]
return full_OB;
def get_change_flag(flag):
if (flag == '1'):
return 'add'
elif (flag == '2'):
return 'remove'
elif (flag == '4'):
return 'update'
return 'unknown'; #This should not happen
def parse_level_2(message):
data_array = message.split('~');
data_obj = {
"message_type": data_array[0],
"exchange": data_array[1],
"fromsymbol": data_array[2],
"tosymbol": data_array[3],
"side": 'bid' if data_array[4] == '1' else 'ask',
"change": get_change_flag(data_array[5]),
"sequence": int(data_array[6]),
"price": float(data_array[7]),
"quantity": float(data_array[8])
}
return data_obj
cryptocompare_ws = WebSocket('wss://streaming.cryptocompare.com')
subs = {
"action": "SubAdd",
"subs": ["8~bitfinex~BTC~USD"],
"api_key": "YOUR-API-KEY",
"format": "streamer"
}
for event in persist(cryptocompare_ws):
if event.name == "ready":
print('ready')
cryptocompare_ws.send_json(subs)
if event.name == "text":
print(event.text)
messages = event.text.split("|")
messages = messages[:-1] #Last element is empty string
for message in messages:
if message == "999~HEARTBEAT|":
print(message)
elif message.split("~")[0] == "9":
print(parse_snapshot(message))
continue
else:
print(parse_level_2(message))