asyncfix 1.0.1__py2.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.
- asyncfix/__init__.py +9 -0
- asyncfix/codec.py +303 -0
- asyncfix/connection.py +841 -0
- asyncfix/connection_client.py +70 -0
- asyncfix/connection_server.py +83 -0
- asyncfix/errors.py +37 -0
- asyncfix/fix_tester.py +514 -0
- asyncfix/fixtags.py +980 -0
- asyncfix/journaler.py +279 -0
- asyncfix/message.py +423 -0
- asyncfix/msgtype.py +120 -0
- asyncfix/protocol/__init__.py +6 -0
- asyncfix/protocol/common.py +98 -0
- asyncfix/protocol/order_single.py +498 -0
- asyncfix/protocol/protocol_base.py +12 -0
- asyncfix/protocol/protocol_fix44.py +243 -0
- asyncfix/protocol/schema.py +731 -0
- asyncfix/session.py +109 -0
- asyncfix-1.0.1.dist-info/LICENSE +21 -0
- asyncfix-1.0.1.dist-info/METADATA +64 -0
- asyncfix-1.0.1.dist-info/RECORD +23 -0
- asyncfix-1.0.1.dist-info/WHEEL +6 -0
- asyncfix-1.0.1.dist-info/top_level.txt +1 -0
asyncfix/__init__.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"""AsyncFIX core package."""
|
|
2
|
+
from .fixtags import FTag
|
|
3
|
+
from .msgtype import FMsg
|
|
4
|
+
from .message import FIXMessage
|
|
5
|
+
from .connection import AsyncFIXConnection, ConnectionRole, ConnectionState
|
|
6
|
+
from .connection_client import AsyncFIXClient
|
|
7
|
+
from .connection_server import AsyncFIXDummyServer
|
|
8
|
+
from .fix_tester import FIXTester
|
|
9
|
+
from .journaler import Journaler
|
asyncfix/codec.py
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
"""FIX Message encoding / decoding module."""
|
|
2
|
+
import logging
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
|
|
5
|
+
from asyncfix import FMsg, FTag
|
|
6
|
+
from asyncfix.errors import EncodingError
|
|
7
|
+
from asyncfix.message import FIXContainer, FIXMessage, RepeatingTagError
|
|
8
|
+
from asyncfix.protocol import FIXProtocolBase
|
|
9
|
+
from asyncfix.session import FIXSession
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class _RepeatingGroupContext(FIXContainer):
|
|
13
|
+
def __init__(self, tag, repeating_group_tags, parent):
|
|
14
|
+
self.tag = tag
|
|
15
|
+
self.repeating_group_tags = repeating_group_tags
|
|
16
|
+
self.parent = parent
|
|
17
|
+
FIXContainer.__init__(self)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class Codec(object):
|
|
21
|
+
"""Encoding / decoding engine.
|
|
22
|
+
|
|
23
|
+
Attributes:
|
|
24
|
+
protocol: FIX protocol
|
|
25
|
+
SOH: encoded message separator
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def __init__(self, protocol: FIXProtocolBase):
|
|
29
|
+
"""Codec init.
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
protocol: FIX protocol used in encoding/decoding
|
|
33
|
+
"""
|
|
34
|
+
self.protocol: FIXProtocolBase = protocol
|
|
35
|
+
self.SOH = "\x01"
|
|
36
|
+
|
|
37
|
+
@staticmethod
|
|
38
|
+
def current_datetime() -> str:
|
|
39
|
+
"""FIX complaint date-time string (UTC now)."""
|
|
40
|
+
return datetime.utcnow().strftime("%Y%m%d-%H:%M:%S.%f")[:-3]
|
|
41
|
+
|
|
42
|
+
def _addTag(self, body, t, msg: FIXMessage):
|
|
43
|
+
if msg.is_group(t):
|
|
44
|
+
groups = msg.get_group_list(t)
|
|
45
|
+
body.append("%s=%s" % (t, len(groups)))
|
|
46
|
+
for group in groups:
|
|
47
|
+
for tag in group.tags:
|
|
48
|
+
self._addTag(body, tag, group)
|
|
49
|
+
else:
|
|
50
|
+
body.append("%s=%s" % (t, msg[t]))
|
|
51
|
+
|
|
52
|
+
def encode(
|
|
53
|
+
self,
|
|
54
|
+
msg: FIXMessage,
|
|
55
|
+
session: FIXSession,
|
|
56
|
+
raw_seq_num: bool = False,
|
|
57
|
+
) -> str:
|
|
58
|
+
"""Encodes FIXMessage into serialized message.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
msg: generic FIXMessage
|
|
62
|
+
session: current session (for seq num)
|
|
63
|
+
raw_seq_num: if True - uses MsgSeqNum from `msg`
|
|
64
|
+
|
|
65
|
+
Returns:
|
|
66
|
+
encoded message (string)
|
|
67
|
+
|
|
68
|
+
Raises:
|
|
69
|
+
EncodingError: when failed MsgSeqNum conditions for some types of messages
|
|
70
|
+
"""
|
|
71
|
+
# Create body
|
|
72
|
+
body = []
|
|
73
|
+
|
|
74
|
+
msg_type = msg.msg_type
|
|
75
|
+
|
|
76
|
+
body.append("%s=%s" % (FTag.SenderCompID, session.sender_comp_id))
|
|
77
|
+
body.append("%s=%s" % (FTag.TargetCompID, session.target_comp_id))
|
|
78
|
+
|
|
79
|
+
seq_no = 0
|
|
80
|
+
if raw_seq_num:
|
|
81
|
+
seq_no = msg[FTag.MsgSeqNum]
|
|
82
|
+
else:
|
|
83
|
+
if msg_type == FMsg.SEQUENCERESET:
|
|
84
|
+
if FTag.MsgSeqNum not in msg:
|
|
85
|
+
raise EncodingError(
|
|
86
|
+
"SequenceReset must have the MsgSeqNum already populated"
|
|
87
|
+
)
|
|
88
|
+
seq_no = msg[FTag.MsgSeqNum]
|
|
89
|
+
else:
|
|
90
|
+
# if we have the PossDupFlag set, we need to send the message
|
|
91
|
+
# with the same seqNo
|
|
92
|
+
if msg.get(FTag.PossDupFlag, "N") == "Y":
|
|
93
|
+
if FTag.MsgSeqNum not in msg:
|
|
94
|
+
raise EncodingError(
|
|
95
|
+
"Failed to encode message with PossDupFlag=Y but no"
|
|
96
|
+
" previous MsgSeqNum"
|
|
97
|
+
)
|
|
98
|
+
seq_no = msg[FTag.MsgSeqNum]
|
|
99
|
+
else:
|
|
100
|
+
seq_no = session.allocate_next_num_out()
|
|
101
|
+
|
|
102
|
+
body.append("%s=%s" % (FTag.MsgSeqNum, seq_no))
|
|
103
|
+
body.append("%s=%s" % (FTag.SendingTime, self.current_datetime()))
|
|
104
|
+
|
|
105
|
+
for t in msg.tags:
|
|
106
|
+
if t in {
|
|
107
|
+
FTag.MsgSeqNum,
|
|
108
|
+
FTag.SendingTime,
|
|
109
|
+
FTag.SenderCompID,
|
|
110
|
+
FTag.TargetCompID,
|
|
111
|
+
}:
|
|
112
|
+
continue
|
|
113
|
+
self._addTag(body, t, msg)
|
|
114
|
+
|
|
115
|
+
# Enable easy change when debugging
|
|
116
|
+
SEP = self.SOH
|
|
117
|
+
|
|
118
|
+
body = self.SOH.join(body) + self.SOH
|
|
119
|
+
|
|
120
|
+
# Create header
|
|
121
|
+
header = []
|
|
122
|
+
msg_type = "%s=%s" % (FTag.MsgType, msg_type)
|
|
123
|
+
header.append("%s=%s" % (FTag.BeginString, self.protocol.beginstring))
|
|
124
|
+
header.append("%s=%i" % (FTag.BodyLength, len(body) + len(msg_type) + 1))
|
|
125
|
+
header.append(msg_type)
|
|
126
|
+
|
|
127
|
+
fixmsg = self.SOH.join(header) + self.SOH + body
|
|
128
|
+
|
|
129
|
+
cksum = sum([ord(i) for i in fixmsg]) % 256
|
|
130
|
+
fixmsg = fixmsg + "%s=%0.3i" % (FTag.CheckSum, cksum)
|
|
131
|
+
|
|
132
|
+
# print len(fixmsg)
|
|
133
|
+
|
|
134
|
+
return fixmsg + SEP
|
|
135
|
+
|
|
136
|
+
def decode(
|
|
137
|
+
self,
|
|
138
|
+
rawmsg: bytes,
|
|
139
|
+
silent: bool = True,
|
|
140
|
+
) -> tuple[FIXMessage | None, int, bytes | None]:
|
|
141
|
+
"""Decodes message from socket.
|
|
142
|
+
|
|
143
|
+
Args:
|
|
144
|
+
rawmsg: message bytes
|
|
145
|
+
silent: no errors raised, returns non
|
|
146
|
+
|
|
147
|
+
Returns:
|
|
148
|
+
if OK - (FIXMessage, bytes_processed, valid_raw_msg_bytes)
|
|
149
|
+
if ERR - (None, n_bytes_skip, None)
|
|
150
|
+
"""
|
|
151
|
+
valid_idx = rawmsg.find(b"8=FIX.")
|
|
152
|
+
if valid_idx == -1:
|
|
153
|
+
assert silent, "no fix header"
|
|
154
|
+
return None, len(rawmsg), None
|
|
155
|
+
|
|
156
|
+
parsed_length = valid_idx
|
|
157
|
+
|
|
158
|
+
msg = rawmsg[valid_idx:].decode("latin-1")
|
|
159
|
+
|
|
160
|
+
next_msg = msg[5:].find("8=FIX.")
|
|
161
|
+
if next_msg != -1:
|
|
162
|
+
# Next fix message added, but incomplete
|
|
163
|
+
next_msg += 5
|
|
164
|
+
else:
|
|
165
|
+
next_msg = len(msg)
|
|
166
|
+
|
|
167
|
+
encoded_msg = rawmsg[valid_idx : next_msg + valid_idx]
|
|
168
|
+
|
|
169
|
+
msg = msg[:next_msg].split(self.SOH)
|
|
170
|
+
if not msg[-1]:
|
|
171
|
+
msg = msg[:-1]
|
|
172
|
+
|
|
173
|
+
# at a minimum we require BeginString, BodyLength & Checksum
|
|
174
|
+
if len(msg) < 3:
|
|
175
|
+
assert silent, "Minimum message"
|
|
176
|
+
return (None, parsed_length, None)
|
|
177
|
+
|
|
178
|
+
tag, value = msg[0].split("=", 1)
|
|
179
|
+
if value != self.protocol.beginstring:
|
|
180
|
+
logging.error(
|
|
181
|
+
"FIX Version unexpected (Recv: %s Expected: %s)"
|
|
182
|
+
% (value, self.protocol.beginstring)
|
|
183
|
+
)
|
|
184
|
+
assert silent, "protocol beginstring mismatch"
|
|
185
|
+
return (None, len(rawmsg), None)
|
|
186
|
+
|
|
187
|
+
toks = msg[1].split("=", 1)
|
|
188
|
+
if len(toks) != 2:
|
|
189
|
+
assert silent, f"BodyLength split error {msg}"
|
|
190
|
+
return (None, len(rawmsg), None)
|
|
191
|
+
tag, value = toks
|
|
192
|
+
|
|
193
|
+
msg_length = len(msg[0]) + len(msg[1]) + len("10=000") + 3
|
|
194
|
+
if tag != FTag.BodyLength:
|
|
195
|
+
logging.error(f"*** BodyLength missing or not 2nd field *** [{tag}]: {msg}")
|
|
196
|
+
assert silent, "2nd tag must be BodyLength"
|
|
197
|
+
return (None, len(rawmsg), None)
|
|
198
|
+
else:
|
|
199
|
+
msg_length += int(value)
|
|
200
|
+
|
|
201
|
+
# message looks incomplete
|
|
202
|
+
if msg_length > len(rawmsg):
|
|
203
|
+
assert silent, "incomplete message"
|
|
204
|
+
return (None, parsed_length, None)
|
|
205
|
+
|
|
206
|
+
checksum_passed = False
|
|
207
|
+
parsed_length += msg_length
|
|
208
|
+
|
|
209
|
+
decoded_msg = FIXMessage("UNKNOWN")
|
|
210
|
+
repeating_groups = []
|
|
211
|
+
repeating_group_tags = self.protocol.repeating_groups
|
|
212
|
+
current_context = decoded_msg
|
|
213
|
+
|
|
214
|
+
for m in msg:
|
|
215
|
+
toks = m.split("=", 1)
|
|
216
|
+
if len(toks) != 2:
|
|
217
|
+
assert silent, f"incomplete tag {m}"
|
|
218
|
+
return (None, len(rawmsg), None)
|
|
219
|
+
tag, value = toks
|
|
220
|
+
|
|
221
|
+
if tag == FTag.CheckSum:
|
|
222
|
+
cheksum_base = self.SOH.join(msg[:-1])
|
|
223
|
+
checksum = (sum([ord(i) for i in cheksum_base]) + 1) % 256
|
|
224
|
+
|
|
225
|
+
if checksum != int(value):
|
|
226
|
+
logging.warning(
|
|
227
|
+
"\tCheckSum: %s (INVALID) expecting %s" % (int(value), checksum)
|
|
228
|
+
)
|
|
229
|
+
assert (
|
|
230
|
+
silent
|
|
231
|
+
), f"invalid checksum tag[10]={value} expected: {checksum} {msg=}"
|
|
232
|
+
checksum_passed = False
|
|
233
|
+
else:
|
|
234
|
+
checksum_passed = True
|
|
235
|
+
elif tag == FTag.MsgType:
|
|
236
|
+
try:
|
|
237
|
+
decoded_msg.msg_type = FMsg(value)
|
|
238
|
+
except ValueError:
|
|
239
|
+
decoded_msg.msg_type = value
|
|
240
|
+
|
|
241
|
+
# found the start of a repeating group
|
|
242
|
+
if tag in repeating_group_tags:
|
|
243
|
+
# i.e. we are already in a repeating group
|
|
244
|
+
if type(current_context) is _RepeatingGroupContext:
|
|
245
|
+
while (
|
|
246
|
+
repeating_groups
|
|
247
|
+
and tag not in current_context.repeating_group_tags
|
|
248
|
+
):
|
|
249
|
+
current_context.parent.add_group(
|
|
250
|
+
current_context.tag, current_context
|
|
251
|
+
)
|
|
252
|
+
current_context = current_context.parent
|
|
253
|
+
# pop the completed group off the stack
|
|
254
|
+
del repeating_groups[-1]
|
|
255
|
+
|
|
256
|
+
ctx = _RepeatingGroupContext(
|
|
257
|
+
tag, repeating_group_tags[tag], current_context
|
|
258
|
+
)
|
|
259
|
+
repeating_groups.append(ctx)
|
|
260
|
+
current_context = ctx
|
|
261
|
+
elif repeating_groups:
|
|
262
|
+
# we have 1 or more repeating groups in progress
|
|
263
|
+
# & our tag isn't the start of a group
|
|
264
|
+
while (
|
|
265
|
+
repeating_groups and tag not in current_context.repeating_group_tags
|
|
266
|
+
):
|
|
267
|
+
current_context.parent.add_group(
|
|
268
|
+
current_context.tag, current_context
|
|
269
|
+
)
|
|
270
|
+
current_context = current_context.parent
|
|
271
|
+
# pop the completed group off the stack
|
|
272
|
+
del repeating_groups[-1]
|
|
273
|
+
|
|
274
|
+
if tag in current_context.tags:
|
|
275
|
+
# if the repeating group already contains this field,
|
|
276
|
+
# start the next
|
|
277
|
+
current_context.parent.add_group(
|
|
278
|
+
current_context.tag, current_context
|
|
279
|
+
)
|
|
280
|
+
ctx = _RepeatingGroupContext(
|
|
281
|
+
current_context.tag,
|
|
282
|
+
current_context.repeating_group_tags,
|
|
283
|
+
current_context.parent,
|
|
284
|
+
)
|
|
285
|
+
del repeating_groups[-1]
|
|
286
|
+
repeating_groups.append(ctx)
|
|
287
|
+
current_context = ctx
|
|
288
|
+
|
|
289
|
+
# else add it to the current one
|
|
290
|
+
current_context.set(tag, value)
|
|
291
|
+
else:
|
|
292
|
+
if tag in decoded_msg:
|
|
293
|
+
# Repeating tag found, possibly RepGrp not in protocol schema
|
|
294
|
+
decoded_msg.set(tag, RepeatingTagError)
|
|
295
|
+
else:
|
|
296
|
+
# this isn't a repeating group field, so just add it normally
|
|
297
|
+
decoded_msg.set(tag, value)
|
|
298
|
+
|
|
299
|
+
if checksum_passed:
|
|
300
|
+
return (decoded_msg, parsed_length, encoded_msg)
|
|
301
|
+
else:
|
|
302
|
+
assert silent, f"Checksum probably missing: {msg}"
|
|
303
|
+
return (None, parsed_length, None)
|