[英]Wait for initiator to logon before continue with QuickFix in python
I'm trying to login to a FIX server and send an order.我正在尝试登录 FIX 服务器并发送订单。 This works if I wait 0.1 seconds after I start the initiator
.如果我在启动initiator
后等待 0.1 秒,这将起作用。 But if I don't, the script tries to send with a session not logged yet.但如果我不这样做,脚本会尝试使用尚未记录的会话发送。 How can I tell quickfix to wait for loggon confirmation?如何告诉 quickfix 等待登录确认?
error:错误:
...
line 22, in main
quickfix.Session.sendToTarget(msg, application.sessionID)
File "C:\Users\XXXX\Anaconda3\lib\site-packages\quickfix.py", line 39558, in <lambda>
2022-05-18 14:51:02,739 : (Admin) S >> 8=FIX.4.4|9=199|35=A|34=1|49=ABCABCABC|52=20220518-18:51:02.000|56=FGHFGHFGH|95=103|96=XYZXYZXYZXYZ|98=0|108=30|141=Y|10=120|
__getattr__ = lambda self, name: _swig_getattr(self, Application, name)
File "C:\Users\XXXX\Anaconda3\lib\site-packages\quickfix.py", line 83, in _swig_getattr
raise AttributeError("'%s' object has no attribute '%s'" % (class_type.__name__, name))
AttributeError: 'Application' object has no attribute 'sessionID'
main code:主要代码:
import quickfix
import time
from application import Application
settings = quickfix.SessionSettings(config_file)
application = Application()
storefactory = quickfix.FileStoreFactory(settings)
logfactory = quickfix.FileLogFactory(settings)
initiator = quickfix.SocketInitiator(application, storefactory, settings, logfactory)
initiator.start()
time.sleep(0.1) # If I delete/comment this line, code does not work
msg = application.get_new_order_message('INSTRMNT', 1000, 1.4, OrderType.Bid, SettlementType.CN)
quickfix.Session.sendToTarget(msg, application.sessionID)
initiator.stop()
application code:应用代码:
class Application(fix.Application):
"""FIX Application"""
execID = 0
def onCreate(self, sessionID):
print("onCreate : Session (%s)" % sessionID.toString())
return
def onLogon(self, sessionID):
self.sessionID = sessionID
print("Successful Logon to session '%s'." % sessionID.toString())
return
def onLogout(self, sessionID):
print("Session (%s) logout !" % sessionID.toString())
return
def toAdmin(self, message, sessionID):
message.setField(fix.RawData(RD))
message.setField(fix.RawDataLength(len(RD)))
msg = message.toString().replace(__SOH__, "|")
logfix.info("(Admin) S >> %s" % msg)
return
def fromAdmin(self, message, sessionID):
msg = message.toString().replace(__SOH__, "|")
logfix.info("(Admin) R << %s" % msg)
self.message_returned = True
return
def toApp(self, message, sessionID):
msg = message.toString().replace(__SOH__, "|")
logfix.info("(App) S >> %s" % msg)
return
def fromApp(self, message, sessionID):
msg = message.toString().replace(__SOH__, "|")
logfix.info("(App) R << %s" % msg)
self.onMessage(message, sessionID)
return
def onMessage(self, message, sessionID):
"""Processing application message here"""
pass
def genExecID(self):
self.execID += 1
return str(self.execID).zfill(5)
#some extra methods to be called like get_new_order_message()'
In quickfix you can check if the session is logged on:在 quickfix 中,您可以检查会话是否已登录:
Session.lookupSession(sessionID).isLoggedOn()
This can be wrapped up in a for loop and actively wait until the session is logged on, or timeout after some time.这可以被包裹在一个 for 循环中并主动等待,直到会话登录,或者在一段时间后超时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.