Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
coinbase_trade_encoder.cpp
Go to the documentation of this file.
2
3#include <common/order.hpp>
4#include <common/types.hpp>
5#include <utils/timestamp.hpp>
6#include <string>
7
8using json = nlohmann::json;
9
11
12namespace {
13
14// The venue reports per-trade times to microsecond precision, and the message
15// envelope to nanosecond precision.
16constexpr int TRADE_TIME_DIGITS = 6;
17constexpr int ENVELOPE_TIME_DIGITS = 9;
18
19json encode_trade(const char* product_id, uint64_t trade_id, uint64_t event_time,
20 price_t price, qty_t qty, Side aggressor_side) {
21 return {
22 {"trade_id", std::to_string(trade_id)},
23 {"product_id", product_id},
24 {"price", to_price_string(price)},
25 {"size", to_qty_string(qty)},
26 {"side", to_string(aggressor_side)},
27 {"time", utils::format_timestamp_iso8601(event_time, TRADE_TIME_DIGITS)}
28 };
29}
30
31json encode_envelope(const char* event_type, json::array_t&& trades) {
32 return {
33 {"channel", "market_trades"},
34 {"client_id", ""},
35 {"timestamp", utils::format_timestamp_iso8601(ENVELOPE_TIME_DIGITS)},
36 {"events", {{
37 {"type", event_type},
38 {"trades", std::move(trades)}
39 }}}
40 };
41}
42
43} // namespace
44
45json encode_market_trade(const char* product_id, const TradeSummary& summary) {
46 json::array_t trades;
47 trades.push_back(encode_trade(product_id, summary.trade_id,
48 static_cast<uint64_t>(summary.timestamp),
49 summary.price, summary.qty, summary.aggressor_side));
50 return encode_envelope("update", std::move(trades));
51}
52
53json encode_market_trades(const char* product_id, const char* event_type,
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));
61 }
62 return encode_envelope(event_type, std::move(encoded));
63}
64
65} // end namespace slick::sim::md_publisher
nlohmann::json json
Definition order.hpp:16
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)
Definition timestamp.hpp:9
constexpr const char * to_string(FeedState state) noexcept
std::string to_price_string(price_t price)
Definition types.hpp:107
int_fast64_t qty_t
Definition types.hpp:22
std::string to_qty_string(qty_t qty)
Definition types.hpp:108
int_fast64_t price_t
Definition types.hpp:21