1

I have been experimenting with QuickFix but I am getting a "AttributeError:SessionID". I honestly have no idea why it's happening, I tinkered with the code a bit but problem remains. Also , google has failed me big on this one so I hope you guys can help me.

Code :

import sys
import time
import thread
import argparse
from datetime import datetime
import quickfix as fix

class Application(fix.Application):
    orderID = 0
    execID = 0
    def gen_ord_id(self):
        global orderID
        orderID+=1
        return orderID


    def onCreate(self, sessionID):
            return
    def onLogon(self, sessionID):
            self.sessionID = sessionID
            print ("Successful Logon to session '%s'." % sessionID.toString())
            return

    def onLogout(self, sessionID): return

    def toAdmin(self, sessionID, message):
        return

    def fromAdmin(self, sessionID, message):
        return


    def toApp(self, sessionID, message):
        print "Sent the following message: %s" % message.toString()
        return

    def fromApp(self, message, sessionID):
        print "Received the following message: %s" % message.toString()
        return

    def genOrderID(self):
        self.orderID = self.orderID+1
        return `self.orderID`

    def genExecID(self):
        self.execID = self.execID+1
        return `self.execID`
    def put_order(self):
        print("Creating the following order: ")
        trade = fix.Message()
        trade.getHeader().setField(fix.BeginString(fix.BeginString_FIX50)) #
        trade.getHeader().setField(fix.MsgType(fix.MsgType_NewOrderSingle)) #39=D
        trade.setField(fix.ClOrdID(self.genExecID())) #11=Unique order

        trade.setField(fix.HandlInst(fix.HandlInst_MANUAL_ORDER_BEST_EXECUTION)) #21=3 (Manual order, best executiona)
        trade.setField(fix.Symbol('SMBL')) #55=SMBL ?
        trade.setField(fix.Side(fix.Side_BUY)) #43=1 Buy
        trade.setField(fix.OrdType(fix.OrdType_LIMIT)) #40=2 Limit order
        trade.setField(fix.OrderQty(100)) #38=100
        trade.setField(fix.Price(10))
        print trade.toString()
        fix.Session.sendToTarget(trade, self.sessionID)


def main(config_file):
    try:
        settings = fix.SessionSettings("initiatorsettings.cfg")
        application = Application()
        storeFactory = fix.FileStoreFactory(settings)
        logFactory = fix.FileLogFactory(settings)
        initiator = fix.SocketInitiator(application, storeFactory, settings, logFactory)
        initiator.start()

        while 1:
                input = raw_input()
                if input == '1':
                    print "Putin Order"
                    application.put_order()
                if input == '2':
                    sys.exit(0)
                if input == 'd':
                    import pdb
                    pdb.set_trace()
                else:
                    print "Valid input is 1 for order, 2 for exit"
                    continue
    except (fix.ConfigError, fix.RuntimeError), e:
        print e

if __name__=='__main__':
    parser = argparse.ArgumentParser(description='FIX Client')
    parser.add_argument('file_name', type=str, help='Name of configuration file')
    args = parser.parse_args()
    main(args.file_name)

I get the following error message :

Traceback (most recent call last): 
File "initiator.py", line 97, in main application.put_order()   
File "initiator.py", line 80, in put_order fix.Session.sendToTarget(trade, self.sessionID)   
File "C:\Users\gribeiro\Anaconda\lib\site-packages\quickfix.py", line 30939, in <lambda>__getattr__ = lambda self, name: _swig_getattr(self, Application, name)   
File "C:\Users\gribeiro\Anaconda\lib\site-packages\quickfix.py", line 57, in _swig_getattr raise AttributeError(name) 
AttributeError: sessionID

------------------------------------------------------------


Update :

Code producing the error:

import sys
import time
import thread
import argparse
from datetime import datetime
import quickfix as fix

class Application(fix.Application):
    orderID = 0
    execID = 0
    def gen_ord_id(self):
        global orderID
        orderID+=1
        return orderID


def onCreate(self, sessionID):
        return
def onLogon(self, sessionID):
        # self.sessionID = sessionID
        # print ("Successful Logon to session '%s'." % sessionID.toString())
        return

def onLogout(self, sessionID): return

def toAdmin(self, sessionID, message):
    return

def fromAdmin(self, sessionID, message):
    return


