Search code examples
python-3.xquickfixfix-protocol

How to send response back to initiator


so i am trying to set up a FIX server that will serve me to accept FIX messages. i managed to receive messages from a demo client that i made. i want to perform an action according to the received message and then return a response to the initiator.

i have added the code that i use, it somehow sends it through the acceptor back to the initiator.

import quickfix as fix


def create_fix_response_for_wm(self, session_id, message, api_response):
        try:
            report = quickfix50sp2.ExecutionReport()
            report.setField(fix.Text(api_response.text))
            report.setField(fix.ListID(message.getField(66)))
            if message.getFieldIfSet(fix.OrderID()):
                report.setField(fix.OrderID(message.getField(14)))
            elif message.getFieldIfSet(fix.ListID()):
                report.setField(fix.OrderID(message.getField(66)))


            fix.Session.sendToTarget(report, session_id)
        except Exception as e:
            logger.exception(f'could not create response')


Solution

  • so after looking and testing a couple of times this is what i found.

    import quickfix as fix
    
    def create_fix_response_for_wm(self, session_id, message, api_response):
            try:
                report = quickfix50sp2.ExecutionReport()
                report.setField(fix.Text(api_response.text))
                report.setField(fix.ListID(message.getField(66)))
                if message.getFieldIfSet(fix.OrderID()):
                    report.setField(fix.OrderID(message.getField(14)))
                elif message.getFieldIfSet(fix.ListID()):
                    report.setField(fix.OrderID(message.getField(66)))
    
    
                fix.Session.sendToTarget(report, session_id)
            except Exception as e:
                logger.exception(f'could not create response')
    
    

    session_id = should be a SessionID object which can be built like this:

    session_id = fix.SessionID("FIXT.1.1", <ACCEPTOR - SenderCompID of the acceptor configuration>, <INITIATOR - TargetCompID in the acceptor configuration >)

    configuration example:

    [SESSION]
    SocketAcceptPort=12000
    StartDay=sunday
    EndDay=friday
    AppDataDictionary=./glossary/FIX50SP2.xml
    TransportDataDictionary=./glossary/FIXT11.xml
    DefaultApplVerID=FIX.5.0SP2
    BeginString=FIXT.1.1
    SenderCompID=MYACCEPTOR
    TargetCompID=CLIENT
    
    

    session_id = fix.SessionID("FIXT.1.1","MYACCEPTOR" , "CLIENT") - in our case

    you you this sessionID object when sending.

    fix.Session.sendToTarget(<YOUR MESSAGE>, session_id)