diff --git a/src/lib/api/md/TaifexMarketDataConfig.proto b/src/lib/api/md/TaifexMarketDataConfig.proto index 519777008..e570bf9a1 100644 --- a/src/lib/api/md/TaifexMarketDataConfig.proto +++ b/src/lib/api/md/TaifexMarketDataConfig.proto @@ -10,14 +10,8 @@ option csharp_namespace = "bts.api.md"; message TaifexMarketDataConfig { uint32 unique_check_value = 1; bool read_from_file = 20; - //uint32 num_depth_levels = 1; - //uint32 depth_throttle_ms = 2; + string ref_data_file_name = 21; + string realtime_data_file_name = 22; bts.api.domain.SecurityCriteria security_criteria = 3; - // Market data isn't subscribed for securities listed here (even if they are - // included by 'security_criteria'). There are some securities that don't - // fit into our system just yet, such as 'BTCBIDR', which trades in prices - // larger than our cpp 'domain::Price' class supports. - //repeated string excluded_securities = 4; - //string url_rest_base = 5; - //string url_ws_base = 6; + } diff --git a/src/server/sparkd/md/taifex/TaifexDataHandler.cpp b/src/server/sparkd/md/taifex/TaifexDataHandler.cpp index 2a4546a4d..9697cc9d5 100644 --- a/src/server/sparkd/md/taifex/TaifexDataHandler.cpp +++ b/src/server/sparkd/md/taifex/TaifexDataHandler.cpp @@ -2,6 +2,7 @@ #include "TaifexMessages.h" #include "TaifexSecurityDetailsMapper.h" #include "lib/api/domain/Venue.pb.h" +#include "lib/api/md/TaifexMarketDataConfig.pb.h" #include "lib/config/config.h" #include "lib/config/protos/system_config.pb.h" #include "lib/core/TimeStamp.h" @@ -12,6 +13,7 @@ #include "lib/logging/logging.h" #include "lib/md/IMarketDataListener.h" #include "lib/persistence/IDGenerator.h" +#include "lib/utils/bts_assert.h" namespace bts::md::taifex { const size_t FOOTER_SIZE = 3; @@ -230,7 +232,6 @@ void handleParsedMessage( TaifexUtils::remove_spaces(i083_message.prod_id)); auto dec_loc = security_details->get_dec_loc( TaifexUtils::remove_spaces(i083_message.prod_id)); - LOG(md, Error, "XSEC: {}", i083_message.prod_id); if (!sec_id.has_value() || !dec_loc.has_value()) { break; } @@ -293,8 +294,8 @@ bool readBinaryFile( const size_t BUFFER_SIZE = 1024 * 1024; std::ifstream file(file_name, std::ios::binary); if (!file) { - std::cerr << "Failed to open file: " << file_name << std::endl; - assert(false); + LOG(md, Error, "File not found: {}",file_name); + bts_assert(false); } std::vector buffer( @@ -302,7 +303,6 @@ bool readBinaryFile( std::vector prev_data(1024); // Stores leftover data bool start = true; size_t prev_data_len = 0; // Length of leftover data - int counter = 0; while (file.read( reinterpret_cast(buffer.data() + prev_data_len), BUFFER_SIZE)) { size_t bytes_read = file.gcount(); @@ -310,21 +310,15 @@ bool readBinaryFile( break; // No more data to read } if (!start) { - counter++; // Move data from prev_data to the start of the buffer std::memcpy(buffer.data(), prev_data.data(), prev_data_len); } - if (counter == 5) - std::cout << "awein"; size_t position = 0; while (position < (BUFFER_SIZE + prev_data_len - HEADER_SIZE)) { auto Header = TaifexMessages::Header_Fields(buffer, position); size_t message_length = Header.Body_Length + HEADER_SIZE + FOOTER_SIZE; - if (position == 0 || position > (BUFFER_SIZE + prev_data_len - 2000)) { - std::cout << "awein"; - } // Check if the full message fits within the buffer if ((BUFFER_SIZE + prev_data_len - position) >= message_length) { // Handle the parsed message @@ -351,14 +345,16 @@ bool readBinaryFile( //const std::string &filename, void bts::md::taifex::TaifexDatahandler::run( - bool use_file, + const api::md::TaifexMarketDataConfig &config, std::shared_ptr listner, std::shared_ptr sec_id_store) { - std::string reference_file_name = "reference_data_20240806_1"; - std::string realtime_file_name = "real_time_data_20240806_274"; std::shared_ptr sec_details = std::make_shared(); - if (use_file) { + if (config.read_from_file()) { + bts_assert(!config.ref_data_file_name().empty()); + bts_assert(!config.realtime_data_file_name().empty()); + std::string reference_file_name = config.ref_data_file_name();//"reference_data_20240806_1"; + std::string realtime_file_name = config.realtime_data_file_name();//"real_time_data_20240806_274"; std::string base_path = config::get().data_path(); readBinaryFile( diff --git a/src/server/sparkd/md/taifex/TaifexDataHandler.h b/src/server/sparkd/md/taifex/TaifexDataHandler.h index a81d621d6..e295d82e9 100644 --- a/src/server/sparkd/md/taifex/TaifexDataHandler.h +++ b/src/server/sparkd/md/taifex/TaifexDataHandler.h @@ -1,5 +1,6 @@ #pragma once +#include "lib/api/md/TaifexMarketDataConfig.pb.h" #include namespace bts::persistence { @@ -15,7 +16,7 @@ namespace bts::md::taifex { class TaifexDatahandler { public: static void run( - bool use_file, + const api::md::TaifexMarketDataConfig &config, std::shared_ptr listner, std::shared_ptr sec_id_store); }; diff --git a/src/server/sparkd/md/taifex/TaifexMessages.h b/src/server/sparkd/md/taifex/TaifexMessages.h index ecd903479..ea421c3c7 100644 --- a/src/server/sparkd/md/taifex/TaifexMessages.h +++ b/src/server/sparkd/md/taifex/TaifexMessages.h @@ -7,930 +7,959 @@ #include namespace bts::md::taifex { class TaifexMessages { - public: + public: struct MessageIdentifier { - enum MessageID { - I010 = 1, - I030 = 2, - I011 = 3, - I050 = 4, - I060 = 5, - I120 = 6, - I130 = 7, - I064 = 8, - I012 = 9, - I083 = 10, - I140 = 11, - I110 = 12, - I014 = 13, - I024 = 14, - I025 = 15, - I100 = 16, - I081 = 17, // Add I081 - I084 = 18, // Add I084 - I072 = 19, // Add I072 - I071 = 20, // Add I071 - I073 = 21, // Add I073 - I070 = 22, - I001 = 23, - I002 = 24, - I999 = 0 // unknown + enum MessageID { + I010 = 1, + I030 = 2, + I011 = 3, + I050 = 4, + I060 = 5, + I120 = 6, + I130 = 7, + I064 = 8, + I012 = 9, + I083 = 10, + I140 = 11, + I110 = 12, + I014 = 13, + I024 = 14, + I025 = 15, + I100 = 16, + I081 = 17, // Add I081 + I084 = 18, // Add I084 + I072 = 19, // Add I072 + I071 = 20, // Add I071 + I073 = 21, // Add I073 + I070 = 22, + I001 = 23, + I002 = 24, + I999 = 0 // unknown + }; + static std::string messageIDToString(MessageID id) { + switch (id) { + case I010: + return "I010"; + case I030: + return "I030"; + case I011: + return "I011"; + case I050: + return "I050"; + case I060: + return "I060"; + case I120: + return "I120"; + case I130: + return "I130"; + case I064: + return "I064"; + case I012: + return "I012"; + case I083: + return "I083"; + case I140: + return "I140"; + case I110: + return "I110"; + case I014: + return "I014"; + case I024: + return "I024"; + case I025: + return "I025"; + case I100: + return "I100"; + case I081: + return "I081"; + case I084: + return "I084"; + case I072: + return "I072"; + case I071: + return "I071"; + case I073: + return "I073"; + case I070: + return "I070"; + case I001: + return "I001"; + case I002: + return "I002"; + default: + return "Unknown"; + } + } + void set_identifier_description( + const unsigned char transmissionCode, + const unsigned char messageKind) { + switch (transmissionCode) { + case '0': + switch (messageKind) { + case '1': + identifier = I001; + description = "Heartbeat"; + break; + case '2': + identifier = I002; + description = "Reset message"; + break; + default: + identifier = I999; + description = "unknown"; + break; + } + break; + case '1': + switch (messageKind) { + case '1': + identifier = I010; + description = "Product basic data"; + break; + case '2': + identifier = I030; + description = "Sum of order data"; + break; + case '3': + identifier = I011; + description = "Contract basic data"; + break; + case '4': + identifier = I050; + description = "System message"; + break; + case '5': + identifier = I060; + description = "Underlying index or stock reference data"; + break; + case '6': + identifier = I120; + description = "Reference of stock or underlying index"; + break; + case '7': + identifier = I130; + description = "Contract Adjustment"; + break; + case '8': + identifier = I064; + description = + "Calculated opening and closing price for underlying stocks"; + break; + case 'A': + identifier = I012; + description = "Price limits"; + break; + default: + identifier = I999; + description = "unknown"; + break; + } + break; + case '2': + switch (messageKind) { + case 'B': + identifier = I083; + description = "Snapshot order book"; + break; + case '3': + identifier = I140; + description = "System message"; + break; + case '4': + identifier = I100; + description = "Closing market data (settlement price included)"; + break; + case 'A': + identifier = I081; + description = "Aggregated order book"; + break; + case 'C': + identifier = I084; + description = "Market data snapshot refresh"; + break; + case 'D': + identifier = I024; + description = "Matched prices and quantities"; + break; + case 'E': + identifier = I025; + description = "Closing market data"; + break; + default: + identifier = I999; + description = "unknown"; + break; + } + break; + case '3': + switch (messageKind) { + case '3': + identifier = I072; + description = "Closing market data"; + break; + case '1': + identifier = I070; + description = "Closing market data"; + break; + case '2': + identifier = I071; + description = "Closing market data with settlement price"; + break; + case '4': + identifier = I073; + description = "Combination product closing market data"; + break; + default: + identifier = I999; + description = "unknown"; + break; + } + break; + default: + identifier = I999; + description = "unknown"; + break; + } + } + MessageIdentifier( + const unsigned char transmissionCode, const unsigned char messageKind) + : transmissionCode(transmissionCode), messageKind(messageKind) { + set_identifier_description(transmissionCode, messageKind); + } + unsigned char transmissionCode; + unsigned char messageKind; + MessageID identifier; + std::string description; + friend std::ostream &operator<<( + std::ostream &os, const MessageIdentifier &MI) { + os << "TransmissionCode: " << MI.transmissionCode + << "\nMessageKind: " << MI.messageKind + << "\nDescription: " << MI.description << "\nidentifier: " + << MessageIdentifier::messageIDToString(MI.identifier); + return os; + } }; - static std::string messageIDToString(MessageID id) { - switch (id) { - case I010: - return "I010"; - case I030: - return "I030"; - case I011: - return "I011"; - case I050: - return "I050"; - case I060: - return "I060"; - case I120: - return "I120"; - case I130: - return "I130"; - case I064: - return "I064"; - case I012: - return "I012"; - case I083: - return "I083"; - case I140: - return "I140"; - case I110: - return "I110"; - case I014: - return "I014"; - case I024: - return "I024"; - case I025: - return "I025"; - case I100: - return "I100"; - case I081: - return "I081"; - case I084: - return "I084"; - case I072: - return "I072"; - case I071: - return "I071"; - case I073: - return "I073"; - case I070: - return "I070"; - case I001: - return "I001"; - case I002: - return "I002"; - default: - return "Unknown"; + + struct Header_Fields { + uint8_t Esc_Code; // 1 byte + MessageIdentifier message_type; + std::string Information_Time; + int Channel_Id; + std::string Channel_Seq; + uint8_t Version_No; + uint Body_Length; + bool valid = false; + Header_Fields( + const std::vector &buffer, const size_t &offset) + : Esc_Code(buffer[offset]), + message_type(buffer[offset + 1], buffer[offset + 2]) { + // creates header feild struct + // and populates all fields + // Header_Fields header; + // Read single-byte fields + assert(message_type.identifier != MessageIdentifier::MessageID::I999); + // Read 2 bytes for Body_Length + unsigned char bl[2] = {buffer[offset + 17], buffer[offset + 18]}; + Body_Length = std::stoul(TaifexUtils::decodePackBCD(bl, 2)); + valid = true; } - } - void set_identifier_description( - const unsigned char transmissionCode, const unsigned char messageKind) { - switch (transmissionCode) { - case '0': - switch (messageKind) { - case '1': - identifier = I001; - description = "Heartbeat"; - break; - case '2': - identifier = I002; - description = "Reset message"; - break; - default: - identifier = I999; - description = "unknown"; - break; - } - break; - case '1': - switch (messageKind) { - case '1': - identifier = I010; - description = "Product basic data"; - break; - case '2': - identifier = I030; - description = "Sum of order data"; - break; - case '3': - identifier = I011; - description = "Contract basic data"; - break; - case '4': - identifier = I050; - description = "System message"; - break; - case '5': - identifier = I060; - description = "Underlying index or stock reference data"; - break; - case '6': - identifier = I120; - description = "Reference of stock or underlying index"; - break; - case '7': - identifier = I130; - description = "Contract Adjustment"; - break; - case '8': - identifier = I064; - description = - "Calculated opening and closing price for underlying stocks"; - break; - case 'A': - identifier = I012; - description = "Price limits"; - break; - default: - identifier = I999; - description = "unknown"; - break; - } - break; - case '2': - switch (messageKind) { - case 'B': - identifier = I083; - description = "Snapshot order book"; - break; - case '3': - identifier = I140; - description = "System message"; - break; - case '4': - identifier = I100; - description = "Closing market data (settlement price included)"; - break; - case 'A': - identifier = I081; - description = "Aggregated order book"; - break; - case 'C': - identifier = I084; - description = "Market data snapshot refresh"; - break; - case 'D': - identifier = I024; - description = "Matched prices and quantities"; - break; - case 'E': - identifier = I025; - description = "Closing market data"; - break; - default: - identifier = I999; - description = "unknown"; - break; - } - break; - case '3': - switch (messageKind) { - case '3': - identifier = I072; - description = "Closing market data"; - break; - case '1': - identifier = I070; - description = "Closing market data"; - break; - case '2': - identifier = I071; - description = "Closing market data with settlement price"; - break; - case '4': - identifier = I073; - description = "Combination product closing market data"; - break; - default: - identifier = I999; - description = "unknown"; - break; - } - break; - default: - identifier = I999; - description = "unknown"; - break; + + friend std::ostream &operator<<( + std::ostream &os, const Header_Fields &hf) { + os << "Esc_Code: " << static_cast(hf.Esc_Code) << "\n" + << hf.message_type + << "\n" + // << "Information_Time: " << hf.Information_Time << "\n" + // << "Channel_Id: " << hf.Channel_Id << "\n" + // << "Channel_Seq: " << hf.Channel_Seq << "\n" + // << "Version_No: " << static_cast(hf.Version_No) << "\n" + << "Body_Length: " << hf.Body_Length << "\n" + << "Valid: " << (hf.valid ? "true" : "false"); + return os; } - } - MessageIdentifier( - const unsigned char transmissionCode, const unsigned char messageKind) - : transmissionCode(transmissionCode), messageKind(messageKind) { - set_identifier_description(transmissionCode, messageKind); - } - unsigned char transmissionCode; - unsigned char messageKind; - MessageID identifier; - std::string description; - friend std::ostream &operator<<( - std::ostream &os, const MessageIdentifier &MI) { - os << "TransmissionCode: " << MI.transmissionCode - << "\nMessageKind: " << MI.messageKind - << "\nDescription: " << MI.description << "\nidentifier: " - << MessageIdentifier::messageIDToString(MI.identifier); - return os; - } -}; + }; + struct I024Data { + struct trade { + enum Sign { POSITIVE = 0, NEGATIVE = 1 }; + Sign sign; + double match_price; // need to determine type(appendix 2) + float match_qty; // keeping str for simplicity -struct Header_Fields { - uint8_t Esc_Code; // 1 byte - MessageIdentifier message_type; - std::string Information_Time; - int Channel_Id; - std::string Channel_Seq; - uint8_t Version_No; - uint Body_Length; - bool valid = false; - Header_Fields(const std::vector &buffer, const size_t &offset) - : Esc_Code(buffer[offset]), - message_type(buffer[offset + 1], buffer[offset + 2]) { - // creates header feild struct - // and populates all fields - // Header_Fields header; - // Read single-byte fields - assert(message_type.identifier != MessageIdentifier::MessageID::I999); - // Read 2 bytes for Body_Length - unsigned char bl[2] = {buffer[offset + 17], buffer[offset + 18]}; - Body_Length = std::stoul(TaifexUtils::decodePackBCD(bl, 2)); - valid = true; - } + trade( + const std::vector &buffer, + const size_t &offset = 0, + const size_t match_qty_size = 2) { + const size_t SIGN_SIZE = 1; + const size_t MATCH_PRICE_SIZE = 5; + const size_t MATCH_QTY_SIZE = match_qty_size; + auto current_position = offset; + // Extract Sign + sign = (buffer[current_position] == '0') ? POSITIVE : NEGATIVE; + current_position += SIGN_SIZE; - friend std::ostream &operator<<(std::ostream &os, const Header_Fields &hf) { - os << "Esc_Code: " << static_cast(hf.Esc_Code) << "\n" - << hf.message_type - << "\n" - // << "Information_Time: " << hf.Information_Time << "\n" - // << "Channel_Id: " << hf.Channel_Id << "\n" - // << "Channel_Seq: " << hf.Channel_Seq << "\n" - // << "Version_No: " << static_cast(hf.Version_No) << "\n" - << "Body_Length: " << hf.Body_Length << "\n" - << "Valid: " << (hf.valid ? "true" : "false"); - return os; - } -}; -struct I024Data { - struct trade { - enum Sign { POSITIVE = 0, NEGATIVE = 1 }; - Sign sign; - double match_price; // need to determine type(appendix 2) - float match_qty; // keeping str for simplicity + match_price = std::stod(TaifexUtils::decodePackBCD( + buffer.data() + current_position, MATCH_PRICE_SIZE)); + current_position += MATCH_PRICE_SIZE; - trade( + match_qty = std::stof(TaifexUtils::extractBCD_STRING( + buffer, current_position, MATCH_QTY_SIZE)); + } + friend std::ostream &operator<<(std::ostream &os, const trade &data) { + os << "Sign: " + << (data.sign == Sign::POSITIVE ? "Positive" : "Negative") + << '\n' + << "Match Price: " << data.match_price << '\n' + << "Match Quantity: " << data.match_qty << '\n'; + return os; + } + }; + std::string prod_id; + unsigned long sequence_number; + bool calculated_flag; + std::string match_time; // make another struct for time? + char match_display_item; // need to discuss + std::vector trades; + // next fields to add + std::string match_total_qty; + std::string match_buy_count; + std::string match_sell_count; + I024Data( const std::vector &buffer, const size_t &offset = 0, - const size_t match_qty_size = 2) { - const size_t SIGN_SIZE = 1; - const size_t MATCH_PRICE_SIZE = 5; - const size_t MATCH_QTY_SIZE = match_qty_size; - auto current_position = offset; - // Extract Sign - sign = (buffer[current_position] == '0') ? POSITIVE : NEGATIVE; - current_position += SIGN_SIZE; + const size_t &length = -1) { + const size_t PROD_ID_SIZE = 20; + const size_t SEQ_NUMBER_SIZE = 5; + const size_t CALCULATED_FLAG = 1; + const size_t MATCH_TIME = 6; + const size_t MATCH_DISPLAY_ITEM_SIZE = 1; + const size_t TRADE_SIZE = 8; + const size_t MATCH_TOTAL_QTY_SIZE = 4; + const size_t MATCH_BUY_COUNT_SIZE = 4; + const size_t MATCH_SELL_COUNT_SIZE = 4; + const size_t FIRST_MATCH_QTY_SIZE = 4; + if (buffer.size() < offset + length) { + throw std::out_of_range( + "Buffer is smaller than the required length"); + } + // do we need this? + size_t current_position = offset; + prod_id = + TaifexUtils::extractString(buffer, current_position, PROD_ID_SIZE); - match_price = std::stod( - TaifexUtils::decodePackBCD(buffer.data() + current_position, MATCH_PRICE_SIZE)); - current_position += MATCH_PRICE_SIZE; + sequence_number = TaifexUtils::extractBCD_STOUL( + buffer, current_position, SEQ_NUMBER_SIZE); - match_qty = std::stof( - TaifexUtils::extractBCD_STRING(buffer, current_position, MATCH_QTY_SIZE)); + // Extract Calculated Flag + std::string calculated_flag_str = TaifexUtils::extractString( + buffer, current_position, CALCULATED_FLAG); + calculated_flag = (calculated_flag_str[0] == '1'); + + // Extract Match Time + match_time = TaifexUtils::time_decodePackBCD( + buffer.data() + current_position, MATCH_TIME); + //match_time = TaifexUtils::convertToISO(match_time); + //match_time = sanitizeISOString(match_time); + ; // extractBCD(buffer, current_position, MATCH_TIME); + current_position += MATCH_TIME; + trades.push_back( + trade(buffer, current_position, FIRST_MATCH_QTY_SIZE)); + current_position += TRADE_SIZE + 2; + // Extract Match Display Item + auto getFirstBitAndRemainingValue = + [](char input) -> std::tuple { + bool firstBit = (input & 0b10000000) != 0; + int remainingValue = input & 0b01111111; + return std::make_tuple(firstBit, remainingValue); + }; + match_display_item = buffer[current_position]; + current_position += MATCH_DISPLAY_ITEM_SIZE; + + auto [new_message, trades_count] = + getFirstBitAndRemainingValue(match_display_item); + // assert(new_message); + for (int i = 0; i < trades_count; i++) { + trades.push_back(trade(buffer, current_position)); + current_position += TRADE_SIZE; + } + // match quantity + match_total_qty = TaifexUtils::extractBCD_STRING( + buffer, current_position, MATCH_TOTAL_QTY_SIZE); + // match buy count + match_buy_count = TaifexUtils::extractBCD_STRING( + buffer, current_position, MATCH_BUY_COUNT_SIZE); + // match sell count + match_sell_count = TaifexUtils::extractBCD_STRING( + buffer, current_position, MATCH_SELL_COUNT_SIZE); + std::cout << "trades count: " << trades_count << std::endl; + assert( + buffer[current_position + 1] == 0x0D && + buffer[current_position + 2] == 0x0A); } - friend std::ostream &operator<<(std::ostream &os, const trade &data) { - os << "Sign: " - << (data.sign == Sign::POSITIVE ? "Positive" : "Negative") << '\n' - << "Match Price: " << data.match_price << '\n' - << "Match Quantity: " << data.match_qty << '\n'; + // 833 251 1142 + friend std::ostream &operator<<(std::ostream &os, const I024Data &data) { + os << "Product ID: " << data.prod_id << '\n' + << "Sequence Number: " << data.sequence_number << '\n' + << "Calculated Flag: " << (data.calculated_flag ? '1' : '0') << '\n' + << "Match Time: " << data.match_time << '\n' + << "Match Display Item: " + << std::bitset<8>{static_cast( + data.match_display_item)} + << "\n"; // Output as integer to show value + + for (auto t : data.trades) { + os << "Trade: " << t << "\n"; + } + + os << "Match Total Quantity: " << data.match_total_qty << '\n' + << "Match Buy Count: " << data.match_buy_count << '\n' + << "Match Sell Count: " << data.match_sell_count << '\n'; return os; } }; - std::string prod_id; - unsigned long sequence_number; - bool calculated_flag; - std::string match_time; // make another struct for time? - char match_display_item; // need to discuss - std::vector trades; - // next fields to add - std::string match_total_qty; - std::string match_buy_count; - std::string match_sell_count; - I024Data( - const std::vector &buffer, - const size_t &offset = 0, - const size_t &length = -1) { - const size_t PROD_ID_SIZE = 20; - const size_t SEQ_NUMBER_SIZE = 5; - const size_t CALCULATED_FLAG = 1; - const size_t MATCH_TIME = 6; - const size_t MATCH_DISPLAY_ITEM_SIZE = 1; - const size_t TRADE_SIZE = 8; - const size_t MATCH_TOTAL_QTY_SIZE = 4; - const size_t MATCH_BUY_COUNT_SIZE = 4; - const size_t MATCH_SELL_COUNT_SIZE = 4; - const size_t FIRST_MATCH_QTY_SIZE = 4; - if (buffer.size() < offset + length) { - throw std::out_of_range("Buffer is smaller than the required length"); - } - // do we need this? - size_t current_position = offset; - prod_id = TaifexUtils::extractString(buffer, current_position, PROD_ID_SIZE); - sequence_number = - TaifexUtils::extractBCD_STOUL(buffer, current_position, SEQ_NUMBER_SIZE); + struct I010Data { + std::string prod_id; // PROD-ID-S X(10) 10 Product ID + unsigned long + reference_price; // REFERENCE-PRICE 9(9) 5 Reference price PACK BCD + std::string prod_kind; // PROD-KIND X(1) 1 Type of contract + unsigned int decimal_locator; // DECIMAL-LOCATOR 9(1) 1 Number of decimal + // digits in price fields PACK BCD + unsigned int + strike_price_decimal_locator; // STRIKE-PRICE-DECIMAL-LOCATOR 9(1) 1 + // Number of decimal digits in strike price + // of options product PACK BCD + std::string + begin_date; // BEGIN-DATE 9(8) 4 Listed date of product (YYYYMMDD) + // PACK BCD + std::string + end_date; // END-DATE 9(8) 4 Expiry date of product (YYYYMMDD) PACK BCD + unsigned int flow_group; // FLOW-GROUP 9(2) 1 Flow group PACK BCD + std::string + delivery_date; // DELIVERY-DATE 9(8) 4 Delivery date of product PACK BCD + bool + dynamic_banding; // DYNAMIC-BANDING X(1) 1 Is dynamic banding applicable? + // Y: Yes, N: No - // Extract Calculated Flag - std::string calculated_flag_str = - TaifexUtils::extractString(buffer, current_position, CALCULATED_FLAG); - calculated_flag = (calculated_flag_str[0] == '1'); + // Embedded ProductInfo struct + struct ProductInfo { + enum class ProductType { FUTURES, OPTIONS } productType; - // Extract Match Time - match_time = TaifexUtils::time_decodePackBCD(buffer.data() + current_position, MATCH_TIME); - //match_time = TaifexUtils::convertToISO(match_time); - //match_time = sanitizeISOString(match_time); - ; // extractBCD(buffer, current_position, MATCH_TIME); - current_position += MATCH_TIME; - trades.push_back(trade(buffer, current_position, FIRST_MATCH_QTY_SIZE)); - current_position += TRADE_SIZE + 2; - // Extract Match Display Item - auto getFirstBitAndRemainingValue = - [](char input) -> std::tuple { - bool firstBit = (input & 0b10000000) != 0; - int remainingValue = input & 0b01111111; - return std::make_tuple(firstBit, remainingValue); - }; - match_display_item = buffer[current_position]; - current_position += MATCH_DISPLAY_ITEM_SIZE; + std::string productID; + int expirationYear; + int expirationMonth; + bool isCall; + double strikePrice; + bool isWeekly; + bool isSpread; + std::string spreadSecondLeg; - auto [new_message, trades_count] = - getFirstBitAndRemainingValue(match_display_item); - // assert(new_message); - for (int i = 0; i < trades_count; i++) { - trades.push_back(trade(buffer, current_position)); - current_position += TRADE_SIZE; - } - // match quantity - match_total_qty = - TaifexUtils::extractBCD_STRING(buffer, current_position, MATCH_TOTAL_QTY_SIZE); - // match buy count - match_buy_count = - TaifexUtils::extractBCD_STRING(buffer, current_position, MATCH_BUY_COUNT_SIZE); - // match sell count - match_sell_count = - TaifexUtils::extractBCD_STRING(buffer, current_position, MATCH_SELL_COUNT_SIZE); - std::cout << "trades count: " << trades_count << std::endl; - assert( - buffer[current_position + 1] == 0x0D && - buffer[current_position + 2] == 0x0A); - } - // 833 251 1142 - friend std::ostream &operator<<(std::ostream &os, const I024Data &data) { - os << "Product ID: " << data.prod_id << '\n' - << "Sequence Number: " << data.sequence_number << '\n' - << "Calculated Flag: " << (data.calculated_flag ? '1' : '0') << '\n' - << "Match Time: " << data.match_time << '\n' - << "Match Display Item: " - << std::bitset<8>{static_cast( - data.match_display_item)} - << "\n"; // Output as integer to show value + // Convert month code to month number + static int getMonthFromCode(char code) { + if (code >= 'A' && code <= 'L') + return code - 'A' + + 1; // 'A' corresponds to 1 (January), 'B' to 2, ..., 'L' to 12 (December) + throw std::runtime_error("Invalid month code"); + } - for (auto t : data.trades) { - os << "Trade: " << t << "\n"; - } + // Method to parse Product ID and initialize ProductInfo + static ProductInfo parseProductID(const std::string &prodID) { + ProductInfo info; - os << "Match Total Quantity: " << data.match_total_qty << '\n' - << "Match Buy Count: " << data.match_buy_count << '\n' - << "Match Sell Count: " << data.match_sell_count << '\n'; - return os; - } -}; - -struct I010Data { - std::string prod_id; // PROD-ID-S X(10) 10 Product ID - unsigned long - reference_price; // REFERENCE-PRICE 9(9) 5 Reference price PACK BCD - std::string prod_kind; // PROD-KIND X(1) 1 Type of contract - unsigned int decimal_locator; // DECIMAL-LOCATOR 9(1) 1 Number of decimal - // digits in price fields PACK BCD - unsigned int - strike_price_decimal_locator; // STRIKE-PRICE-DECIMAL-LOCATOR 9(1) 1 - // Number of decimal digits in strike price - // of options product PACK BCD - std::string - begin_date; // BEGIN-DATE 9(8) 4 Listed date of product (YYYYMMDD) - // PACK BCD - std::string - end_date; // END-DATE 9(8) 4 Expiry date of product (YYYYMMDD) PACK BCD - unsigned int flow_group; // FLOW-GROUP 9(2) 1 Flow group PACK BCD - std::string - delivery_date; // DELIVERY-DATE 9(8) 4 Delivery date of product PACK BCD - bool - dynamic_banding; // DYNAMIC-BANDING X(1) 1 Is dynamic banding applicable? - // Y: Yes, N: No - - // Embedded ProductInfo struct - struct ProductInfo { - enum class ProductType { FUTURES, OPTIONS } productType; - - std::string productID; - int expirationYear; - int expirationMonth; - bool isCall; - double strikePrice; - bool isWeekly; - bool isSpread; - std::string spreadSecondLeg; - - // Convert month code to month number - static int getMonthFromCode(char code) { - if (code >= 'A' && code <= 'L') - return code - 'A' + - 1; // 'A' corresponds to 1 (January), 'B' to 2, ..., 'L' to 12 (December) - throw std::runtime_error("Invalid month code"); - } - - // Method to parse Product ID and initialize ProductInfo - static ProductInfo parseProductID(const std::string &prodID) { - ProductInfo info; - - if (prodID.length() == 10 || prodID.length() == 20) { - // Futures product parsing - info.productType = ProductType::FUTURES; - info.productID = prodID.substr(0, 3); - info.expirationMonth = getMonthFromCode(prodID[3]); - info.expirationYear = - 2020 + - (prodID[4] - '0'); // Assuming the year is between 2020 and 2029 - info.isWeekly = - prodID[2] >= '1' && prodID[2] <= '5'; // Weekly expired contract - info.isSpread = prodID.length() > 10; // Check if it's a spread - if (info.isSpread) { - info.spreadSecondLeg = - prodID.substr(11, 6); // Parsing the second leg of the spread + if (prodID.length() == 10 || prodID.length() == 20) { + // Futures product parsing + info.productType = ProductType::FUTURES; + info.productID = prodID.substr(0, 3); + info.expirationMonth = getMonthFromCode(prodID[3]); + info.expirationYear = + 2020 + (prodID[4] - + '0'); // Assuming the year is between 2020 and 2029 + info.isWeekly = prodID[2] >= '1' && + prodID[2] <= '5'; // Weekly expired contract + info.isSpread = prodID.length() > 10; // Check if it's a spread + if (info.isSpread) { + info.spreadSecondLeg = prodID.substr( + 11, 6); // Parsing the second leg of the spread + } + } else if (prodID.length() == 11) { + // Weekly time spread product parsing + info.productType = ProductType::FUTURES; + info.isWeekly = true; + info.isSpread = true; + info.productID = prodID.substr(0, 3); + info.expirationMonth = getMonthFromCode(prodID[3]); + info.expirationYear = 2020 + (prodID[4] - '0'); + info.spreadSecondLeg = prodID.substr(6, 5); + } else if (prodID.length() == 12) { + // Options product parsing + info.productType = ProductType::OPTIONS; + info.productID = prodID.substr(0, 3); + info.strikePrice = std::stod( + prodID.substr(3, 5)); // Assuming no decimal point for now + info.isCall = + prodID[8] >= 'A' && prodID[8] <= 'L'; // Call options + info.expirationMonth = getMonthFromCode(prodID[8]); + info.expirationYear = 2020 + (prodID[9] - '0'); + } else { + throw std::invalid_argument("Invalid Product ID length"); } - } else if (prodID.length() == 11) { - // Weekly time spread product parsing - info.productType = ProductType::FUTURES; - info.isWeekly = true; - info.isSpread = true; - info.productID = prodID.substr(0, 3); - info.expirationMonth = getMonthFromCode(prodID[3]); - info.expirationYear = 2020 + (prodID[4] - '0'); - info.spreadSecondLeg = prodID.substr(6, 5); - } else if (prodID.length() == 12) { - // Options product parsing - info.productType = ProductType::OPTIONS; - info.productID = prodID.substr(0, 3); - info.strikePrice = std::stod( - prodID.substr(3, 5)); // Assuming no decimal point for now - info.isCall = prodID[8] >= 'A' && prodID[8] <= 'L'; // Call options - info.expirationMonth = getMonthFromCode(prodID[8]); - info.expirationYear = 2020 + (prodID[9] - '0'); - } else { - throw std::invalid_argument("Invalid Product ID length"); + + return info; } - return info; + // Overload the << operator to print ProductInfo + friend std::ostream &operator<<( + std::ostream &os, const ProductInfo &info) { + os << "Product Type: " + << (info.productType == ProductType::FUTURES ? "Futures" + : "Options") + << '\n' + << "Product ID: " << info.productID << '\n' + << "Expiration Month: " << info.expirationMonth << " (" + << (info.expirationMonth == 1 ? "January" + : info.expirationMonth == 2 ? "February" + : info.expirationMonth == 3 ? "March" + : info.expirationMonth == 4 ? "April" + : info.expirationMonth == 5 ? "May" + : info.expirationMonth == 6 ? "June" + : info.expirationMonth == 7 ? "July" + : info.expirationMonth == 8 ? "August" + : info.expirationMonth == 9 ? "September" + : info.expirationMonth == 10 ? "October" + : info.expirationMonth == 11 ? "November" + : info.expirationMonth == 12 ? "December" + : "Unknown") + << ")\n" + << "Expiration Year: " << info.expirationYear << '\n' + << "Strike Price: " << info.strikePrice << '\n' + << "Is Weekly: " << (info.isWeekly ? "Yes" : "No") << '\n' + << "Is Call: " << (info.isCall ? "Yes" : "No") << '\n' + << "Is Spread: " << (info.isSpread ? "Yes" : "No") << '\n'; + if (info.isSpread) { + os << "Spread Second Leg: " << info.spreadSecondLeg << '\n'; + } + return os; + } + } parsedProductInfo; + // Constructor to initialize from a buffer + I010Data( + const std::vector &buffer, + size_t offset = 0, + size_t length = -1) { + const size_t PROD_ID_SIZE = 10; + const size_t REFERENCE_PRICE_SIZE = 5; + const size_t PROD_KIND_SIZE = 1; + const size_t DECIMAL_LOCATOR_SIZE = 1; + const size_t STRIKE_PRICE_DECIMAL_LOCATOR_SIZE = 1; + const size_t BEGIN_DATE_SIZE = 4; + const size_t END_DATE_SIZE = 4; + const size_t FLOW_GROUP_SIZE = 1; + const size_t DELIVERY_DATE_SIZE = 4; + // const size_t DYNAMIC_BANDING_SIZE = 1; + + // Parse ProductInfo using the extracted product ID + // parsedProductInfo = ProductInfo::parseProductID(prod_id, strike_price_decimal_locator); + + if (buffer.size() < offset + length) { + throw std::out_of_range( + "Buffer is smaller than the required length"); + } + + size_t current_position = offset; + + prod_id = + TaifexUtils::extractString(buffer, current_position, PROD_ID_SIZE); + reference_price = TaifexUtils::extractBCD_STOUL( + buffer, current_position, REFERENCE_PRICE_SIZE); + prod_kind = TaifexUtils::extractString( + buffer, current_position, PROD_KIND_SIZE); + decimal_locator = TaifexUtils::extractBCD_STOUL( + buffer, current_position, DECIMAL_LOCATOR_SIZE); + strike_price_decimal_locator = TaifexUtils::extractBCD_STOUL( + buffer, current_position, STRIKE_PRICE_DECIMAL_LOCATOR_SIZE); + begin_date = TaifexUtils::extractBCDDate( + buffer, current_position, BEGIN_DATE_SIZE); + end_date = TaifexUtils::extractBCDDate( + buffer, current_position, END_DATE_SIZE); + flow_group = TaifexUtils::extractBCD_STOUL( + buffer, current_position, FLOW_GROUP_SIZE); + delivery_date = TaifexUtils::extractBCDDate( + buffer, current_position, DELIVERY_DATE_SIZE); + dynamic_banding = TaifexUtils::extractBool(buffer, current_position); + + parsedProductInfo = ProductInfo::parseProductID(prod_id); } - // Overload the << operator to print ProductInfo - friend std::ostream &operator<<( - std::ostream &os, const ProductInfo &info) { - os << "Product Type: " - << (info.productType == ProductType::FUTURES ? "Futures" - : "Options") + // Overload the << operator for ProductInfo to print its contents + + // Friend function to overload the << operator for outputting the struct data + friend std::ostream &operator<<(std::ostream &os, const I010Data &data) { + os << "Product ID: " << data.prod_id << '\n' + << "Reference Price: " << data.reference_price << '\n' + << "Product Kind: " << data.prod_kind << '\n' + << "Decimal Locator: " << data.decimal_locator << '\n' + << "Strike Price Decimal Locator: " + << data.strike_price_decimal_locator << '\n' + << "Begin Date: " << data.begin_date << '\n' + << "End Date: " << data.end_date << '\n' + << "Flow Group: " << data.flow_group << '\n' + << "Delivery Date: " << data.delivery_date << '\n' + << "Dynamic Banding: " << (data.dynamic_banding ? 'Y' : 'N') + << "\nParsed Product Info:\n" + << data.parsedProductInfo; + return os; + } + // Overload the << operator for ProductInfo to print its contents + // friend std::ostream &operator<<(std::ostream &os, const ProductInfo &info) { + // os << "Product Type: " << info.productType << '\n' + // << "Product ID: " << info.productID << '\n' + // << "Contract Type: " << info.contractType << '\n' + // << "Expiration Year: " << info.expirationYear << '\n' + // << "Expiration Month: " << info.expirationMonth << '\n' + // << "Is Call: " << (info.isCall ? "Yes" : "No") << '\n' + // << "Strike Price: " << info.strikePrice << '\n' + // << "Is Weekly: " << (info.isWeekly ? "Yes" : "No") << '\n' + // << "Is Spread: " << (info.isSpread ? "Yes" : "No") << '\n' + // << "Spread Second Leg: " << info.spreadSecondLeg; + // return os; + // } + }; + + struct I011Data { + std::string kind_id; // KIND-ID X(4) 4 Contract ID + std::string + chinese_name; // NAME X(30) 30 Contract name contract's Chinese name + std::string + stock_id; // STOCK-ID X(6) 6 Underlying stock ID Only for stock type + unsigned long + contract_size; // CONTRACT-SIZE 9(7)V9(4) 6 Contract size PACK BCD + unsigned int decimal_locator; // DECIMAL-LOCATOR 9(1) 1 Decimals of strike + // price PACK BCD + unsigned int + strike_price_decimal_locator; // STRIKE-PRICE-DECIMAL-LOCATOR 9(1) 1 + // Decimals of strike price PACK BCD + std::string + begin_date; // BEGIN-DATE X(8) 8 List date Format: YYYYMMDD Only + // for stock type + + enum CONTRACT_TYPE { // Contract types + INDEX = 1, + INTEREST_RATE, // 2 + BOND, // 3 + COMMODITY, // 4 + STOCK, // 5 + CURRENCY // 6 + } contract_type; + + enum CURRENCY_TYPE { // Currency types + TWD = 1, // Taiwan Dollar + USD, // US Dollar + EUR, // Euro + JPY, // Japanese Yen + GBP, // British Pound + AUD, // Australian Dollar + HKD, // Hong Kong Dollar + CNY // Chinese Yuan + } currency_enum_type; + + enum STATUS_CODE { // Status codes + NORMAL = 'N', + PAUSE = 'P', + UNLISTED = 'U' + } status_code; + + enum EXPIRY_TYPE { // Expiry types + STANDARD = 'S', + WEEKLY = 'W' + } expiry_type; + + enum UNDERLYING_TYPE { // Underlying types + ETF = 'E', + SINGLE_STOCK = 'S' + } underlying_type; + + bool + accept_quote_flag; // ACCEPT-QUOTE-FLAG X(1) 1 Quote acceptable? true: + // Yes, false: No + bool block_trade_flag; // BLOCK-TRADE-FLAG X(1) 1 Can block trade? true: + // Yes, false: No + + // Constructor to initialize from a buffer + I011Data( + const std::vector &buffer, + size_t offset = 0, + size_t length = -1) { + const size_t KIND_ID_SIZE = 4; + const size_t CHINESE_NAME_SIZE = 30; + const size_t STOCK_ID_SIZE = 6; + const size_t CONTRACT_SIZE_SIZE = 6; + const size_t DECIMAL_LOCATOR_SIZE = 1; + const size_t STRIKE_PRICE_DECIMAL_LOCATOR_SIZE = 1; + const size_t BEGIN_DATE_SIZE = 8; + + if (buffer.size() < offset + length) { + throw std::out_of_range( + "Buffer is smaller than the required length"); + } + + size_t current_position = offset; + + kind_id = + TaifexUtils::extractString(buffer, current_position, KIND_ID_SIZE); + chinese_name = TaifexUtils::extractString( + buffer, current_position, CHINESE_NAME_SIZE); + stock_id = TaifexUtils::extractString( + buffer, current_position, STOCK_ID_SIZE); + contract_size = TaifexUtils::extractBCD_STOUL( + buffer, current_position, CONTRACT_SIZE_SIZE); + status_code = static_cast(buffer[current_position++]); + currency_enum_type = + static_cast(buffer[current_position++]); + decimal_locator = TaifexUtils::extractBCD_STOUL( + buffer, current_position, DECIMAL_LOCATOR_SIZE); + strike_price_decimal_locator = TaifexUtils::extractBCD_STOUL( + buffer, current_position, STRIKE_PRICE_DECIMAL_LOCATOR_SIZE); + accept_quote_flag = TaifexUtils::extractBool(buffer, current_position); + begin_date = TaifexUtils::extractString( + buffer, current_position, BEGIN_DATE_SIZE); + block_trade_flag = TaifexUtils::extractBool(buffer, current_position); + expiry_type = static_cast(buffer[current_position++]); + underlying_type = + static_cast(buffer[current_position++]); + } + + std::string getCurrencyString() const { + switch (currency_enum_type) { + case TWD: + return "Taiwan Dollar"; + case USD: + return "US Dollar"; + case EUR: + return "Euro"; + case JPY: + return "Japanese Yen"; + case GBP: + return "British Pound"; + case AUD: + return "Australian Dollar"; + case HKD: + return "Hong Kong Dollar"; + case CNY: + return "Chinese Yuan"; + default: + return "Unknown Currency"; + } + } + // Friend function to overload the << operator for outputting the struct + // data + friend std::ostream &operator<<(std::ostream &os, const I011Data &data) { + os << "Kind ID: " << data.kind_id << '\n' + << "Chinese Name: " << data.chinese_name << '\n' + << "Stock ID: " << data.stock_id << '\n' + << "Contract Size: " << data.contract_size << '\n' + << "Status Code: " << static_cast(data.status_code) << '\n' + << "Currency Type: " << static_cast(data.currency_enum_type) << '\n' - << "Product ID: " << info.productID << '\n' - << "Expiration Month: " << info.expirationMonth << " (" - << (info.expirationMonth == 1 ? "January" - : info.expirationMonth == 2 ? "February" - : info.expirationMonth == 3 ? "March" - : info.expirationMonth == 4 ? "April" - : info.expirationMonth == 5 ? "May" - : info.expirationMonth == 6 ? "June" - : info.expirationMonth == 7 ? "July" - : info.expirationMonth == 8 ? "August" - : info.expirationMonth == 9 ? "September" - : info.expirationMonth == 10 ? "October" - : info.expirationMonth == 11 ? "November" - : info.expirationMonth == 12 ? "December" - : "Unknown") - << ")\n" - << "Expiration Year: " << info.expirationYear << '\n' - << "Strike Price: " << info.strikePrice << '\n' - << "Is Weekly: " << (info.isWeekly ? "Yes" : "No") << '\n' - << "Is Call: " << (info.isCall ? "Yes" : "No") << '\n' - << "Is Spread: " << (info.isSpread ? "Yes" : "No") << '\n'; - if (info.isSpread) { - os << "Spread Second Leg: " << info.spreadSecondLeg << '\n'; + << "Decimal Locator: " << data.decimal_locator << '\n' + << "Strike Price Decimal Locator: " + << data.strike_price_decimal_locator << '\n' + << "Accept Quote Flag: " << (data.accept_quote_flag ? 'Y' : 'N') + << '\n' + << "Begin Date: " << data.begin_date << '\n' + << "Block Trade Flag: " << (data.block_trade_flag ? 'Y' : 'N') + << '\n' + << "Expiry Type: " << static_cast(data.expiry_type) << '\n' + << "Underlying Type: " << static_cast(data.underlying_type); + return os; + } + }; + + struct I081Data { + struct MD_ENTRY { + enum Sign { POSITIVE = 0, NEGATIVE = 1 }; + int md_update_action; + char md_entry_type; + Sign sign; + double price; + size_t level; + size_t quantity; + // operator overload for MDentry + friend std::ostream &operator<<( + std::ostream &os, const I081Data::MD_ENTRY &entry) { + os << "MD Update Action: " << entry.md_update_action << "\n" + << "MD Entry Type: " << entry.md_entry_type << "\n" + << "Sign: " + << (entry.sign == I081Data::MD_ENTRY::POSITIVE ? "Positive" + : "Negative") + << "\n" + << "Price: " << entry.price << "\n" + << "Level: " << entry.level << "\n" + << "Quantity: " << entry.quantity << "\n"; + return os; } - return os; - } - } parsedProductInfo; - // Constructor to initialize from a buffer - I010Data( - const std::vector &buffer, - size_t offset = 0, - size_t length = -1) { - const size_t PROD_ID_SIZE = 10; - const size_t REFERENCE_PRICE_SIZE = 5; - const size_t PROD_KIND_SIZE = 1; - const size_t DECIMAL_LOCATOR_SIZE = 1; - const size_t STRIKE_PRICE_DECIMAL_LOCATOR_SIZE = 1; - const size_t BEGIN_DATE_SIZE = 4; - const size_t END_DATE_SIZE = 4; - const size_t FLOW_GROUP_SIZE = 1; - const size_t DELIVERY_DATE_SIZE = 4; - // const size_t DYNAMIC_BANDING_SIZE = 1; + MD_ENTRY(const std::vector &buffer, size_t offset = 0) { + md_update_action = buffer[offset]; + md_entry_type = buffer[offset + 1]; + sign = static_cast(buffer[offset + 2]); + // price + std::string price_str = + TaifexUtils::decodePackBCD(&buffer[offset + 3], 5); + price = std::stod(price_str); - // Parse ProductInfo using the extracted product ID - // parsedProductInfo = ProductInfo::parseProductID(prod_id, strike_price_decimal_locator); + // quantity + std::string size_str = + TaifexUtils::decodePackBCD(&buffer[offset + 8], 4); + quantity = std::stoul(size_str); - if (buffer.size() < offset + length) { - throw std::out_of_range("Buffer is smaller than the required length"); - } - - size_t current_position = offset; - - prod_id = TaifexUtils::extractString(buffer, current_position, PROD_ID_SIZE); - reference_price = - TaifexUtils::extractBCD_STOUL(buffer, current_position, REFERENCE_PRICE_SIZE); - prod_kind = TaifexUtils::extractString(buffer, current_position, PROD_KIND_SIZE); - decimal_locator = - TaifexUtils::extractBCD_STOUL(buffer, current_position, DECIMAL_LOCATOR_SIZE); - strike_price_decimal_locator = TaifexUtils::extractBCD_STOUL( - buffer, current_position, STRIKE_PRICE_DECIMAL_LOCATOR_SIZE); - begin_date = TaifexUtils::extractBCDDate(buffer, current_position, BEGIN_DATE_SIZE); - end_date = TaifexUtils::extractBCDDate(buffer, current_position, END_DATE_SIZE); - flow_group = TaifexUtils::extractBCD_STOUL(buffer, current_position, FLOW_GROUP_SIZE); - delivery_date = - TaifexUtils::extractBCDDate(buffer, current_position, DELIVERY_DATE_SIZE); - dynamic_banding = TaifexUtils::extractBool(buffer, current_position); - - parsedProductInfo = ProductInfo::parseProductID(prod_id); - } - - // Overload the << operator for ProductInfo to print its contents - - // Friend function to overload the << operator for outputting the struct data - friend std::ostream &operator<<(std::ostream &os, const I010Data &data) { - os << "Product ID: " << data.prod_id << '\n' - << "Reference Price: " << data.reference_price << '\n' - << "Product Kind: " << data.prod_kind << '\n' - << "Decimal Locator: " << data.decimal_locator << '\n' - << "Strike Price Decimal Locator: " - << data.strike_price_decimal_locator << '\n' - << "Begin Date: " << data.begin_date << '\n' - << "End Date: " << data.end_date << '\n' - << "Flow Group: " << data.flow_group << '\n' - << "Delivery Date: " << data.delivery_date << '\n' - << "Dynamic Banding: " << (data.dynamic_banding ? 'Y' : 'N') - << "\nParsed Product Info:\n" - << data.parsedProductInfo; - return os; - } - // Overload the << operator for ProductInfo to print its contents - // friend std::ostream &operator<<(std::ostream &os, const ProductInfo &info) { - // os << "Product Type: " << info.productType << '\n' - // << "Product ID: " << info.productID << '\n' - // << "Contract Type: " << info.contractType << '\n' - // << "Expiration Year: " << info.expirationYear << '\n' - // << "Expiration Month: " << info.expirationMonth << '\n' - // << "Is Call: " << (info.isCall ? "Yes" : "No") << '\n' - // << "Strike Price: " << info.strikePrice << '\n' - // << "Is Weekly: " << (info.isWeekly ? "Yes" : "No") << '\n' - // << "Is Spread: " << (info.isSpread ? "Yes" : "No") << '\n' - // << "Spread Second Leg: " << info.spreadSecondLeg; - // return os; - // } -}; - -struct I011Data { - std::string kind_id; // KIND-ID X(4) 4 Contract ID - std::string - chinese_name; // NAME X(30) 30 Contract name contract's Chinese name - std::string - stock_id; // STOCK-ID X(6) 6 Underlying stock ID Only for stock type - unsigned long - contract_size; // CONTRACT-SIZE 9(7)V9(4) 6 Contract size PACK BCD - unsigned int decimal_locator; // DECIMAL-LOCATOR 9(1) 1 Decimals of strike - // price PACK BCD - unsigned int - strike_price_decimal_locator; // STRIKE-PRICE-DECIMAL-LOCATOR 9(1) 1 - // Decimals of strike price PACK BCD - std::string begin_date; // BEGIN-DATE X(8) 8 List date Format: YYYYMMDD Only - // for stock type - - enum CONTRACT_TYPE { // Contract types - INDEX = 1, - INTEREST_RATE, // 2 - BOND, // 3 - COMMODITY, // 4 - STOCK, // 5 - CURRENCY // 6 - } contract_type; - - enum CURRENCY_TYPE { // Currency types - TWD = 1, // Taiwan Dollar - USD, // US Dollar - EUR, // Euro - JPY, // Japanese Yen - GBP, // British Pound - AUD, // Australian Dollar - HKD, // Hong Kong Dollar - CNY // Chinese Yuan - } currency_enum_type; - - enum STATUS_CODE { // Status codes - NORMAL = 'N', - PAUSE = 'P', - UNLISTED = 'U' - } status_code; - - enum EXPIRY_TYPE { // Expiry types - STANDARD = 'S', - WEEKLY = 'W' - } expiry_type; - - enum UNDERLYING_TYPE { // Underlying types - ETF = 'E', - SINGLE_STOCK = 'S' - } underlying_type; - - bool accept_quote_flag; // ACCEPT-QUOTE-FLAG X(1) 1 Quote acceptable? true: - // Yes, false: No - bool block_trade_flag; // BLOCK-TRADE-FLAG X(1) 1 Can block trade? true: - // Yes, false: No - - // Constructor to initialize from a buffer - I011Data( - const std::vector &buffer, - size_t offset = 0, - size_t length = -1) { - const size_t KIND_ID_SIZE = 4; - const size_t CHINESE_NAME_SIZE = 30; - const size_t STOCK_ID_SIZE = 6; - const size_t CONTRACT_SIZE_SIZE = 6; - const size_t DECIMAL_LOCATOR_SIZE = 1; - const size_t STRIKE_PRICE_DECIMAL_LOCATOR_SIZE = 1; - const size_t BEGIN_DATE_SIZE = 8; - - if (buffer.size() < offset + length) { - throw std::out_of_range("Buffer is smaller than the required length"); - } - - size_t current_position = offset; - - kind_id = TaifexUtils::extractString(buffer, current_position, KIND_ID_SIZE); - chinese_name = TaifexUtils::extractString(buffer, current_position, CHINESE_NAME_SIZE); - stock_id = TaifexUtils::extractString(buffer, current_position, STOCK_ID_SIZE); - contract_size = - TaifexUtils::extractBCD_STOUL(buffer, current_position, CONTRACT_SIZE_SIZE); - status_code = static_cast(buffer[current_position++]); - currency_enum_type = - static_cast(buffer[current_position++]); - decimal_locator = - TaifexUtils::extractBCD_STOUL(buffer, current_position, DECIMAL_LOCATOR_SIZE); - strike_price_decimal_locator = TaifexUtils::extractBCD_STOUL( - buffer, current_position, STRIKE_PRICE_DECIMAL_LOCATOR_SIZE); - accept_quote_flag = TaifexUtils::extractBool(buffer, current_position); - begin_date = TaifexUtils::extractString(buffer, current_position, BEGIN_DATE_SIZE); - block_trade_flag = TaifexUtils::extractBool(buffer, current_position); - expiry_type = static_cast(buffer[current_position++]); - underlying_type = - static_cast(buffer[current_position++]); - } - - std::string getCurrencyString() const { - switch (currency_enum_type) { - case TWD: - return "Taiwan Dollar"; - case USD: - return "US Dollar"; - case EUR: - return "Euro"; - case JPY: - return "Japanese Yen"; - case GBP: - return "British Pound"; - case AUD: - return "Australian Dollar"; - case HKD: - return "Hong Kong Dollar"; - case CNY: - return "Chinese Yuan"; - default: - return "Unknown Currency"; - } - } - // Friend function to overload the << operator for outputting the struct - // data - friend std::ostream &operator<<(std::ostream &os, const I011Data &data) { - os << "Kind ID: " << data.kind_id << '\n' - << "Chinese Name: " << data.chinese_name << '\n' - << "Stock ID: " << data.stock_id << '\n' - << "Contract Size: " << data.contract_size << '\n' - << "Status Code: " << static_cast(data.status_code) << '\n' - << "Currency Type: " << static_cast(data.currency_enum_type) - << '\n' - << "Decimal Locator: " << data.decimal_locator << '\n' - << "Strike Price Decimal Locator: " - << data.strike_price_decimal_locator << '\n' - << "Accept Quote Flag: " << (data.accept_quote_flag ? 'Y' : 'N') - << '\n' - << "Begin Date: " << data.begin_date << '\n' - << "Block Trade Flag: " << (data.block_trade_flag ? 'Y' : 'N') << '\n' - << "Expiry Type: " << static_cast(data.expiry_type) << '\n' - << "Underlying Type: " << static_cast(data.underlying_type); - return os; - } -}; - -struct I081Data { - struct MD_ENTRY { - enum Sign { POSITIVE = 0, NEGATIVE = 1 }; - int md_update_action; - char md_entry_type; - Sign sign; - double price; - size_t level; - size_t quantity; - // operator overload for MDentry - friend std::ostream &operator<<( - std::ostream &os, const I081Data::MD_ENTRY &entry) { - os << "MD Update Action: " << entry.md_update_action << "\n" - << "MD Entry Type: " << entry.md_entry_type << "\n" - << "Sign: " - << (entry.sign == I081Data::MD_ENTRY::POSITIVE ? "Positive" - : "Negative") - << "\n" - << "Price: " << entry.price << "\n" - << "Level: " << entry.level << "\n" - << "Quantity: " << entry.quantity << "\n"; - return os; - } - MD_ENTRY(const std::vector &buffer, size_t offset = 0) { - md_update_action = buffer[offset]; - md_entry_type = buffer[offset + 1]; - sign = static_cast(buffer[offset + 2]); - // price - std::string price_str = TaifexUtils::decodePackBCD(&buffer[offset + 3], 5); - price = std::stod(price_str); - - // quantity - std::string size_str = TaifexUtils::decodePackBCD(&buffer[offset + 8], 4); - quantity = std::stoul(size_str); - - // level - std::string level_str = TaifexUtils::decodePackBCD(&buffer[offset + 12], 1); - level = std::stoul(level_str); - } - }; - std::vector entries; - std::string prod_id; - size_t entries_count; - unsigned long seq_number; - // overload that prints dummy values - friend std::ostream &operator<<(std::ostream &os, const I081Data &data) { - os << "Product ID: " << data.prod_id << "\n" - << "Sequence Number: " << data.seq_number << "\n" - << "Entries Count: " << data.entries_count << "\n" - << "MD entries: "; - for (auto entry : data.entries) { - std::cout << " Entry: " << entry; - } - - return os; - } - I081Data( - const std::vector &buffer, const size_t &offset = 0) { - const size_t ENTRIES_COUNT_SIZE = 1; - const size_t PROD_ID_SIZE = 20; - const size_t SEQ_NUMBER_SIZE = 5; - - auto current_position = buffer.begin() + offset; - - prod_id.assign(current_position, current_position + PROD_ID_SIZE); - current_position += PROD_ID_SIZE; - - std::string seq_number_str = - TaifexUtils::decodePackBCD(&(*current_position), SEQ_NUMBER_SIZE); - seq_number = std::stoul(seq_number_str); - current_position += SEQ_NUMBER_SIZE; - - std::string entries_count_string = - TaifexUtils::decodePackBCD(&(*current_position), ENTRIES_COUNT_SIZE); - entries_count = std::stoul(entries_count_string); - current_position += ENTRIES_COUNT_SIZE; - // Following fields group repeat NO-MD-ENTRIES times - auto position = PROD_ID_SIZE + SEQ_NUMBER_SIZE + ENTRIES_COUNT_SIZE; - for (size_t i = 0; i < entries_count; ++i) { - entries.push_back(MD_ENTRY(buffer, offset + position)); - position += 13; - // current_position += ENTRIES_COUNT_SIZE;//need to fix - } - } -}; - -struct I083Data { - struct MD_ENTRY { - char md_entry_type; - enum Sign { POSITIVE = 0, NEGATIVE = 1 }; - Sign sign; - double price; - size_t quantity; - size_t level; - - friend std::ostream &operator<<( - std::ostream &os, const I083Data::MD_ENTRY &entry) { - os << "\nMD Entry Type: " << entry.md_entry_type << "\n" - << "Sign: " - << (entry.sign == I083Data::MD_ENTRY::POSITIVE ? "Positive" - : "Negative") - << "\n" - << "Price: " << entry.price << "\n" - << "Quantity: " << entry.quantity << "\n" - << "Level: " << entry.level << "\n"; - return os; - } - - MD_ENTRY(const std::vector &buffer, size_t offset = 0) { - md_entry_type = buffer[offset]; - sign = static_cast(buffer[offset + 1]); - - // price - std::string price_str = TaifexUtils::decodePackBCD(&buffer[offset + 2], 5); - price = std::stod(price_str); - - // quantity - std::string size_str = TaifexUtils::decodePackBCD(&buffer[offset + 7], 4); - quantity = std::stoul(size_str); - - // level - std::string level_str = TaifexUtils::decodePackBCD(&buffer[offset + 11], 1); - level = std::stoul(level_str); + // level + std::string level_str = + TaifexUtils::decodePackBCD(&buffer[offset + 12], 1); + level = std::stoul(level_str); + } }; + std::vector entries; + std::string prod_id; + size_t entries_count; + unsigned long seq_number; + // overload that prints dummy values + friend std::ostream &operator<<(std::ostream &os, const I081Data &data) { + os << "Product ID: " << data.prod_id << "\n" + << "Sequence Number: " << data.seq_number << "\n" + << "Entries Count: " << data.entries_count << "\n" + << "MD entries: "; + for (auto entry : data.entries) { + std::cout << " Entry: " << entry; + } + + return os; + } + I081Data( + const std::vector &buffer, const size_t &offset = 0) { + const size_t ENTRIES_COUNT_SIZE = 1; + const size_t PROD_ID_SIZE = 20; + const size_t SEQ_NUMBER_SIZE = 5; + + auto current_position = buffer.begin() + offset; + + prod_id.assign(current_position, current_position + PROD_ID_SIZE); + current_position += PROD_ID_SIZE; + + std::string seq_number_str = + TaifexUtils::decodePackBCD(&(*current_position), SEQ_NUMBER_SIZE); + seq_number = std::stoul(seq_number_str); + current_position += SEQ_NUMBER_SIZE; + + std::string entries_count_string = TaifexUtils::decodePackBCD( + &(*current_position), ENTRIES_COUNT_SIZE); + entries_count = std::stoul(entries_count_string); + current_position += ENTRIES_COUNT_SIZE; + // Following fields group repeat NO-MD-ENTRIES times + auto position = PROD_ID_SIZE + SEQ_NUMBER_SIZE + ENTRIES_COUNT_SIZE; + for (size_t i = 0; i < entries_count; ++i) { + entries.push_back(MD_ENTRY(buffer, offset + position)); + position += 13; + // current_position += ENTRIES_COUNT_SIZE;//need to fix + } + } }; - std::vector entries; - std::string prod_id; - unsigned long seq_number; - int calculated_flag; - size_t entries_count; - friend std::ostream &operator<<(std::ostream &os, const I083Data &data) { - os << "Product ID: " << data.prod_id << "\n" - << "Sequence Number: " << data.seq_number << "\n" - << "Calculated Flag: " << data.calculated_flag << "\n" - << "Entries Count: " << data.entries_count << "\n" - << "MD entries: "; - for (auto entry : data.entries) { - std::cout << entry; + + struct I083Data { + struct MD_ENTRY { + char md_entry_type; + enum Sign { POSITIVE = 0, NEGATIVE = 1 }; + Sign sign; + double price; + size_t quantity; + size_t level; + + friend std::ostream &operator<<( + std::ostream &os, const I083Data::MD_ENTRY &entry) { + os << "\nMD Entry Type: " << entry.md_entry_type << "\n" + << "Sign: " + << (entry.sign == I083Data::MD_ENTRY::POSITIVE ? "Positive" + : "Negative") + << "\n" + << "Price: " << entry.price << "\n" + << "Quantity: " << entry.quantity << "\n" + << "Level: " << entry.level << "\n"; + return os; + } + + MD_ENTRY(const std::vector &buffer, size_t offset = 0) { + md_entry_type = buffer[offset]; + sign = static_cast(buffer[offset + 1]); + + // price + std::string price_str = + TaifexUtils::decodePackBCD(&buffer[offset + 2], 5); + price = std::stod(price_str); + + // quantity + std::string size_str = + TaifexUtils::decodePackBCD(&buffer[offset + 7], 4); + quantity = std::stoul(size_str); + + // level + std::string level_str = + TaifexUtils::decodePackBCD(&buffer[offset + 11], 1); + level = std::stoul(level_str); + }; + }; + std::vector entries; + std::string prod_id; + unsigned long seq_number; + int calculated_flag; + size_t entries_count; + friend std::ostream &operator<<(std::ostream &os, const I083Data &data) { + os << "Product ID: " << data.prod_id << "\n" + << "Sequence Number: " << data.seq_number << "\n" + << "Calculated Flag: " << data.calculated_flag << "\n" + << "Entries Count: " << data.entries_count << "\n" + << "MD entries: "; + for (auto entry : data.entries) { + std::cout << entry; + } + + return os; } - return os; - } + I083Data( + const std::vector &buffer, + const size_t &offset = 0, + size_t length = -1) { + UNUSED(length); + const size_t ENTRIES_COUNT_SIZE = 1; + const size_t PROD_ID_SIZE = 20; + const size_t SEQ_NUMBER_SIZE = 5; + const size_t CALCULATED_FLAG_SIZE = 1; - I083Data( - const std::vector &buffer, - const size_t &offset = 0, - size_t length = -1) { - UNUSED(length); - const size_t ENTRIES_COUNT_SIZE = 1; - const size_t PROD_ID_SIZE = 20; - const size_t SEQ_NUMBER_SIZE = 5; - const size_t CALCULATED_FLAG_SIZE = 1; + auto current_position = buffer.begin() + offset; - auto current_position = buffer.begin() + offset; + prod_id.assign(current_position, current_position + PROD_ID_SIZE); + current_position += PROD_ID_SIZE; - prod_id.assign(current_position, current_position + PROD_ID_SIZE); - current_position += PROD_ID_SIZE; + std::string seq_number_str = + TaifexUtils::decodePackBCD(&(*current_position), SEQ_NUMBER_SIZE); + seq_number = std::stoul(seq_number_str); + current_position += SEQ_NUMBER_SIZE; - std::string seq_number_str = - TaifexUtils::decodePackBCD(&(*current_position), SEQ_NUMBER_SIZE); - seq_number = std::stoul(seq_number_str); - current_position += SEQ_NUMBER_SIZE; + calculated_flag = static_cast(*current_position); + current_position += CALCULATED_FLAG_SIZE; - calculated_flag = static_cast(*current_position); - current_position += CALCULATED_FLAG_SIZE; + std::string entries_count_string = TaifexUtils::decodePackBCD( + &(*current_position), ENTRIES_COUNT_SIZE); + entries_count = std::stoul(entries_count_string); + current_position += ENTRIES_COUNT_SIZE; - std::string entries_count_string = - TaifexUtils::decodePackBCD(&(*current_position), ENTRIES_COUNT_SIZE); - entries_count = std::stoul(entries_count_string); - current_position += ENTRIES_COUNT_SIZE; + // Following fields group repeat NO-MD-ENTRIES times + auto position = PROD_ID_SIZE + SEQ_NUMBER_SIZE + CALCULATED_FLAG_SIZE + + ENTRIES_COUNT_SIZE; - // Following fields group repeat NO-MD-ENTRIES times - auto position = PROD_ID_SIZE + SEQ_NUMBER_SIZE + CALCULATED_FLAG_SIZE + - ENTRIES_COUNT_SIZE; - - for (size_t i = 0; i < entries_count; ++i) { - entries.push_back(MD_ENTRY(buffer, offset + position)); - position += 12; - // current_position += ENTRIES_COUNT_SIZE;//need to fix + for (size_t i = 0; i < entries_count; ++i) { + entries.push_back(MD_ENTRY(buffer, offset + position)); + position += 12; + // current_position += ENTRIES_COUNT_SIZE;//need to fix + } } - } + }; }; -}; -}//bts::md::taifex \ No newline at end of file +} // namespace bts::md::taifex \ No newline at end of file diff --git a/src/server/sparkd/md/taifex/TaifexSecurityDetailsMapper.h b/src/server/sparkd/md/taifex/TaifexSecurityDetailsMapper.h index 821ff82de..f0888373b 100644 --- a/src/server/sparkd/md/taifex/TaifexSecurityDetailsMapper.h +++ b/src/server/sparkd/md/taifex/TaifexSecurityDetailsMapper.h @@ -1,7 +1,7 @@ #include "lib/domain/SecurityID.h" #include -#include #include +#include namespace bts::md::taifex { class TaifexSecurityDetailsMapper { struct security_details { @@ -88,4 +88,4 @@ class TaifexSecurityDetailsMapper { std::unordered_map sec_deets_map; }; -}//bts::md::taifex \ No newline at end of file +} // namespace bts::md::taifex \ No newline at end of file diff --git a/src/server/sparkd/md/taifex/TaifexUtils.h b/src/server/sparkd/md/taifex/TaifexUtils.h index 844549282..02d67b751 100644 --- a/src/server/sparkd/md/taifex/TaifexUtils.h +++ b/src/server/sparkd/md/taifex/TaifexUtils.h @@ -9,300 +9,312 @@ namespace bts::md::taifex { class TaifexUtils { - public: + public: static void printBuffer( - const std::vector &buffer, - size_t offset = 0, - size_t length = std::numeric_limits::max()) { - // Adjust length if it exceeds buffer size - if (length == std::numeric_limits::max() || - offset + length > buffer.size()) { - length = buffer.size() - offset; - } - if (offset >= buffer.size()) - offset = buffer.size() - 1; - for (size_t i = 0; i < length; ++i) { - printf( - " offset: %zu, i: %zu, value: %.2X|", offset, i, - (unsigned int)buffer[offset + i]); - } - // std::cout << std::dec << std::endl; -} - -//Replace std::localtime with localtime_s (use localtime_r for POSIX) -static std::string getCurrentDate() { - auto now = std::chrono::system_clock::now(); - std::time_t now_c = std::chrono::system_clock::to_time_t(now); - std::tm local_tm = *std::localtime(&now_c); - - char buffer[11]; // Buffer to hold formatted date - std::strftime(buffer, sizeof(buffer), "%Y-%m-%d", &local_tm); - return std::string(buffer); -} - -static boost::posix_time::ptime convertToPTime(const std::string &match_time) { - if (match_time.length() != 12) { - throw std::invalid_argument( - "match_time must be exactly 12 characters long"); + const std::vector &buffer, + size_t offset = 0, + size_t length = std::numeric_limits::max()) { + // Adjust length if it exceeds buffer size + if (length == std::numeric_limits::max() || + offset + length > buffer.size()) { + length = buffer.size() - offset; + } + if (offset >= buffer.size()) + offset = buffer.size() - 1; + for (size_t i = 0; i < length; ++i) { + printf( + " offset: %zu, i: %zu, value: %.2X|", offset, i, + (unsigned int)buffer[offset + i]); + } + // std::cout << std::dec << std::endl; } - // Extract hours, minutes, seconds, and microseconds from match_time - std::string hours = match_time.substr(0, 2); - std::string minutes = match_time.substr(2, 2); - std::string seconds = match_time.substr(4, 2); - std::string microseconds = match_time.substr(6, 6); + //Replace std::localtime with localtime_s (use localtime_r for POSIX) + static std::string getCurrentDate() { + auto now = std::chrono::system_clock::now(); + std::time_t now_c = std::chrono::system_clock::to_time_t(now); + std::tm local_tm = *std::localtime(&now_c); - // Get the current date - std::string date = getCurrentDate(); - - // Combine the date and time - std::string datetime = date + " " + hours + ":" + minutes + ":" + seconds; - - // Convert to ptime - boost::posix_time::ptime pt = boost::posix_time::time_from_string(datetime); - - // Add fractional seconds (microseconds) - boost::posix_time::ptime result = - pt + boost::posix_time::microseconds(std::stoi(microseconds)); - - return result; -} - -static std::string convertToISO( - const std::string &match_time, - const std::string &format = "datetime_no_timezone") { - if (match_time.length() != 12) { - throw std::invalid_argument( - "in function convertToISO: match_time must be exactly 12 characters " - "long: -> " + - match_time); + char buffer[11]; // Buffer to hold formatted date + std::strftime(buffer, sizeof(buffer), "%Y-%m-%d", &local_tm); + return std::string(buffer); } - try { - // Extract time components + static boost::posix_time::ptime convertToPTime( + const std::string &match_time) { + if (match_time.length() != 12) { + throw std::invalid_argument( + "match_time must be exactly 12 characters long"); + } + + // Extract hours, minutes, seconds, and microseconds from match_time std::string hours = match_time.substr(0, 2); std::string minutes = match_time.substr(2, 2); std::string seconds = match_time.substr(4, 2); + std::string microseconds = match_time.substr(6, 6); - // Validate and sanitize time components - int h = std::abs(std::stoi(hours)) % 24; - int m = std::abs(std::stoi(minutes)) % 60; - int s = std::abs(std::stoi(seconds)) % 60; + // Get the current date + std::string date = getCurrentDate(); - // Get the current date dynamically - std::string current_date = getCurrentDate(); + // Combine the date and time + std::string datetime = date + " " + hours + ":" + minutes + ":" + seconds; - // Construct the desired ISO format - std::ostringstream iso_stream; - if (format == "datetime_with_timezone") { - // Full date-time with time zone: "YYYY-MM-DDTHH:MM:SS+02:00" (for simplicity using UTC "Z") - iso_stream << current_date << "T" << std::setfill('0') << std::setw(2) - << h << ":" << std::setfill('0') << std::setw(2) << m << ":" - << std::setfill('0') << std::setw(2) << s - << "Z"; // Assuming UTC for now - } else if (format == "datetime_no_timezone") { - // Date-time without time zone: "YYYY-MM-DDTHH:MM:SS" - iso_stream << current_date << "T" << std::setfill('0') << std::setw(2) - << h << ":" << std::setfill('0') << std::setw(2) << m << ":" - << std::setfill('0') << std::setw(2) << s; - } else if (format == "date_only") { - // Date only: "YYYY-MM-DD" - iso_stream << current_date; - } else { + // Convert to ptime + boost::posix_time::ptime pt = + boost::posix_time::time_from_string(datetime); + + // Add fractional seconds (microseconds) + boost::posix_time::ptime result = + pt + boost::posix_time::microseconds(std::stoi(microseconds)); + + return result; + } + + static std::string convertToISO( + const std::string &match_time, + const std::string &format = "datetime_no_timezone") { + if (match_time.length() != 12) { throw std::invalid_argument( - "Unknown format specified for convertToISO: " + format); + "in function convertToISO: match_time must be exactly 12 " + "characters " + "long: -> " + + match_time); } - return iso_stream.str(); - } catch (const std::exception &e) { - std::cerr << "Error in convertToISO: " << e.what() << std::endl; - throw; - } -} + try { + // Extract time components + std::string hours = match_time.substr(0, 2); + std::string minutes = match_time.substr(2, 2); + std::string seconds = match_time.substr(4, 2); -static std::string sanitizeISOString(const std::string &isoString) { - std::string result = isoString; + // Validate and sanitize time components + int h = std::abs(std::stoi(hours)) % 24; + int m = std::abs(std::stoi(minutes)) % 60; + int s = std::abs(std::stoi(seconds)) % 60; - // Remove any whitespace - result.erase( - std::remove_if(result.begin(), result.end(), ::isspace), result.end()); + // Get the current date dynamically + std::string current_date = getCurrentDate(); - // Ensure it's at least 20 characters long (e.g., YYYY-MM-DDTHH:MM:SS) - if (result.size() != 20) { - if (result.size() < 20) { - // If it's too short, pad with zeros at the end (assuming it's missing time components) - result.append(20 - result.size(), '0'); - } else { - // If it's too long, truncate to 20 characters - result = result.substr(0, 20); - } - } - - // Check for and correct any time zone issues or missing Z (UTC indicator) - size_t posT = result.find('T'); - if (posT != std::string::npos) { - size_t posZ = result.find('Z', posT); - if (posZ == std::string::npos) { - size_t posMinus = result.find('-', posT); - size_t posPlus = result.find('+', posT); - if (posMinus != std::string::npos && posMinus > posT) { - // Check if it's "-0" or "-00" or "-000", etc. - if (std::all_of( - result.begin() + posMinus + 1, result.end(), - [](char c) { return c == '0'; })) { - result = result.substr(0, posMinus) + "Z"; - } - } else if (posPlus != std::string::npos && posPlus > posT) { - // Check if it's "+0" or "+00" or "+000", etc. - if (std::all_of( - result.begin() + posPlus + 1, result.end(), - [](char c) { return c == '0'; })) { - result = result.substr(0, posPlus) + "Z"; - } + // Construct the desired ISO format + std::ostringstream iso_stream; + if (format == "datetime_with_timezone") { + // Full date-time with time zone: "YYYY-MM-DDTHH:MM:SS+02:00" (for simplicity using UTC "Z") + iso_stream << current_date << "T" << std::setfill('0') + << std::setw(2) << h << ":" << std::setfill('0') + << std::setw(2) << m << ":" << std::setfill('0') + << std::setw(2) << s << "Z"; // Assuming UTC for now + } else if (format == "datetime_no_timezone") { + // Date-time without time zone: "YYYY-MM-DDTHH:MM:SS" + iso_stream << current_date << "T" << std::setfill('0') + << std::setw(2) << h << ":" << std::setfill('0') + << std::setw(2) << m << ":" << std::setfill('0') + << std::setw(2) << s; + } else if (format == "date_only") { + // Date only: "YYYY-MM-DD" + iso_stream << current_date; } else { - // If there's no timezone indicator, assume UTC and add 'Z' - result += "Z"; + throw std::invalid_argument( + "Unknown format specified for convertToISO: " + format); + } + + return iso_stream.str(); + } catch (const std::exception &e) { + std::cerr << "Error in convertToISO: " << e.what() << std::endl; + throw; + } + } + + static std::string sanitizeISOString(const std::string &isoString) { + std::string result = isoString; + + // Remove any whitespace + result.erase( + std::remove_if(result.begin(), result.end(), ::isspace), + result.end()); + + // Ensure it's at least 20 characters long (e.g., YYYY-MM-DDTHH:MM:SS) + if (result.size() != 20) { + if (result.size() < 20) { + // If it's too short, pad with zeros at the end (assuming it's missing time components) + result.append(20 - result.size(), '0'); + } else { + // If it's too long, truncate to 20 characters + result = result.substr(0, 20); } } - } - return result; -} - -static std::string time_decodePackBCD(const unsigned char *data, size_t length) { - std::stringstream ss; - for (size_t i = 0; i < length; ++i) { - int upper = (data[i] >> 4) & 0x0F; - int lower = data[i] & 0x0F; - - // Check if the decoded values are valid digits - if (upper > 9 || lower > 9) { - // Instead of throwing an error, replace invalid digits with '0' - upper = upper > 9 ? 0 : upper; - lower = lower > 9 ? 0 : lower; + // Check for and correct any time zone issues or missing Z (UTC indicator) + size_t posT = result.find('T'); + if (posT != std::string::npos) { + size_t posZ = result.find('Z', posT); + if (posZ == std::string::npos) { + size_t posMinus = result.find('-', posT); + size_t posPlus = result.find('+', posT); + if (posMinus != std::string::npos && posMinus > posT) { + // Check if it's "-0" or "-00" or "-000", etc. + if (std::all_of( + result.begin() + posMinus + 1, result.end(), + [](char c) { return c == '0'; })) { + result = result.substr(0, posMinus) + "Z"; + } + } else if (posPlus != std::string::npos && posPlus > posT) { + // Check if it's "+0" or "+00" or "+000", etc. + if (std::all_of( + result.begin() + posPlus + 1, result.end(), + [](char c) { return c == '0'; })) { + result = result.substr(0, posPlus) + "Z"; + } + } else { + // If there's no timezone indicator, assume UTC and add 'Z' + result += "Z"; + } + } } - ss << upper << lower; + return result; } - std::string result = ss.str(); + static std::string time_decodePackBCD( + const unsigned char *data, size_t length) { + std::stringstream ss; + for (size_t i = 0; i < length; ++i) { + int upper = (data[i] >> 4) & 0x0F; + int lower = data[i] & 0x0F; - // Ensure the result is exactly 12 characters long (hhmmssxxxxxx) - if (result.length() < 12) { - // If less than 12 characters, pad with zeros at the end - result = result + std::string(12 - result.length(), '0'); - } else if (result.length() > 12) { - // If more than 12 characters, keep the first 12 - result = result.substr(0, 12); + // Check if the decoded values are valid digits + if (upper > 9 || lower > 9) { + // Instead of throwing an error, replace invalid digits with '0' + upper = upper > 9 ? 0 : upper; + lower = lower > 9 ? 0 : lower; + } + + ss << upper << lower; + } + + std::string result = ss.str(); + + // Ensure the result is exactly 12 characters long (hhmmssxxxxxx) + if (result.length() < 12) { + // If less than 12 characters, pad with zeros at the end + result = result + std::string(12 - result.length(), '0'); + } else if (result.length() > 12) { + // If more than 12 characters, keep the first 12 + result = result.substr(0, 12); + } + + // Ensure the time part (hhmmss) is valid + int hours = std::stoi(result.substr(0, 2)); + int minutes = std::stoi(result.substr(2, 2)); + int seconds = std::stoi(result.substr(4, 2)); + + hours = std::min(23, std::max(0, hours)); + minutes = std::min(59, std::max(0, minutes)); + seconds = std::min(59, std::max(0, seconds)); + + // Reconstruct the result with valid time + std::stringstream final_ss; + final_ss << std::setfill('0') << std::setw(2) << hours + << std::setfill('0') << std::setw(2) << minutes + << std::setfill('0') << std::setw(2) << seconds + << result.substr(6); // Keep the last 6 characters (xxxxxx) as is + + return final_ss.str(); } - // Ensure the time part (hhmmss) is valid - int hours = std::stoi(result.substr(0, 2)); - int minutes = std::stoi(result.substr(2, 2)); - int seconds = std::stoi(result.substr(4, 2)); + // need to change parameters + static std::string decodePackBCD(const unsigned char *data, size_t length) { + std::stringstream ss; + for (size_t i = 0; i < length; ++i) { + // Extract the upper and lower nibble (4 bits each) + int upper = (data[i] >> 4) & 0x0F; + int lower = data[i] & 0x0F; - hours = std::min(23, std::max(0, hours)); - minutes = std::min(59, std::max(0, minutes)); - seconds = std::min(59, std::max(0, seconds)); + // Append both nibbles to the stringstream + ss << upper << lower; + } - // Reconstruct the result with valid time - std::stringstream final_ss; - final_ss << std::setfill('0') << std::setw(2) << hours << std::setfill('0') - << std::setw(2) << minutes << std::setfill('0') << std::setw(2) - << seconds - << result.substr(6); // Keep the last 6 characters (xxxxxx) as is + std::string result = ss.str(); - return final_ss.str(); -} + // Remove leading zeros + result.erase(0, result.find_first_not_of('0')); -// need to change parameters -static std::string decodePackBCD(const unsigned char *data, size_t length) { - std::stringstream ss; - for (size_t i = 0; i < length; ++i) { - // Extract the upper and lower nibble (4 bits each) - int upper = (data[i] >> 4) & 0x0F; - int lower = data[i] & 0x0F; + // If the result is empty (all zeros), return "0" + return result.empty() ? "0" : result; + } + static std::string extractString( + const std::vector &buffer, + size_t &position, + size_t length) { + // Extract the substring from the buffer + std::string result( + buffer.begin() + position, buffer.begin() + position + length); + position += length; - // Append both nibbles to the stringstream - ss << upper << lower; + // Log the raw result + return result; } - std::string result = ss.str(); - - // Remove leading zeros - result.erase(0, result.find_first_not_of('0')); - - // If the result is empty (all zeros), return "0" - return result.empty() ? "0" : result; -} -static std::string extractString( - const std::vector &buffer, size_t &position, size_t length) { - // Extract the substring from the buffer - std::string result( - buffer.begin() + position, buffer.begin() + position + length); - position += length; - - // Log the raw result - return result; -} - -// Helper function to extract a BCD value from the buffer and convert to -// unsigned long -static unsigned long extractBCD_STOUL( - const std::vector &buffer, size_t &position, size_t length) { - std::string bcd_str = decodePackBCD(&buffer[position], length); - position += length; - return std::stoul(bcd_str); -} -static std::string extractBCD_STRING( - const std::vector &buffer, size_t &position, size_t length) { - std::string bcd_str = decodePackBCD(&buffer[position], length); - position += length; - return bcd_str; -} -// Helper function to extract a single character and convert to bool -static bool extractBool(const std::vector &buffer, size_t &position) { - bool result = (buffer[position] == 'Y'); - position += 1; - return result; -} - -static std::string extractBCDDate( - const std::vector &buffer, - size_t ¤t_position, - size_t size) { - if (size != 4) { - throw std::invalid_argument("BCD date must be 4 bytes long"); + // Helper function to extract a BCD value from the buffer and convert to + // unsigned long + static unsigned long extractBCD_STOUL( + const std::vector &buffer, + size_t &position, + size_t length) { + std::string bcd_str = decodePackBCD(&buffer[position], length); + position += length; + return std::stoul(bcd_str); + } + static std::string extractBCD_STRING( + const std::vector &buffer, + size_t &position, + size_t length) { + std::string bcd_str = decodePackBCD(&buffer[position], length); + position += length; + return bcd_str; + } + // Helper function to extract a single character and convert to bool + static bool extractBool( + const std::vector &buffer, size_t &position) { + bool result = (buffer[position] == 'Y'); + position += 1; + return result; } - std::string date; - for (size_t i = 0; i < size; ++i) { - unsigned char byte = buffer[current_position++]; - char high = (byte >> 4) + '0'; - char low = (byte & 0x0F) + '0'; - date.push_back(high); - date.push_back(low); + static std::string extractBCDDate( + const std::vector &buffer, + size_t ¤t_position, + size_t size) { + if (size != 4) { + throw std::invalid_argument("BCD date must be 4 bytes long"); + } + + std::string date; + for (size_t i = 0; i < size; ++i) { + unsigned char byte = buffer[current_position++]; + char high = (byte >> 4) + '0'; + char low = (byte & 0x0F) + '0'; + date.push_back(high); + date.push_back(low); + } + + // Ensure the date is in YYYYMMDD format + if (date.length() != 8) { + throw std::runtime_error("Extracted date is not in YYYYMMDD format"); + } + + return date; } - // Ensure the date is in YYYYMMDD format - if (date.length() != 8) { - throw std::runtime_error("Extracted date is not in YYYYMMDD format"); - } - - return date; -} - - -static auto remove_spaces(std::string &name) { - std::string removed = ""; - const char space = ' '; - for (auto c : name) { - if (c != space) { - removed += c; - } - } - return removed; - }; + static auto remove_spaces(std::string &name) { + std::string removed = ""; + const char space = ' '; + for (auto c : name) { + if (c != space) { + removed += c; + } + } + return removed; + }; }; } //namespace bts::md::taifex \ No newline at end of file