def toApp(self, sessionID, message):
    print "Sent the following message: %s" % message.toString()
    return

def fromApp(self, message, sessionID):
    print "Received the following message: %s" % message.toString()
    return

def genOrderID(self):
    self.orderID = self.orderID+1
    return `self.orderID`

def genExecID(self):
    self.execID = self.execID+1
    return `self.execID`
def put_order(self):
    print("Creating the following order: ")
    trade = fix.Message()
    trade.getHeader().setField(fix.BeginString(fix.BeginString_FIX50)) #
    trade.getHeader().setField(fix.MsgType(fix.MsgType_NewOrderSingle)) #39=D
    trade.setField(fix.ClOrdID(self.genExecID())) #11=Unique order

    trade.setField(fix.HandlInst(fix.HandlInst_MANUAL_ORDER_BEST_EXECUTION)) #21=3 (Manual order, best executiona)
    trade.setField(fix.Symbol('SMBL')) #55=SMBL ?
    trade.setField(fix.Side(fix.Side_BUY)) #43=1 Buy
    trade.setField(fix.OrdType(fix.OrdType_LIMIT)) #40=2 Limit order
    trade.setField(fix.OrderQty(100)) #38=100
    trade.setField(fix.Price(10))
    print trade.toString()
    fix.Session_sendToTarget(trade)


def main(config_file):
    try:
        settings = fix.SessionSettings(config_file)
        application = Application()
        storeFactory = fix.FileStoreFactory(settings)
        logFactory = fix.FileLogFactory(settings)
        initiator = fix.SocketInitiator(application, storeFactory, settings, logFactory)
        initiator.start()

    while 1:
            input = raw_input()
            if input == '1':
                print "Putin Order"
                application.put_order()
            if input == '2':
                sys.exit(0)
            if input == 'd':
                import pdb
                pdb.set_trace()
            else:
                print "Valid input is 1 for order, 2 for exit"
                continue
except (fix.ConfigError, fix.RuntimeError), e:
    print e

if __name__=='__main__':
    parser = argparse.ArgumentParser(description='FIX Client')
    parser.add_argument('file_name', type=str, help='Name of configuration file')
    args = parser.parse_args()
    main(args.file_name)

Config file :

[DEFAULT]
ConnectionType=initiator
ReconnectInterval=60
FileStorePath=store
FileLogPath=log
StartTime=00:00:00
EndTime=00:00:00
UseDataDictionary=Y
DataDictionary=spec/FIX42.xml
HttpAcceptPort=9911
ValidateUserDefinedFields=N
ResetOnLogout=Y
ResetOnLogon=Y
ValidateFieldsOutOfOrder=N
DefaultApplVerID=FIX.5.0SP2

# standard config elements

[SESSION]
# inherit ConnectionType, ReconnectInterval and SenderCompID from default
BeginString=FIX.4.2
SenderCompID=BRANZOL
TargetCompID=FIXSIM
SocketConnectHost=host
SocketConnectPort=port
HeartBtInt=30 

Output from ScreenLogFactory >

<20151216-17:09:43.426, FIX.4.2:BRANZOL->FIXSIM, event>
  (Created session)
<20151216-17:09:43.434, FIX.4.2:BRANZOL->FIXSIM, event>
  (Connecting to hostX on port Y)
<20151216-17:09:44.159, FIX.4.2:BRANZOL->FIXSIM, outgoing>
  (8=FIX.4.29=7435=A34=149=BRANZOL52=20151216-17:09:43.88256=FIXSIM98=0108=30141=Y10=032)
<20151216-17:09:44.159, FIX.4.2:BRANZOL->FIXSIM, event>
  (Initiated logon request)
<20151216-17:09:44.264, FIX.4.2:BRANZOL->FIXSIM, event>
  (Socket Error: Connection reset by peer.)
<20151216-17:09:44.264, FIX.4.2:BRANZOL->FIXSIM, event>
  (Disconnecting)
Valid input is 1 for order, 2 for exit
Putin Order
Creating the following order: 
8=FIX.5.09=4635=D11=121=338=10040=244=1054=155=SMBL10=081
ap14
  • 4,393
  • 1
  • 15
  • 30
  • Does `initiatorsettings.cfg` exist in the same folder you launched your script from? You setup `argparse` to pass in the location of a config file to `main()`, but it seems you don't actually use `config_file` parameter anywhere in your code. Instead you hardcoded in `initiatorsettings.cfg` as your config file. – Joe Young Nov 24 '15 at 15:47
  • @JoeYoung True, my bad but initiatorsettings.cfg is in the same location so the issue is not there. I am now using the configfile variable but the problem remains. – worldexplorer95 Nov 24 '15 at 15:55

