Files
spark/src/lib/md/binance/BinancePriceStatistics.cpp
T
John Colvin 8fa82840be feat: Add Binance market data
Restructure build configurations so 'dev' builds target venue test urls,
while 'prod' builds target production urls (technically, there is only
one prod build at the moment; it's compiled w/ gcc/release). This is
necessary because the urls ccapi uses are defined at compile time (all
seem to be pre-processor directives defined in ccapi_macro.h).
2023-01-12 12:50:28 -05:00

125 lines
4.0 KiB
C++

#ifdef SPARK_ENABLE_CCAPI
#include "BinancePriceStatistics.h"
#include "lib/core/TimeStamp.h"
#include "lib/domain/Price.h"
#include "lib/domain/Quantity.h"
#include "lib/domain/SecurityMarketDataStatistics.h"
#include "lib/io/JsonDumper.h"
#include "lib/io/JsonReader.h"
#include "lib/md/CCApiReader.h"
#include <ccapi_cpp/ccapi_macro.h>
#include <ccapi_cpp/ccapi_message.h>
#include <string>
namespace bts::md::binance {
using bts::core::TimeStamp;
using bts::domain::Price;
using bts::domain::Quantity;
using bts::domain::SecurityID;
using bts::domain::SecurityMarketDataStatistics;
using bts::io::JsonReader;
using std::map;
using std::string;
using std::vector;
static bool parse(
const map<string, string> &name_values,
vector<BinancePriceStatistics> &result) {
CCApiReader ccapi_reader{&name_values};
nlohmann::json js;
if (!ccapi_reader.read(CCAPI_HTTP_BODY, js)) {
return false;
}
JsonReader jr{&js};
if (!jr.read(result)) {
LOG(md, Error, "Failed to read BinancePriceStatistics json:\n{}", js);
return false;
}
return true;
}
bool BinancePriceStatistics::parse(
const ccapi::Message &msg, vector<BinancePriceStatistics> &result) {
auto orig_sz = result.size();
for (const auto &sec_el : msg.getElementList()) {
const auto &hp_props = sec_el.getNameValueMap();
auto it = hp_props.find(CCAPI_HTTP_BODY);
if (it == hp_props.end()) {
continue;
}
bts::md::binance::parse(hp_props, result);
}
return result.size() > orig_sz;
}
void BinancePriceStatistics::to_security_statistics(
SecurityMarketDataStatistics &result) const {
result.open = Price::from_double(open_price);
result.settle = Price::from_double(prev_close_price);
result.high = Price::from_double(high_price);
result.low = Price::from_double(low_price);
result.volume.value() = static_cast<Quantity::Value>(quote_volume);
}
void from_json(const nlohmann::json &j, BinancePriceStatistics &result) {
JsonReader jr{&j};
jr.read("symbol", result.symbol);
jr.read("priceChange", result.price_change);
jr.read("priceChangePercent", result.price_change_percent);
jr.read("weightedAvgPrice", result.weighted_avg_price);
jr.read("prevClosePrice", result.prev_close_price);
jr.read("lastPrice", result.last_price);
jr.read("lastQty", result.last_qty);
jr.read("bidPrice", result.bid_price);
jr.read("bidQty", result.bid_qty);
jr.read("askPrice", result.ask_price);
jr.read("askQty", result.ask_qty);
jr.read("openPrice", result.open_price);
jr.read("highPrice", result.high_price);
jr.read("lowPrice", result.low_price);
jr.read("volume", result.volume);
jr.read("quoteVolume", result.quote_volume);
result.open_time = TimeStamp::from_millis(jr.read<std::int64_t>("openTime"));
result.close_time =
TimeStamp::from_millis(jr.read<std::int64_t>("closeTime"));
jr.read("firstId", result.first_id);
jr.read("lastId", result.last_id);
jr.read("count", result.count);
}
void to_json(nlohmann::json &j, const BinancePriceStatistics &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<std::int64_t>(p.open_time.millis())},
{"closeTime", static_cast<std::int64_t>(p.close_time.millis())},
{"firstId", p.first_id},
{"lastId", p.last_id},
{"count", p.count}};
}
std::ostream &operator<<(
std::ostream &out, const BinancePriceStatistics &p) noexcept {
out << io::JsonDumper::dump(p);
return out;
}
} // namespace bts::md::binance
#endif