Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
fix_parser.cpp
Go to the documentation of this file.
1#include "fix_parser.hpp"
2#include <slick/logger.hpp>
3#include <sstream>
4#include <iomanip>
5#include <quickfix/Message.h>
6#include <quickfix/Field.h>
7
9
10FixParser::FixParser() : exec_id_counter_(1), current_client_id_(-1) {}
11
12bool FixParser::can_handle(const uint8_t* data, size_t length) const {
13 if (length < 10) return false;
14
15 std::string str(reinterpret_cast<const char*>(data), std::min(length, size_t(50)));
16
17 // Check for FIX protocol markers
18 return str.find("8=FIX") == 0 || str.find("8=FIXT") == 0;
19}
20
21ParseResult FixParser::parse_message(const uint8_t* data, size_t length, int client_id) {
22 ParseResult result;
23
24 current_client_id_ = client_id;
25
26 try {
27 // Accumulate data in buffer
28 message_buffer_.append(reinterpret_cast<const char*>(data), length);
29
30 // Process complete FIX messages (ending with SOH - ASCII 1)
31 size_t pos = 0;
32 while ((pos = message_buffer_.find('\001')) != std::string::npos) {
33 std::string fix_message = message_buffer_.substr(0, pos + 1);
34 message_buffer_.erase(0, pos + 1);
35
36 if (is_complete_fix_message(fix_message)) {
37 FIX::Message message(fix_message);
38
39 // Create a fake session ID for message cracking
40 FIX::SessionID session_id("FIX.4.4", "EXCHANGE", std::to_string(client_id));
41
42 // Reset last result and crack the message
43 last_result_ = ParseResult{};
44 crack(message, session_id);
45
46 result = std::move(last_result_);
47 result.is_complete_message = true;
48
49 if (result.has_order()) {
50 LOG_DEBUG("FIX: Parsed order for client {}: {}", client_id,
51 result.order->client_order_id.view());
52 return result;
53 }
54 }
55 }
56
57 // If we get here, no complete message was found
58 result.is_complete_message = false;
59
60 } catch (const std::exception& e) {
61 result.error_message = "FIX parsing error: " + std::string(e.what());
62 LOG_ERROR("FIX parsing error: {}", e.what());
63 }
64 return result;
65}
66
67void FixParser::onMessage(const FIX44::NewOrderSingle& message, [[maybe_unused]] const FIX::SessionID& sessionID) {
68 try {
69 Order order = parse_new_order_single(message, current_client_id_);
70 last_result_.order = std::move(order);
71 } catch (const std::exception& e) {
72 last_result_.error_message = "Error processing NewOrderSingle: " + std::string(e.what());
73 }
74}
75
76Order FixParser::parse_new_order_single(const FIX44::NewOrderSingle& message, int client_id) {
77 Order order;
78 order.client_id = client_id;
79
80 // Required fields
81 FIX::ClOrdID clOrdID;
82 message.get(clOrdID);
83 order.client_order_id = clOrdID.getValue();
84
85 FIX::Symbol symbol;
86 message.get(symbol);
87 order.symbol = symbol.getValue();
88
89 // Side
90 FIX::Side side;
91 message.get(side);
92 order.side = static_cast<Side>(side.getValue());
93
94 // Order Type
95 FIX::OrdType ordType;
96 message.get(ordType);
97 order.type = static_cast<OrderType>(ordType.getValue());
98
99 // Quantity
100 FIX::OrderQty qty;
101 message.get(qty);
102 order.quantity = static_cast<uint64_t>(qty.getValue());
103 order.leaves_quantity = order.quantity;
104
105 // Price (if limit order)
106 if (order.type == OrderType::LIMIT || order.type == OrderType::STOP_LIMIT) {
107 FIX::Price price;
108 message.get(price);
109 order.price = to_price_t(price.getValue());
110 }
111
112 // Time in Force
113 if (message.isSetField(FIX::FIELD::TimeInForce)) {
114 FIX::TimeInForce tif;
115 message.get(tif);
116 order.time_in_force = static_cast<TimeInForce>(tif.getValue());
117 }
118
119 return order;
120}
121
122std::vector<uint8_t> FixParser::serialize_response(const OrderResponse& response) {
123 try {
124 FIX44::ExecutionReport execReport = create_execution_report(response);
125 std::string fix_string = execReport.toString();
126
127 return std::vector<uint8_t>(fix_string.begin(), fix_string.end());
128 } catch (const std::exception& e) {
129 LOG_ERROR("FIX serialization error: {}", e.what());
130 return {};
131 }
132}
133
134FIX44::ExecutionReport FixParser::create_execution_report(const OrderResponse& response) {
135 FIX44::ExecutionReport execReport;
136
137 execReport.set(FIX::OrderID(response.client_order_id));
138 execReport.set(FIX::ExecID(response.order_id));
139 execReport.set(FIX::ExecType(static_cast<char>(response.order_status)));
140 execReport.set(FIX::OrdStatus(static_cast<char>(response.order_status)));
141 execReport.set(FIX::Side(FIX::Side_BUY)); // TODO: Should come from original order
142 execReport.set(FIX::LeavesQty(to_qty_double(response.leaves_qty)));
143 execReport.set(FIX::CumQty(to_qty_double(response.cum_qty)));
144 execReport.set(FIX::AvgPx(to_price_double(response.price)));
145
146 // if (response.reject_reason != OrdRejectReason::NONE) {
147 // execReport.set(FIX::Text(response.reject_reason));
148 // }
149
150 return execReport;
151}
152
153bool FixParser::is_complete_fix_message(const std::string& buffer) const {
154 // Very basic check - a complete FIX message should have BeginString and end with SOH
155 return buffer.find("8=FIX") == 0 && buffer.back() == '\001';
156}
157
158std::string FixParser::generate_exec_id() {
159 auto counter = exec_id_counter_.fetch_add(1, std::memory_order_relaxed);
160 std::ostringstream oss;
161 oss << "FIX_EXEC" << std::setfill('0') << std::setw(8) << counter;
162 return oss.str();
163}
164
166 message_buffer_.clear();
167 last_result_ = ParseResult{};
168 current_client_id_ = -1;
169}
170
171} // namespace slick::sim::order_gateway
ParseResult parse_message(const uint8_t *data, size_t length, int client_id) override
std::vector< uint8_t > serialize_response(const OrderResponse &response) override
bool can_handle(const uint8_t *data, size_t length) const override
void onMessage(const FIX44::NewOrderSingle &message, const FIX::SessionID &sessionID)
constexpr double to_price_double(price_t price)
Definition types.hpp:46
constexpr price_t to_price_t(double price)
Scales a decimal into the fixed-point representation, rounding to the nearest tick rather than trunca...
Definition types.hpp:42
constexpr double to_qty_double(qty_t qty)
Definition types.hpp:54
utils::fixed_string< 32 > symbol
Definition order.hpp:289
utils::fixed_string< 37 > client_order_id
Definition order.hpp:291
OrderType type
Definition order.hpp:306
TimeInForce time_in_force
Definition order.hpp:308
qty_t leaves_quantity
Definition order.hpp:316