Quick start

This guide aims to show the procedure of creating a minimal working code of an application that uses FIXAntenna Python to connect to some counterparty as an FIX Session Initiator

Note

The sample expected to be working using the default package’s properties file.

1. Preparation

Extract the FIXAntenna Python package somewhere on your hard drive and create a file named quick-start.py in the extracted directory.

2. Import required libraries and make the initial setup

import sys
import time

# put V12 .so/.pyd and dependencies near
# or specify V12 location:
# import sys
# sys.path.append('./V12')

# load library by name
from V12 import FIXFields, FIXAntenna as v12

3. Declare an V12.FIXAntenna.Application class implementation to handle session’s events.

The methods declared in this class will be called by FIXAntenna when session related events occur.

# define session listener
class Application(v12.Application):
    def __init__(self, ssn: v12.Session, host, port):
        # If the __init__ method is overloaded, calling the parent's version is a must.
        super().__init__()
        self.ssn = ssn
        # An application implementation should be passed to the register_application method
        # to start receiving any callbacks
        self.ssn.register_application(self)
        self.ssn.connect(30, host, port)

    def __del__(self):
        self.shutdown()

    def shutdown(self):
        if self.ssn is not None:
            ssn = self.ssn
            self.ssn = None
            ssn.disconnect()
            ssn.finalize()

    def process(self, message: v12.FixMessage, sid: v12.SessionId):
        try:
            if self.ssn is None:
                return False

            print(f'IN - {message}')
        except (Exception) as e:
            print(e)
            raise
        return True

    def on_logon_event(self, event: v12.LogonEvent, sid: v12.SessionId):
        event.logon_action = v12.LogonAction.AcceptWithConfirmLogon

    def on_logout_event(self, event: v12.LogoutEvent, sid: v12.SessionId):
        event.reconnect_flag = True if self.ssn is not None else False

    def on_session_level_reject_event(self, event: v12.SessionLevelRejectEvent, sid: v12.SessionId):
        return

    def on_msg_reject_event(self, event: v12.MsgRejectEvent, sid: v12.SessionId):
        return

    def on_new_state_event(self, event: v12.NewStateEvent, sid: v12.SessionId):
        return

    def on_resend(self, message: v12.FixMessage, sid: v12.SessionId):
        return True

4. Declare the main function.

In this function, configure and start the V12.FIXAntenna.FixEngine instance and create a session.

def main():
    try:
        propfile = sys.argv[1]
        host = sys.argv[2]
        port = int(sys.argv[3])
        # an instance of the FixEngine class should be created before any other calls to the library.
        engine = v12.FixEngine( propfile )

        app = Application(engine.create_session(v12.SessionId("TEST1", "TEST2"), "FIX44"), host, port)

        # the application should hold the execution thread.
        while input("enter 'q' to exit\n") != "q":
            time.sleep(1)

        app.shutdown()

    except (RuntimeError) as e:
        print(e)


if __name__ == '__main__':
    main()

5. The complete sample.

import sys
import time

# put V12 .so/.pyd and dependencies near
# or specify V12 location:
# import sys
# sys.path.append('./V12')

# load library by name
from V12 import FIXFields, FIXAntenna as v12

# define session listener
class Application(v12.Application):
    def __init__(self, ssn: v12.Session, host, port):
        # If the __init__ method is overloaded, calling the parent's version is a must.
        super().__init__()
        self.ssn = ssn
        # An application implementation should be passed to the register_application method
        # to start receiving any callbacks
        self.ssn.register_application(self)
        self.ssn.connect(30, host, port)

    def __del__(self):
        self.shutdown()

    def shutdown(self):
        if self.ssn is not None:
            ssn = self.ssn
            self.ssn = None
            ssn.disconnect()
            ssn.finalize()

    def process(self, message: v12.FixMessage, sid: v12.SessionId):
        try:
            if self.ssn is None:
                return False

            print(f'IN - {message}')
        except (Exception) as e:
            print(e)
            raise
        return True

    def on_logon_event(self, event: v12.LogonEvent, sid: v12.SessionId):
        event.logon_action = v12.LogonAction.AcceptWithConfirmLogon

    def on_logout_event(self, event: v12.LogoutEvent, sid: v12.SessionId):
        event.reconnect_flag = True if self.ssn is not None else False

    def on_session_level_reject_event(self, event: v12.SessionLevelRejectEvent, sid: v12.SessionId):
        return

    def on_msg_reject_event(self, event: v12.MsgRejectEvent, sid: v12.SessionId):
        return

    def on_new_state_event(self, event: v12.NewStateEvent, sid: v12.SessionId):
        return

    def on_resend(self, message: v12.FixMessage, sid: v12.SessionId):
        return True


def main():
    try:
        propfile = sys.argv[1]
        host = sys.argv[2]
        port = int(sys.argv[3])
        # an instance of the FixEngine class should be created before any other calls to the library.
        engine = v12.FixEngine( propfile )

        app = Application(engine.create_session(v12.SessionId("TEST1", "TEST2"), "FIX44"), host, port)

        # the application should hold the execution thread.
        while input("enter 'q' to exit\n") != "q":
            time.sleep(1)

        app.shutdown()

    except (RuntimeError) as e:
        print(e)


if __name__ == '__main__':
    main()

6. Run the sample.

python quick-start.py engine.properties 127.0.0.1 9011