Files
spark/src/app_sparkd/sparkd/md/coinbase/CoinbasePriceStatistics.cpp
T
John Colvin a26918efe9 refactor: Rename a few TitleCase .h files snake_case
* EnumMacros.h -> enum_macros.h
* TimeSpan.h ---> time_span.h
* TimeStamp.h --> time_stamp.h
2026-03-26 16:20:40 -04:00

100 lines
3.2 KiB
C++

#include "CoinbasePriceStatistics.h"
#include "spark/core/time_stamp.h"
#include "spark/domain/Price.h"
#include "spark/domain/Quantity.h"
#include "spark/domain/SecurityMarketDataStatistics.h"
#include "spark/io/JsonDumper.h"
#include "spark/io/JsonReader.h"
#include "sparkd/md/ccapi/CCApiReader.h"
#include <ccapi_cpp/ccapi_macro.h>
#include <ccapi_cpp/ccapi_message.h>
#include <string>
namespace bts::md::coinbase {
using bts::domain::Price;
using bts::domain::Quantity;
using bts::domain::SecurityID;
using bts::domain::SecurityMarketDataStatistics;
using bts::io::JsonReader;
using std::int64_t;
using std::map;
using std::ostream;
using std::string;
using std::vector;
static bool parse(
const map<string, string> &name_values,
vector<CoinbasePriceStatistics> &result) {
auto it = name_values.find(CCAPI_HTTP_BODY);
const auto &body = it->second;
const auto js = nlohmann::json::parse(body);
CoinbasePriceStatistics r;
from_json(js, r);
result.emplace_back(r);
return true;
}
bool CoinbasePriceStatistics::parse(
const ccapi::Message &msg, vector<CoinbasePriceStatistics> &result) {
auto orig_sz = result.size();
for (const auto &sec_el : msg.getElementList()) {
const auto &hp_props = sec_el.getNameValueMap();
auto st_it = hp_props.find(CCAPI_HTTP_STATUS_CODE);
auto it = hp_props.find(CCAPI_HTTP_BODY);
if (it == hp_props.end() || st_it->second != "200") {
continue;
}
bts::md::coinbase::parse(hp_props, result);
}
return result.size() > orig_sz;
}
void CoinbasePriceStatistics::to_security_statistics(
SecurityMarketDataStatistics &result) const {
result.open = Price::from_double(open_price);
result.settle = Price::from_double(last_price);
result.high = Price::from_double(high_price);
result.low = Price::from_double(low_price);
result.volume.value() = static_cast<Quantity::Value>(volume);
}
void from_json(const nlohmann::json &j, CoinbasePriceStatistics &result) {
JsonReader jr{&j};
jr.read("last", result.last_price);
jr.read("open", result.open_price);
jr.read("high", result.high_price);
jr.read("low", result.low_price);
jr.read("volume", result.volume);
}
void to_json(nlohmann::json &j, const CoinbasePriceStatistics &p) {
j = nlohmann::json{
{"symbol", p.symbol},
{"priceChange", p.price_change},
{"priceChangePercent", p.price_change_percent},
{"weightedAvgPrice", p.weighted_avg_price},
{"prevClosePrice", p.prev_close_price},
{"lastPrice", p.last_price},
{"lastQty", p.last_qty},
{"bidPrice", p.bid_price},
{"bidQty", p.bid_qty},
{"askPrice", p.ask_price},
{"askQty", p.ask_qty},
{"openPrice", p.open_price},
{"highPrice", p.high_price},
{"lowPrice", p.low_price},
{"volume", p.volume},
{"quoteVolume", p.quote_volume},
{"openTime", static_cast<int64_t>(p.open_time.millis())},
{"closeTime", static_cast<int64_t>(p.close_time.millis())},
{"firstId", p.first_id},
{"lastId", p.last_id},
{"count", p.count}};
}
ostream &operator<<(ostream &out, const CoinbasePriceStatistics &p) noexcept {
out << io::JsonDumper::dump(p);
return out;
}
} // namespace bts::md::coinbase