simplefix

repository·master·Indexed 42 days ago

https://github.com/da4089/simplefix

A lightweight Python implementation of the FIX (Financial Information eXchange) application-layer protocol. It provides tools for encoding and decoding FIX messages via the FixMessage and FixParser classes without the complexity of a full FIX engine, as it does not handle socket communication, network transport, or session-layer recovery.

Tokens
4.5K
Snippets
21
Records
24
Agent score
54%

What's inside simplefix

  1. What is simplefix?

    master

    simplefix is a lightweight implementation of the FIX (Financial Information eXchange) application-layer protocol. It is designed for the creation, encoding, and decoding of FIX messages.

    Key Characteristics:

    • Application-layer only: It does not handle socket communication, network transport, or connection management.
    • No persistence/recovery: It does not implement FIX session-layer features like message persistence or sequence number recovery.
    • Use case: Use simplefix when you need to manipulate FIX message structures (encoding/decoding) within your own application logic or custom transport layer, rather than needing a full-blown FIX Engine (like QuickFIX) that manages connections and sessions.
  2. Insert fields into the FIX header using the header parameter

    master

    While FixMessage does not strictly distinguish between header and body fields, you can force a field into the header section by setting the header=True keyword argument in append_pair(), append_string(), or append_strings(). This ensures the field is inserted after any previously added header fields at the beginning of the message. This is useful for setting MsgSeqNum (34) or SendingTime (52) just before encoding.

    Note that BeginString (8), BodyLength (9), MsgType (35), and Checksum (10) are always encoded in their required locations regardless of addition order.

    message.append_pair(8, "FIX.4.4")
    message.append_pair(35, 0)
    message.append_pair(49, "SENDER")
    message.append_pair(56, "TARGET")
    message.append_pair(112, "TR0003692")
    # These will be inserted at the beginning of the message
    message.append_pair(34, 4684, header=True)
    message.append_time(52, header=True)
  3. Encode a message to a FIX byte buffer

    master

    To generate a correctly formatted FIX message as a byte buffer, call the encode() method on a message object. This process automatically orders the fields correctly and calculates/sets the required BodyLength (9) and Checksum (10) fields.

    byte_buffer = message.encode()
  4. Parse FIX messages using FixParser

    master

    To parse FIX messages from a byte stream (e.g., from a socket), instantiate a simplefix.FixParser. Use append_buffer() to add incoming bytes to the parser's internal reassembly buffer. Call get_message() to attempt to extract a complete message.

    If no complete message is available in the buffer, get_message() returns None. If a complete message is found, it returns a FixMessage instance.

    parser = simplefix.FixParser()
    
    # Append bytes received from a socket
    parser.append_buffer(response_from_socket)
    
    # Retrieve the next complete message
    message = parser.get_message()
    if message:
        # Process message
        pass
  5. Run all unit tests locally

    master

    To run the full suite of unit tests from a local shell, set the PYTHONPATH to the current directory and execute the test/all.py script. Note that you may see a DeprecationWarning regarding append_time(), but you should expect approximately 90 tests to pass.

    env PYTHONPATH=. python test/all.py
  6. Get the simplefix source code

    master

    If you prefer to work with the source code directly, you can clone the repository, download a tarball, or download a zipball from GitHub.

    Once you have the source code, you can install it into your site-packages using setup.py.

    # Clone the repository
    $ git clone git://github.com/da4089/simplefix.git
    
    # Download the tarball
    $ curl -OL https://github.com/da4089/simplefix/tarball/master
    
    # Download the zipball
    $ curl -OL https://github.com/da4089/simplefix/zipball/master
    
    # Install from source
    $ python setup.py install
  7. Use Raw Mode to manually control encoding

    master

    If you need to manually control the ordering of fields or override the automatic calculation of the BodyLength (9) and Checksum (10) fields, call encode(True). This 'raw' mode disables the default automatic formatting and is primarily intended for creating known-bad messages for testing purposes.

    byte_buffer = message.encode(True)
  8. Import the simplefix module

    master

    You can use simplefix in two ways. The recommended approach is to import the module itself to maintain its internal structure and avoid namespace pollution. Alternatively, you can use from simplefix import * to import all public bindings directly into your namespace; the package explicitly manages its namespace to ensure only public features are exposed via this method.

    import simplefix
    
    message = simplefix.FixMessage()
  9. Run specific test cases or methods

    master

    You can filter the test execution by passing the class name or a specific method name as arguments to test/all.py.

    To run all tests within a specific class, pass the class name. To run a single test method, pass the class name followed by the method name.

    # Run a specific test class
    env PYTHONPATH=. python test/all.py ParserTest
    
    # Run a single test method
    env PYTHONPATH=. python test/all.py ParserTest.test_raw_data