#ifdef SPARK_ENABLE_CCAPI #include "CCApiTradeRecord.h" #include "lib/domain/AggressorSide.h" #include "lib/domain/Price.h" #include "lib/domain/Quantity.h" #include "lib/domain/TradeRecord.h" #include "lib/io/JsonDumper.h" #include "lib/io/JsonReader.h" #include "lib/md/CCApiReader.h" #include "lib/utils/datetime.h" #include "system/bts_chrono.h" #include #include #include #include #include #include namespace bts::md { using bts::domain::TradeRecord; using bts::io::JsonDumper; using bts::io::JsonReader; using std::vector; void CCApiTradeRecord::to_trade_record(TradeRecord &value) const { // This alias allows the underlying type in 'domain::Quantity' to change // without breaking this code (will change from uint to double soon) using QuantityValue = std::remove_reference< decltype(std::declval().value())>::type; value.trade_price = domain::Price::from_double(last_price); // Temp code to show non-zero values until we have double quantities value.trade_quantity.value() = static_cast(std::max(last_size * 1000, 1.0)); value.agg_side = is_buyer_maker ? domain::AggressorSide::Buy : domain::AggressorSide::Sell; } bool CCApiTradeRecord::parse( const ccapi::Element &source, CCApiTradeRecord &result, bool is_aggregated) { CCApiReader reader{&source.getNameValueMap()}; return reader.read(CCAPI_LAST_PRICE, result.last_price) && reader.read(CCAPI_LAST_SIZE, result.last_size) && reader.read( (is_aggregated ? CCAPI_AGG_TRADE_ID : CCAPI_TRADE_ID), result.trade_id) && reader.read(CCAPI_IS_BUYER_MAKER, result.is_buyer_maker); } std::vector CCApiTradeRecord::parse( const ccapi::Message &msg) { std::vector result; bts_assert(parse(msg, result)); return result; } bool CCApiTradeRecord::parse( const ccapi::Message &msg, vector &result) { bool is_aggregated = false; switch (msg.getType()) { case ccapi::Message::Type::MARKET_DATA_EVENTS_AGG_TRADE: is_aggregated = true; break; case ccapi::Message::Type::MARKET_DATA_EVENTS_TRADE: break; default: return false; } const auto &trade_els = msg.getElementList(); result.reserve(result.size() + trade_els.size()); for (const auto &trade_el : trade_els) { auto &trade_rec = result.emplace_back(); if (!CCApiTradeRecord::parse(trade_el, trade_rec, is_aggregated)) { return false; } } return true; } void from_json(const nlohmann::json &j, CCApiTradeRecord &r) { JsonReader jr{&j}; bool success = jr.read(CCAPI_LAST_PRICE, r.last_price) && jr.read(CCAPI_LAST_SIZE, r.last_size) && jr.read(CCAPI_TRADE_ID, r.trade_id) && jr.read(CCAPI_IS_BUYER_MAKER, r.is_buyer_maker); if (!success) { const auto j_str = j.dump(2); LOG(md, Error, "Failed to parse json to CCApiTradeRecord:\n{}", j_str); } } void to_json(nlohmann::json &j, const CCApiTradeRecord &r) { j = nlohmann::json{ {CCAPI_LAST_PRICE, r.last_price}, {CCAPI_LAST_SIZE, r.last_size}, {CCAPI_TRADE_ID, r.trade_id}, {CCAPI_IS_BUYER_MAKER, r.is_buyer_maker}}; } std::ostream &operator<<( std::ostream &out, const CCApiTradeRecord &r) noexcept { out << JsonDumper::dump(r, 0); return out; } } // namespace bts::md #endif