-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummarize_stream.py
More file actions
77 lines (63 loc) · 2.95 KB
/
summarize_stream.py
File metadata and controls
77 lines (63 loc) · 2.95 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
65
66
67
68
69
70
71
72
73
74
75
76
import struct
import sys
def summarize_stream(file_path):
"""
Parses and summarizes the content of a binary .bin file.
"""
print(f"Summarizing file: {file_path}")
try:
with open(file_path, 'rb') as f:
count = 0
checkpoint_count = 100000
while True:
count += 1
if count % checkpoint_count == 0:
# Save to checkpoint ckpt_{count}.bin
print(f"Processed {count} messages...")
header = f.read(8)
if not header:
break
if len(header) < 8:
print(f"Incomplete header found: {header.hex()}")
break
seq_no, msg_size = struct.unpack('<II', header)
msg_data = f.read(msg_size)
if len(msg_data) < msg_size:
print(f"Incomplete message data for seq_no {seq_no}. Expected {msg_size}, got {len(msg_data)}")
break
msg_type = chr(msg_data[0])
print(f"SeqNo: {seq_no}, MsgSize: {msg_size}, MsgType: {msg_type}", end='')
if msg_type == 'A' or msg_type == 'U':
# ADD or UPDATE
symbol = msg_data[1:4].decode('ascii').strip()
order_id = struct.unpack('<Q', msg_data[4:12])[0]
side = chr(msg_data[12])
volume = struct.unpack('<Q', msg_data[16:24])[0]
price = struct.unpack('<i', msg_data[24:28])[0]
print(f", Symbol: {symbol}, OrderId: {order_id}, Side: {side}, Volume: {volume}, Price: {price}")
elif msg_type == 'D':
# DELETE
symbol = msg_data[1:4].decode('ascii').strip()
order_id = struct.unpack('<Q', msg_data[4:12])[0]
side = chr(msg_data[12])
print(f", Symbol: {symbol}, OrderId: {order_id}, Side: {side}")
elif msg_type == 'E':
# EXECUTE
symbol = msg_data[1:4].decode('ascii').strip()
order_id = struct.unpack('<Q', msg_data[4:12])[0]
side = chr(msg_data[12])
traded_qty = struct.unpack('<Q', msg_data[16:24])[0]
print(f", Symbol: {symbol}, OrderId: {order_id}, Side: {side}, TradedQty: {traded_qty}")
else:
print(f", Unknown message type: {msg_type}")
except FileNotFoundError:
print(f"Error: File not found at {file_path}")
except Exception as e:
print(f"An error occurred: {e}")
print("-" * 20)
if __name__ == "__main__":
if len(sys.argv) > 1:
for file_path in sys.argv[1:]:
summarize_stream(file_path)
else:
print("Usage: python summarize_stream.py <file1.bin> [<file2.bin> ...]")