Python - 快速修复属性错误: sessionID

标签 python attributeerror quickfix fix-protocol

我一直在尝试 QuickFix,但收到“AttributeError:SessionID”。老实说,我不知道为什么会发生这种情况,我对代码做了一些修改,但问题仍然存在。另外,谷歌在这方面让我很失望,所以我希望你们能帮助我。

代码:

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)

我收到以下错误消息:

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

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

<小时/>

更新:

产生错误的代码:

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)

配置文件:

[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 

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

最佳答案

尝试完全切断这条线self.sessionID = sessionID

编辑——好的,感谢您提供回溯。您应该替换行 fix.Session.sendToTarget(trade, self.sessionID)线路 fix.Session_sendToTarget(trade)让我知道进展如何。

关于Python - 快速修复属性错误: sessionID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33897426/

相关文章:

python - Openpyxl:确定单元格值中的哪个字符是删除线

python - 快速修复 : how to get Symbol ( flag 55 ) from messages?

java - 如何使用私钥通过quickfix连接服务器

python - youtube dl - 覆盖元数据

python - Sphinx 文档处理器扩展对 HTML 和 LaTeX 输出的工作方式不同?

python - 从 Python 中的模型在 Postgres DB 中创建表/生成模式

Python tkinter 通过按钮添加新字段

python - AttributeError:类型对象没有属性(*args 有问题?)

c++ - 开源 FIX 客户端模拟器