Python - Quickfix : getHeader() attribute error wh

2020-07-22 19:57发布

问题:

I am using Quickfix and I modified my toAdmin function to insert the username and password into the logon message. I adapted my code from the c++ instructions but I got a weird getHeader() attribute error.

The traceback is the following :

<20151223-10:48:31.142, FIX.4.2:MATHCLIENT1->CSTEST, event>
  (Created session)
Type 1 for order , 2 to exit and d to debug.
<20151223-10:48:31.149, FIX.4.2:CLIENT1->TEST, event>
  (Connecting to hostX on port Y)

Traceback (most recent call last):
  File "initiator.py", line 28, in toAdmin
    message.getHeader ().getField (msgType)
  File "C:\Users\user\Anaconda\lib\site-packages\quickfix.py", line 27015, in <lambda>
    __getattr__ = lambda self, name: _swig_getattr(self, SessionID, name)
  File "C:\Users\user\Anaconda\lib\site-packages\quickfix.py", line 57, in _swig_getattr
    raise AttributeError(name)
AttributeError: getHeader

My code follows below :

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):
    msgType = fix.MsgType ()
    message.getHeader ().getField (msgType)
    if (msgType.getValue () == fix.MsgType_Logon):
        print 'Logging on.'
        # message.setField (fix.Password (settings.get (self.sessionID).getString ("Password")))
        # message.setField (fix.Username (settings.get (self.sessionID).getString ("Username")))   
        message.setField(fix.Password('password'))
        message.setField(fix.Username('username'))        
        message.setField (fix.ResetSeqNumFlag (True))     
    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(config_file)
        application = Application()
        storeFactory = fix.FileStoreFactory(settings)
        # logFactory = fix.FileLogFactory(settings)
        logFactory = fix.ScreenLogFactory(settings)
        initiator = fix.SocketInitiator(application, storeFactory, settings, logFactory)
        initiator.start()

    print 'Type 1 for order , 2 to exit and d to debug.'
    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__':
    # logfile = open('errorlog.txt','w')
    # original_stderr = sys.stderr
    # sys.stderr = logfile
    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)
    # sys.stderr = original_stderr
    # logfile.close()

回答1:

This looks mostly correct, however, I believe you have entered your arguments in the wrong order. Try defining your function as follows:

def toAdmin(self, message, sessionID):