编程语言
首页 > 编程语言> > Python-Quickfix:尝试登录时出现getHeader()属性错误

Python-Quickfix:尝试登录时出现getHeader()属性错误

作者:互联网

我正在使用Quickfix,并且修改了toAdmin函数,以将用户名和密码插入登录消息.我从c指令改编了我的代码,但是出现了一个奇怪的getHeader()属性错误.

追溯如下:

<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

我的代码如下:

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()

解决方法:

这似乎大体上是正确的,但是,我相信您以错误的顺序输入了论点.尝试如下定义函数:

def toAdmin(自己,消息,会话ID):

标签:python-2-7,quickfix,python
来源: https://codeday.me/bug/20191027/1945350.html