itchfeed 1.0.0__py3-none-any.whl → 1.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- itch/__init__.py +10 -14
- itch/indicators.py +206 -206
- itch/messages.py +1600 -1600
- itch/parser.py +173 -180
- {itchfeed-1.0.0.dist-info → itchfeed-1.0.2.dist-info}/METADATA +225 -217
- itchfeed-1.0.2.dist-info/RECORD +9 -0
- {itchfeed-1.0.0.dist-info → itchfeed-1.0.2.dist-info}/WHEEL +1 -1
- {itchfeed-1.0.0.dist-info → itchfeed-1.0.2.dist-info}/licenses/LICENSE +21 -21
- itchfeed-1.0.0.dist-info/RECORD +0 -9
- {itchfeed-1.0.0.dist-info → itchfeed-1.0.2.dist-info}/top_level.txt +0 -0
itch/parser.py
CHANGED
@@ -1,180 +1,173 @@
|
|
1
|
-
from queue import Queue
|
2
|
-
from typing import BinaryIO, List
|
3
|
-
|
4
|
-
from itch import
|
5
|
-
from itch.messages import
|
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
|
-
if buffer_len <
|
68
|
-
new_data = file.read(cachesize)
|
69
|
-
if not new_data:
|
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
|
-
if
|
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
|
-
return msg.RetailPriceImprovementIndicator(message)
|
175
|
-
case b"O":
|
176
|
-
return msg.DLCRMessage(message)
|
177
|
-
case _:
|
178
|
-
raise ValueError(
|
179
|
-
f"Unknown message type: {message_type.decode(encoding='ascii')}"
|
180
|
-
)
|
1
|
+
from queue import Queue
|
2
|
+
from typing import BinaryIO, List
|
3
|
+
|
4
|
+
from itch.messages import MESSAGES, MarketMessage
|
5
|
+
from itch.messages import messages as msgs
|
6
|
+
|
7
|
+
|
8
|
+
class MessageParser(object):
|
9
|
+
"""
|
10
|
+
A market message parser for ITCH 5.0 data.
|
11
|
+
|
12
|
+
"""
|
13
|
+
|
14
|
+
def __init__(self, message_type: bytes = MESSAGES):
|
15
|
+
self.message_type = message_type
|
16
|
+
|
17
|
+
def read_message_from_file(
|
18
|
+
self,
|
19
|
+
file: BinaryIO,
|
20
|
+
cachesize: int = 4096,
|
21
|
+
) -> List[MarketMessage]:
|
22
|
+
"""
|
23
|
+
Reads and parses market messages from a binary file-like object.
|
24
|
+
|
25
|
+
This method processes binary data in chunks, extracts individual messages
|
26
|
+
according to a specific format, and returns a list of successfully decoded
|
27
|
+
MarketMessage objects. Parsing stops either when the end of the file is
|
28
|
+
reached or when a system message with an end-of-messages event code is encountered.
|
29
|
+
|
30
|
+
Args:
|
31
|
+
file (BinaryIO):
|
32
|
+
A binary file-like object (opened in binary mode) from which market messages are read.
|
33
|
+
cachesize (int, optional):
|
34
|
+
The size (in bytes) of each data chunk read from the file. Defaults to 4096 bytes.
|
35
|
+
|
36
|
+
Returns:
|
37
|
+
List[MarketMessage]:
|
38
|
+
A list of parsed MarketMessage objects that match the allowed message types
|
39
|
+
defined in self.message_type.
|
40
|
+
|
41
|
+
Raises:
|
42
|
+
ValueError:
|
43
|
+
If a message does not start with the expected 0x00 byte, indicating
|
44
|
+
an unexpected file format or possible corruption.
|
45
|
+
|
46
|
+
Message Format:
|
47
|
+
- Each message starts with a 0x00 byte.
|
48
|
+
- The following byte specifies the message length.
|
49
|
+
- The complete message consists of the first 2 bytes and 'message length' bytes of body.
|
50
|
+
- If a system message (message_type == b'S') with event_code == b'C' is encountered,
|
51
|
+
parsing stops immediately.
|
52
|
+
|
53
|
+
Example:
|
54
|
+
>>> with open('market_data.bin', 'rb') as binary_file:
|
55
|
+
>>> messages = reader.read_message_from_file(binary_file, cachesize=4096)
|
56
|
+
>>> for message in messages:
|
57
|
+
>>> print(message)
|
58
|
+
"""
|
59
|
+
max_message_size = 52
|
60
|
+
file_end_reached = False
|
61
|
+
|
62
|
+
data_buffer = file.read(cachesize)
|
63
|
+
buffer_len = len(data_buffer)
|
64
|
+
messages: List[MarketMessage] = []
|
65
|
+
|
66
|
+
while not file_end_reached:
|
67
|
+
if buffer_len < 2:
|
68
|
+
new_data = file.read(cachesize)
|
69
|
+
if not new_data:
|
70
|
+
break
|
71
|
+
data_buffer += new_data
|
72
|
+
buffer_len = len(data_buffer)
|
73
|
+
continue
|
74
|
+
|
75
|
+
if data_buffer[0:1] != b"\x00":
|
76
|
+
raise ValueError(
|
77
|
+
"Unexpected byte: " + str(data_buffer[0:1], encoding="ascii")
|
78
|
+
)
|
79
|
+
|
80
|
+
message_len = data_buffer[1]
|
81
|
+
total_len = 2 + message_len
|
82
|
+
|
83
|
+
if buffer_len < total_len:
|
84
|
+
# Wait for more data if message is incomplete
|
85
|
+
new_data = file.read(cachesize)
|
86
|
+
if not new_data:
|
87
|
+
break
|
88
|
+
data_buffer += new_data
|
89
|
+
buffer_len = len(data_buffer)
|
90
|
+
continue
|
91
|
+
message_data = data_buffer[2:total_len]
|
92
|
+
message = self.get_message_type(message_data)
|
93
|
+
|
94
|
+
if message.message_type in self.message_type:
|
95
|
+
messages.append(message)
|
96
|
+
|
97
|
+
if message.message_type == b"S": # System message
|
98
|
+
if message.event_code == b"C": # End of messages
|
99
|
+
break
|
100
|
+
|
101
|
+
# Update buffer
|
102
|
+
data_buffer = data_buffer[total_len:]
|
103
|
+
buffer_len = len(data_buffer)
|
104
|
+
|
105
|
+
if buffer_len < max_message_size and not file_end_reached:
|
106
|
+
new_data = file.read(cachesize)
|
107
|
+
if not new_data:
|
108
|
+
file_end_reached = True
|
109
|
+
else:
|
110
|
+
data_buffer += new_data
|
111
|
+
buffer_len = len(data_buffer)
|
112
|
+
|
113
|
+
return messages
|
114
|
+
|
115
|
+
def read_message_from_bytes(self, data: bytes):
|
116
|
+
"""
|
117
|
+
Process one or multiple ITCH binary messages from a raw bytes input.
|
118
|
+
|
119
|
+
Args:
|
120
|
+
data (bytes): Binary blob containing one or more ITCH messages.
|
121
|
+
|
122
|
+
Returns:
|
123
|
+
Queue: A queue containing parsed ITCH message objects.
|
124
|
+
|
125
|
+
Notes:
|
126
|
+
- Each message must be prefixed with a 0x00 header and a length byte.
|
127
|
+
- No buffering is done here — this is meant for real-time decoding.
|
128
|
+
"""
|
129
|
+
|
130
|
+
offset = 0
|
131
|
+
messages = Queue()
|
132
|
+
while offset + 2 <= len(data):
|
133
|
+
# Each message starts with: 1-byte header (0x00) 1-byte length
|
134
|
+
if data[offset : offset + 1] != b"\x00":
|
135
|
+
raise ValueError(
|
136
|
+
f"Unexpected start byte at offset {offset}: "
|
137
|
+
f"{str(data[offset : offset + 1], encoding='ascii')}"
|
138
|
+
)
|
139
|
+
|
140
|
+
msg_len = data[offset + 1]
|
141
|
+
total_len = 2 + msg_len
|
142
|
+
|
143
|
+
if offset + total_len > len(data):
|
144
|
+
break
|
145
|
+
|
146
|
+
raw_msg = data[offset + 2 : offset + total_len]
|
147
|
+
message = self.get_message_type(raw_msg)
|
148
|
+
|
149
|
+
if message.message_type in self.message_type:
|
150
|
+
messages.put(message)
|
151
|
+
|
152
|
+
if message.message_type == b"S": # System message
|
153
|
+
if message.event_code == b"C": # End of messages
|
154
|
+
break
|
155
|
+
|
156
|
+
offset += total_len
|
157
|
+
|
158
|
+
return messages
|
159
|
+
|
160
|
+
def get_message_type(self, message: bytes):
|
161
|
+
"""
|
162
|
+
Take an entire bytearray and return the appropriate ITCH message
|
163
|
+
instance based on the message type indicator (first byte of the message).
|
164
|
+
|
165
|
+
All message type indicators are single ASCII characters.
|
166
|
+
"""
|
167
|
+
message_type = message[0:1]
|
168
|
+
try:
|
169
|
+
return msgs[message_type](message)
|
170
|
+
except Exception:
|
171
|
+
raise ValueError(
|
172
|
+
f"Unknown message type: {message_type.decode(encoding='ascii')}"
|
173
|
+
)
|