Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
message_parser.cpp
Go to the documentation of this file.
1#include "message_parser.hpp"
2#include "fix_parser.hpp"
3#include "json_parser.hpp"
4#include <slick/logger.hpp>
5
7
8Side MessageParser::parse_side(const std::string& side_str) {
9 if (side_str == "1" || side_str == "B" || side_str == "BUY" || side_str == "buy") {
10 return Side::BUY;
11 } else if (side_str == "2" || side_str == "S" || side_str == "SELL" || side_str == "sell") {
12 return Side::SELL;
13 }
14 return Side::BUY; // Default
15}
16
17OrderType MessageParser::parse_order_type(const std::string& type_str) {
18 if (type_str == "1" || type_str == "MARKET" || type_str == "market") {
19 return OrderType::MARKET;
20 } else if (type_str == "2" || type_str == "LIMIT" || type_str == "limit") {
21 return OrderType::LIMIT;
22 } else if (type_str == "3" || type_str == "STOP" || type_str == "stop") {
23 return OrderType::STOP;
24 } else if (type_str == "4" || type_str == "STOP_LIMIT" || type_str == "stop_limit") {
26 }
27 return OrderType::MARKET; // Default
28}
29
31 if (tif_str == "0" || tif_str == "DAY" || tif_str == "day") {
32 return TimeInForce::DAY;
33 } else if (tif_str == "1" || tif_str == "GTC" || tif_str == "gtc") {
35 } else if (tif_str == "3" || tif_str == "IOC" || tif_str == "ioc") {
37 } else if (tif_str == "4" || tif_str == "FOK" || tif_str == "fok") {
39 }
40 return TimeInForce::DAY; // Default
41}
42
44 return std::to_string(static_cast<uint8_t>(side));
45}
46
48 return std::string(1, static_cast<char>(type));
49}
50
52 return std::string(1, static_cast<char>(status));
53}
54
55std::unique_ptr<MessageParser> MessageParserFactory::create_parser(ProtocolType type) {
56 switch (type) {
58 return std::make_unique<FixParser>();
60 // SBE is not one protocol but a family of venue schemas, each with its
61 // own session layer - so it is spoken by a venue adapter, not by a
62 // generic parser.
63 LOG_ERROR("SBE is decoded by venue adapters, not by a generic parser");
64 return nullptr;
66 return std::make_unique<JsonParser>();
67 default:
68 LOG_ERROR("Unsupported protocol type: {}", static_cast<int>(type));
69 return nullptr;
70 }
71}
72
73std::unique_ptr<MessageParser> MessageParserFactory::create_parser_from_data(const uint8_t* data, size_t length) {
74 if (length == 0) return nullptr;
75
76 // Try each available parser to see which one can handle the data
77 std::vector<std::unique_ptr<MessageParser>> parsers;
78 parsers.push_back(std::make_unique<JsonParser>());
79 parsers.push_back(std::make_unique<FixParser>());
80
81 for (auto& parser : parsers) {
82 if (parser->can_handle(data, length)) {
83 LOG_DEBUG("Auto-detected protocol: {}", static_cast<int>(parser->get_protocol_type()));
84 return std::move(parser);
85 }
86 }
87
88 LOG_WARN("No parser found for data, defaulting to JSON");
89 return std::make_unique<JsonParser>();
90}
91
92} // namespace slick::sim::order_gateway
static std::unique_ptr< MessageParser > create_parser(ProtocolType type)
static std::unique_ptr< MessageParser > create_parser_from_data(const uint8_t *data, size_t length)
static std::string side_to_string(Side side)
static TimeInForce parse_time_in_force(const std::string &tif_str)
static std::string order_status_to_string(OrderStatus status)
static Side parse_side(const std::string &side_str)
static OrderType parse_order_type(const std::string &type_str)
static std::string order_type_to_string(OrderType type)