FIX 4.0 Demo 1.0
Loading...
Searching...
No Matches
fix_message_builder.hpp
Go to the documentation of this file.
1
8#pragma once
9
10#include "fix/fix_codec.hpp"
11#include "fix/fix_tags.hpp"
12#include "app/model/order.hpp"
13#include <sstream>
14#include <iomanip>
15#include <ctime>
16
17namespace fix40 {
18
24inline std::string formatTransactTime(std::chrono::system_clock::time_point tp) {
25 auto time_t_val = std::chrono::system_clock::to_time_t(tp);
26 std::tm tm_buf;
27 gmtime_r(&time_t_val, &tm_buf);
28
29 std::ostringstream oss;
30 oss << std::put_time(&tm_buf, "%Y%m%d-%H:%M:%S");
31 return oss.str();
32}
33
37inline std::string sideToFix(OrderSide side) {
38 return side == OrderSide::BUY ? "1" : "2";
39}
40
44inline std::string ordTypeToFix(OrderType type) {
45 return type == OrderType::MARKET ? "1" : "2";
46}
47
51inline std::string ordStatusToFix(OrderStatus status) {
52 switch (status) {
53 case OrderStatus::NEW: return "0";
54 case OrderStatus::PARTIALLY_FILLED: return "1";
55 case OrderStatus::FILLED: return "2";
56 case OrderStatus::CANCELED: return "4";
57 case OrderStatus::PENDING_CANCEL: return "6";
58 case OrderStatus::REJECTED: return "8";
59 case OrderStatus::PENDING_NEW: return "A"; // FIX 4.2+, 但为了兼容
60 default: return "0";
61 }
62}
63
67inline std::string execTransTypeToFix(ExecTransType type) {
68 switch (type) {
69 case ExecTransType::NEW: return "0";
70 case ExecTransType::CANCEL: return "1";
71 case ExecTransType::CORRECT: return "2";
72 case ExecTransType::STATUS: return "3";
73 default: return "0";
74 }
75}
76
85 FixMessage msg;
86
87 // MsgType = 8 (ExecutionReport)
88 msg.set(tags::MsgType, "8");
89
90 // 标识符
91 msg.set(tags::OrderID, report.orderID);
92 msg.set(tags::ClOrdID, report.clOrdID);
93 msg.set(tags::ExecID, report.execID);
94
95 if (!report.origClOrdID.empty()) {
97 }
98
99 // 执行信息
102
103 // 订单信息
104 msg.set(tags::Symbol, report.symbol);
105 msg.set(tags::Side, sideToFix(report.side));
106 msg.set(tags::OrderQty, std::to_string(report.orderQty));
107
108 if (report.ordType == OrderType::LIMIT && report.price > 0) {
109 msg.set(tags::Price, std::to_string(report.price));
110 }
112
113 // 成交信息
114 msg.set(tags::CumQty, std::to_string(report.cumQty));
115 msg.set(tags::AvgPx, std::to_string(report.avgPx));
116
117 if (report.lastShares > 0) {
118 msg.set(tags::LastShares, std::to_string(report.lastShares));
119 msg.set(tags::LastPx, std::to_string(report.lastPx));
120 }
121
122 // 剩余数量(FIX 4.0 没有 LeavesQty,但可以通过 OrderQty - CumQty 计算)
123 // 这里我们用 Text 字段传递额外信息
124
125 // 时间
127
128 // 拒绝原因
129 if (report.ordStatus == OrderStatus::REJECTED && report.ordRejReason != 0) {
130 msg.set(tags::OrdRejReason, std::to_string(report.ordRejReason));
131 }
132
133 // 文本说明
134 if (!report.text.empty()) {
135 msg.set(tags::Text, report.text);
136 }
137
138 return msg;
139}
140
141} // namespace fix40
FIX 消息的面向对象封装
Definition fix_codec.hpp:46
void set(int tag, const std::string &value)
设置字符串类型字段
Definition fix_codec.hpp:53
FIX 消息编解码器
FIX 4.0 协议标签定义
constexpr int AvgPx
平均成交价格 (Average Price)
Definition fix_tags.hpp:141
constexpr int ClOrdID
客户端订单ID (Client Order ID),必填,客户端生成用于唯一标识订单
Definition fix_tags.hpp:79
constexpr int LastPx
本次成交价格 (Last Price) - FIX 4.0 用于表达单次成交价
Definition fix_tags.hpp:147
constexpr int OrderQty
订单数量 (Order Quantity)
Definition fix_tags.hpp:107
constexpr int Price
价格 (Price),限价单必填
Definition fix_tags.hpp:110
constexpr int Side
买卖方向 (Side): 1=Buy, 2=Sell
Definition fix_tags.hpp:104
constexpr int Text
文本消息(用于 Logout 原因等)
Definition fix_tags.hpp:65
constexpr int OrderID
交易所/服务端订单ID (Order ID),服务端生成
Definition fix_tags.hpp:82
constexpr int CumQty
累计成交数量 (Cumulative Quantity)
Definition fix_tags.hpp:138
constexpr int TransactTime
交易时间 (Transaction Time),UTC 时间
Definition fix_tags.hpp:116
constexpr int OrdType
订单类型 (Order Type): 1=Market, 2=Limit
Definition fix_tags.hpp:113
constexpr int ExecID
执行ID (Execution ID),每一笔成交或状态变化都应有唯一的执行ID
Definition fix_tags.hpp:85
constexpr int OrdStatus
订单状态 (Order Status) 0=New, 1=Partially Filled, 2=Filled, 4=Canceled, 8=Rejected
Definition fix_tags.hpp:130
constexpr int MsgType
消息类型(如 "A"=Logon, "0"=Heartbeat, "5"=Logout)
Definition fix_tags.hpp:33
constexpr int OrigClOrdID
原始客户端订单ID (Original Client Order ID),撤单/改单时引用原订单
Definition fix_tags.hpp:88
constexpr int Symbol
标的代码 (Symbol),如 "IF2305", "AAPL"
Definition fix_tags.hpp:101
constexpr int OrdRejReason
订单拒绝原因 (Order Reject Reason)
Definition fix_tags.hpp:150
constexpr int LastShares
本次成交数量 (Last Shares) - FIX 4.0 用于表达单次成交量
Definition fix_tags.hpp:144
constexpr int ExecTransType
执行事务类型 (Execution Transaction Type) - FIX 4.0 核心字段 0=New, 1=Cancel, 2=Correct, 3=Status 注意:FIX 4....
Definition fix_tags.hpp:135
Definition matching_engine.hpp:23
ExecTransType
执行事务类型 (FIX 4.0)
Definition order.hpp:72
@ CORRECT
更正之前的执行报告
@ CANCEL
取消之前的执行报告
@ NEW
新执行报告
@ STATUS
状态查询响应
std::string sideToFix(OrderSide side)
将 OrderSide 转换为 FIX 字符串
Definition fix_message_builder.hpp:37
std::string ordTypeToFix(OrderType type)
将 OrderType 转换为 FIX 字符串
Definition fix_message_builder.hpp:44
OrderStatus
订单状态
Definition order.hpp:58
@ NEW
新订单(已接受)
@ PENDING_NEW
待确认(内部状态)
@ PARTIALLY_FILLED
部分成交
@ PENDING_CANCEL
待撤销(内部状态)
@ FILLED
全部成交
@ REJECTED
已拒绝
@ CANCELED
已撤销
OrderSide
买卖方向
Definition order.hpp:26
FixMessage buildExecutionReport(const ExecutionReport &report)
将 ExecutionReport 转换为 FIX 消息
Definition fix_message_builder.hpp:84
OrderType
订单类型
Definition order.hpp:35
@ LIMIT
限价单
@ MARKET
市价单
std::string execTransTypeToFix(ExecTransType type)
将 ExecTransType 转换为 FIX 字符串
Definition fix_message_builder.hpp:67
std::string ordStatusToFix(OrderStatus status)
将 OrderStatus 转换为 FIX 字符串
Definition fix_message_builder.hpp:51
std::string formatTransactTime(std::chrono::system_clock::time_point tp)
格式化 UTC 时间为 FIX 格式
Definition fix_message_builder.hpp:24
内部订单数据结构
执行报告
Definition order.hpp:195
std::string clOrdID
客户端订单ID
Definition order.hpp:198
int64_t orderQty
订单数量
Definition order.hpp:206
OrderStatus ordStatus
订单状态
Definition order.hpp:211
OrderSide side
买卖方向
Definition order.hpp:204
std::string orderID
服务端订单ID
Definition order.hpp:197
OrderType ordType
订单类型
Definition order.hpp:205
std::string origClOrdID
原订单ID(撤单时使用)
Definition order.hpp:200
double avgPx
平均成交价
Definition order.hpp:216
ExecTransType execTransType
执行事务类型
Definition order.hpp:210
std::string execID
执行ID(每次报告唯一)
Definition order.hpp:199
int64_t lastShares
本次成交数量 (FIX 4.0: LastShares)
Definition order.hpp:212
std::chrono::system_clock::time_point transactTime
交易时间
Definition order.hpp:219
std::string text
文本说明
Definition order.hpp:223
double lastPx
本次成交价格
Definition order.hpp:213
int ordRejReason
拒绝原因代码
Definition order.hpp:222
double price
订单价格
Definition order.hpp:207
std::string symbol
标的代码
Definition order.hpp:203
int64_t cumQty
累计成交数量
Definition order.hpp:215