FIX 消息构建器
Fix Message Builder
题目详情
FIX 协议是量化金融中实时证券交易的电子通信标准。FIX 消息由分隔符分隔的 tag-value 对组成,要求精确的格式和严格的校验规则。高效构建和序列化 FIX 消息是低延迟交易系统的关键环节。
任务:实现 FIXMessageBuilder 类,按 FIX 协议规范构建消息。支持添加 tag-value 对、自动计算 BodyLength(Tag 9)和 CheckSum(Tag 10),并序列化为标准 FIX 字符串格式。
英文原题
The Financial Information eXchange (FIX) protocol is a standard electronic communications protocol used in quantitative finance for the real-time exchange of securities transactions. A FIX message consists of a collection of tag-value pairs separated by a delimiter, requiring precise formatting and checksum validation to ensure data integrity during high-frequency trading.
Task
Implement the build method within the FIXMessageBuilder class that constructs a valid FIX 4.2 message string given a s
解析
问题分析
FIX(金融信息交换)协议是电子交易的行业标准。消息构建需要按 FIX 标签-值格式编码,校验和是对消息字节求和模 256。会话层管理序列号、心跳和重传。
解法
class FIXMessage {
std::string body_;
public:
FIXMessage& add(int tag, const std::string& val) { body_ += std::to_string(tag) + "=" + val + "\x01"; return *this; }
std::string build() const {
std::string msg = "8=FIX.4.2\x01" + body_;
int checksum = 0; for (char c : msg) checksum += (unsigned char)c;
return msg + "10=" + std::to_string(checksum % 256) + "\x01";
}
};
// 使用: FIXMessage().add(55,"AAPL").add(54,1).add(38,"100").add(44,"150.25").build()验证
构建订单消息: 8=FIX.4.2|55=AAPL|54=1|38=100|44=150.25|10=XXX
checksum = (各字节之和) % 256,确保与接收端计算一致 ✓
复杂度与边界
- 时间复杂度:build O(N),N 为消息长度
- 边界条件:(1) 字段值含分隔符需转义 (2) checksum 三位数不足补零 (3) 序列号跳变触发重传
英文解析
Analysis
FIX (Financial Information Exchange) protocol is the industry standard for electronic trading. Message construction requires encoding in FIX tag-value format. The checksum is the sum of all message bytes modulo 256. The session layer manages sequence numbers, heartbeats, and retransmission.
Solution
class FIXMessage {
std::string body_;
public:
FIXMessage& add(int tag, const std::string& val) { body_ += std::to_string(tag) + "=" + val + "\x01"; return *this; }
std::string build() const {
std::string msg = "8=FIX.4.2\x01" + body_;
int checksum = 0; for (char c : msg) checksum += (unsigned char)c;
return msg + "10=" + std::to_string(checksum % 256) + "\x01";
}
};
// Usage: FIXMessage().add(55,"AAPL").add(54,1).add(38,"100").add(44,"150.25").build()Verification
Build order message: 8=FIX.4.2|55=AAPL|54=1|38=100|44=150.25|10=XXX. checksum = (sum of all bytes) % 256, must match receiver-side calculation.
Complexity & Edge Cases
- Time complexity: build O(N), N = message length
- Edge cases: (1) Field values containing separator character must be escaped (2) checksum padded to 3 digits with leading zeros (3) Sequence number gap triggers retransmission
Key Considerations
- Tag ordering: FIX standard requires tags in ascending order within repeating groups; validator must enforce ordering for interop with exchange FIX engines
- Required tag enforcement: Each message type has mandatory tags (e.g., MsgType=35 requires Symbol, Side, OrderQty); builder must validate required tags before encoding
- Repeating group structure: Groups must have NoXxx tag preceding group entries; each entry must contain all required group tags
- Checksum computation: FIX checksum (tag 10) covers entire message including SOH delimiters; must be computed and appended as final three-digit field