Simple FIX Protocol Implementation
SimpleFIX is a Python library that provides a straightforward implementation of the FIX (Financial Information eXchange) application-layer protocol. It enables the creation, encoding, and decoding of FIX messages. Unlike full-fledged FIX engines, SimpleFIX focuses solely on message handling and does not include functionality for socket communication, session management, recovery, or message persistence. The library is actively maintained, with its latest version being 1.0.17.
Warnings
- breaking A checksum calculation bug was fixed in v1.0.17. If your application relies on SimpleFIX to calculate and set the checksum (tag 10), upgrading to v1.0.17 will result in different, correct checksums. Any systems validating checksums against messages produced by older SimpleFIX versions will break.
- breaking Version 1.0.7 introduced major changes to string/bytes handling for Python 3.x. All received FIX values are now exported as bytes. Input string values are transformed to bytes using UTF-8 encoding (from strings) and ASCII for everything else. If you require a different encoding for input, you must convert your values to bytes manually before appending them.
- gotcha While `FixMessage` generally retains the order in which fields are added, crucial FIX header and trailer fields (BeginString (8), BodyLength (9), MsgType (35), Checksum (10)) are always encoded in their mandated positions regardless of the order they were appended.
- gotcha The FIX standard prohibits empty (zero-length) values. By default, `FixParser` will raise an `EmptyValueError` if it encounters such a field during parsing.
- gotcha Prior to v1.0.8, adding a field with a value of `None` could lead to unexpected behavior. Since v1.0.8, attempting to add a field with a `None` value will silently fail (the field will not be added to the message).
Install
-
pip install simplefix
Imports
- FixMessage
import simplefix message = simplefix.FixMessage()
- FixParser
import simplefix parser = simplefix.FixParser()
Quickstart
import simplefix
import datetime
# Create a FIX message
message = simplefix.FixMessage()
message.append_pair(8, "FIX.4.2") # BeginString
message.append_pair(35, "D") # MsgType - New Order Single
message.append_pair(49, "SENDER") # SenderCompID
message.append_pair(56, "TARGET") # TargetCompID
message.append_pair(11, "ORDER123") # ClOrdID
message.append_pair(21, 1) # HandlInst
message.append_pair(55, "IBM") # Symbol
message.append_pair(54, 1) # Side - Buy
message.append_pair(60, datetime.datetime.utcnow(), fix_type=simplefix.FIX_UTC_TIMESTAMP) # TransactTime
message.append_pair(38, 100) # OrderQty
message.append_pair(40, 1) # OrdType - Market
# Encode the message
encoded_message = message.encode()
print(f"Encoded Message: {encoded_message.decode('ascii').replace(chr(1), '|')}")
# Parse a FIX message
parser = simplefix.FixParser()
parser.append_buffer(encoded_message)
parsed_message = parser.get_message()
if parsed_message:
print(f"Parsed MsgType: {parsed_message.get(35).decode('ascii')}")
print(f"Parsed Symbol: {parsed_message.get(55).decode('ascii')}")
else:
print("No complete message found in buffer.")