1 Answers1

1

Try cutting this line entirely self.sessionID = sessionID

EDIT -- OKay, thanks for including the traceback. You should replace the line fix.Session.sendToTarget(trade, self.sessionID) with the line fix.Session_sendToTarget(trade) and let me know how that goes.

Wapiti
  • 1,851
  • 2
  • 20
  • 40
  • I tried but the error is still there. The traceback is as follow : Traceback (most recent call last): File "initiator.py", line 97, in main application.put_order() File "initiator.py", line 80, in put_order fix.Session.sendToTarget(trade, self.sessionID) File "C:\Users\gribeiro\Anaconda\lib\site-packages\quickfix.py", line 30939, in __getattr__ = lambda self, name: _swig_getattr(self, Application, name) File "C:\Users\gribeiro\Anaconda\lib\site-packages\quickfix.py", line 57, in _swig_getattr raise AttributeError(name) AttributeError: sessionID – worldexplorer95 Nov 30 '15 at 09:12
  • Sorry for the late response . No , it didnt. I got a quickfix.SessionNotFound : Session Not Found error . – worldexplorer95 Dec 14 '15 at 13:15
  • include the traceback please -- this is progress – Wapiti Dec 14 '15 at 22:16
  • thank you for the help , here follows the traceback : Traceback (most recent call last): File "initiator.py", line 88, in main application.put_order() File "initiator.py", line 71, in put_order fix.Session.sendToTarget(trade) SessionNotFound: Session Not Found DEBUG:root:This message should go to the log file ERROR:root:Got exception on main handler Traceback (most recent call last): File "initiator.py", line 88, in main application.put_order() File "initiator.py", line 71, in put_order fix.Session.sendToTarget(trade) SessionNotFound: Session Not Found – worldexplorer95 Dec 15 '15 at 20:20
  • You did not follow my instructions. You must include the underscore. The call should be `fix.Session_sendToTarget(trade)`. – Wapiti Dec 15 '15 at 20:50
  • you're right sorry. I got the following traceback : Traceback (most recent call last): File "initiator.py", line 88, in main application.put_order() File "initiator.py", line 71, in put_order fix.Session_sendToTarget(trade) SessionNotFound: Session Not Found – worldexplorer95 Dec 15 '15 at 21:03
  • will you please update your question to reflect the code you are actually using now, and add your config file as well – Wapiti Dec 16 '15 at 16:35
  • just did. Thanks for the help. – worldexplorer95 Dec 16 '15 at 16:46
  • are `host` and `port` and `log` and `store` actually defined in your config file, and you've anonymized them for the interwebs? if they're not defined, you need to define them. if they are defined, try changing to `logFactory =fix.ScreenLogFactory(settings)` and run again. this will print up your session when you start the program. tell me what it prints – Wapiti Dec 16 '15 at 16:52
  • I have them defined. I updated the question with the log. Thanks!! – worldexplorer95 Dec 16 '15 at 17:12
  • It looks like your counterparty is not letting you log on. Thus you are not connected and cannot send an order. Make sure you can log on, and that heartbeat messages are coming through and try again. – Wapiti Dec 16 '15 at 17:34
  • Thanks , I really appreciate your help. I will talk to my counterparty then. – worldexplorer95 Dec 16 '15 at 17:35
  • No problem. Remember to accept the answer if when you logon your problem is solved. Otherwise let me know the new problem – Wapiti Dec 17 '15 at 18:05
  • do you know how to add username and password to the logon message? Thank you in advance. – worldexplorer95 Dec 22 '15 at 16:33
  • You should ask a new question -- this question is messy enough already – Wapiti Dec 22 '15 at 20:56
  • Add a link here to your question when you post it. Then I will move this messy comment thread to chat. – Wapiti Dec 23 '15 at 05:41
  • here it is http://stackoverflow.com/questions/34434302/python-quickfix-getheader-attribute-error-when-trying-to-login , thanks! – worldexplorer95 Dec 23 '15 at 11:08