如何在QuickFix Python中为Logon报文添加Tag 554密码字段
Hey there! I see you're close to getting this working—your initial attempt is on the right track, but there are a couple of key tweaks needed to make sure the password field only gets added to Logon messages (and that it's formatted correctly for QuickFix Python).
The Problem with Your Current Code
Your toAdmin method tries to add the field to every admin message, which includes heartbeats, logout requests, etc. That's not compliant with FIX protocol rules, and most exchanges will reject those malformed messages. Also, QuickFix Python requires you to pass a proper Field object to setField, not just a tag number and value directly.
The Fix: Target Only Logon Messages
Here's the corrected toAdmin method, with explanations:
def toAdmin(self, sessionID, message): # First, check if this is a Logon message (MsgType = "A") msg_type = message.getHeader().getString(35) if msg_type == "A": # Create a StringField for Tag 554 and add it to the header password_field = quickfix.StringField(554, "your_actual_password") message.getHeader().setField(password_field)
Breakdown of the Changes:
- Filter by MsgType: We check if the message type (Tag 35) is
"A"—this is the FIX spec code for Logon messages. This ensures we only add the password where it's needed. - Use StringField: QuickFix Python requires field values to be wrapped in the appropriate field type class. Since Tag 554 is a string field, we use
quickfix.StringField. - Replace the placeholder: Swap
"your_actual_password"with your real exchange password, making sure it matches any formatting rules the exchange specifies (like length or allowed characters).
Full Example Application Class
To put this in context, here's a complete example of a QuickFix Application class with the corrected toAdmin method:
import quickfix import quickfix42 # Adjust this import based on your FIX version (e.g., quickfix44 for FIX 4.4) class MyFIXApplication(quickfix.Application): def onCreate(self, sessionID): pass def onLogon(self, sessionID): print(f"Successfully logged on to session {sessionID}!") def onLogout(self, sessionID): print(f"Logged out from session {sessionID}.") def toAdmin(self, sessionID, message): msg_type = message.getHeader().getString(35) if msg_type == "A": # Add Tag 554 Password field to Logon header message.getHeader().setField(quickfix.StringField(554, "your_secure_password")) def toApp(self, sessionID, message): pass def fromAdmin(self, sessionID, message): pass def fromApp(self, sessionID, message): pass
Quick Validation Tip
Enable QuickFix's session logging to verify the outbound Logon message includes Tag 554. You can configure this in your QuickFix settings file by setting FileLogPath to a directory—check the log files after starting your application to confirm the password field is present.
内容的提问来源于stack exchange,提问作者jgillespie484848

