8using json = nlohmann::json;
16constexpr int TRADE_TIME_DIGITS = 6;
17constexpr int ENVELOPE_TIME_DIGITS = 9;
19json encode_trade(
const char* product_id, uint64_t trade_id, uint64_t event_time,
22 {
"trade_id", std::to_string(trade_id)},
23 {
"product_id", product_id},
31json encode_envelope(
const char* event_type, json::array_t&& trades) {
33 {
"channel",
"market_trades"},
38 {
"trades", std::move(trades)}
47 trades.push_back(encode_trade(product_id, summary.
trade_id,
50 return encode_envelope(
"update", std::move(trades));
54 const MDTrade* trades, uint32_t num_trades,
bool newest_first) {
55 json::array_t encoded;
56 encoded.reserve(num_trades);
57 for (uint32_t i = 0; i < num_trades; ++i) {
58 const auto& trade = trades[newest_first ? num_trades - 1 - i : i];
59 encoded.push_back(encode_trade(product_id, trade.trade_id, trade.event_time,
60 trade.price, trade.qty, trade.side));
62 return encode_envelope(event_type, std::move(encoded));
json encode_market_trade(const char *product_id, const TradeSummary &summary)
One executed trade, as a "type":"update" event.
json encode_market_trades(const char *product_id, const char *event_type, const MDTrade *trades, uint32_t num_trades, bool newest_first)
A run of trades as a single event of the given type.
std::string format_timestamp_iso8601(const std::chrono::system_clock::time_point &tp, int num_sebsecond_digits=3)
constexpr const char * to_string(FeedState state) noexcept
std::string to_price_string(price_t price)
std::string to_qty_string(qty_t qty)