diff --git a/src/lib/md/binance/BinanceBookLevel.cpp b/src/lib/md/binance/BinanceBookLevel.cpp new file mode 100644 index 000000000..b82c66b24 --- /dev/null +++ b/src/lib/md/binance/BinanceBookLevel.cpp @@ -0,0 +1,44 @@ +#include "BinanceBookLevel.h" +#include "lib/os/os.h" + +namespace bts::md::binance { + +bool operator==(const BinanceBookLevel &lhs, const BinanceBookLevel &rhs) { + // return lhs.quantity == rhs.quantity && lhs.price == rhs.price; + if (lhs.quantity != rhs.quantity) { + return false; + } + if (lhs.price == rhs.price) { + return true; + } + return os::is_nan(lhs.price) && os::is_nan(rhs.price); +} + +bool operator!=(const BinanceBookLevel &lhs, const BinanceBookLevel &rhs) { + return !(lhs == rhs); +} + +std::ostream &operator<<( + std::ostream &out, const BinanceBookLevel &book_level) noexcept { + out << book_level.quantity << "@" << book_level.price; + return out; +} + +void from_json(const nlohmann::json &j, BinanceBookLevel &upd) { + if (j.is_array() && j.size() == 2) { + const nlohmann::json &jprice = j.at(0); + const auto &price_str = jprice.get(); + upd.price = std::stod(price_str); + + const nlohmann::json &jqty = j.at(1); + const auto &qty_str = jqty.get(); + upd.quantity = std::stod(qty_str); + } +} + +void to_json(nlohmann::json &j, const BinanceBookLevel &upd) { + j.push_back(upd.price); + j.push_back(upd.quantity); +} + +} // namespace bts::md::binance diff --git a/src/lib/md/binance/BinanceBookLevel.h b/src/lib/md/binance/BinanceBookLevel.h new file mode 100644 index 000000000..3e57284c5 --- /dev/null +++ b/src/lib/md/binance/BinanceBookLevel.h @@ -0,0 +1,23 @@ +#pragma once +#include +#include +#include + +namespace bts::md::binance { + +class BinanceBookLevel { + public: + double price; + std::size_t quantity; +}; + +bool operator==(const BinanceBookLevel &lhs, const BinanceBookLevel &rhs); +bool operator!=(const BinanceBookLevel &lhs, const BinanceBookLevel &rhs); + +std::ostream &operator<<( + std::ostream &out, const BinanceBookLevel &book_level) noexcept; + +void from_json(const nlohmann::json &j, BinanceBookLevel &upd); +void to_json(nlohmann::json &j, const BinanceBookLevel &upd); + +} // namespace bts::md::binance diff --git a/src/lib/md/binance/BinanceBookLevelComp.h b/src/lib/md/binance/BinanceBookLevelComp.h new file mode 100644 index 000000000..859bae36a --- /dev/null +++ b/src/lib/md/binance/BinanceBookLevelComp.h @@ -0,0 +1,46 @@ +#pragma once +#include "BinanceBookLevel.h" +#include "lib/domain/Price.h" +#include "lib/domain/Side.h" + +namespace bts::md::binance { + +class BinanceBookLevelComp { + public: + explicit constexpr BinanceBookLevelComp(domain::Side side); + + constexpr bool operator()( + const BinanceBookLevel &lhs, const BinanceBookLevel &rhs) const; + constexpr bool operator()(const BinanceBookLevel &lhs, double rhs) const; + constexpr bool operator()(double lhs, const BinanceBookLevel &rhs) const; + + private: + domain::Side side_; +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +constexpr BinanceBookLevelComp::BinanceBookLevelComp(domain::Side side) + : side_(side) {} + +constexpr bool BinanceBookLevelComp::operator()( + const BinanceBookLevel &lhs, const BinanceBookLevel &rhs) const { + if (lhs.price == rhs.price) { + return lhs.quantity > rhs.quantity; + } + return domain::Price::is_better(side_, lhs.price, rhs.price); +} + +constexpr bool BinanceBookLevelComp::operator()( + const BinanceBookLevel &lhs, double rhs) const { + return domain::Price::is_better(side_, lhs.price, rhs); +} + +constexpr bool BinanceBookLevelComp::operator()( + double lhs, const BinanceBookLevel &rhs) const { + return domain::Price::is_better(side_, lhs, rhs.price); +} + +} // namespace bts::md::binance diff --git a/src/lib/md/binance/BinanceBookTickerUpdate.h b/src/lib/md/binance/BinanceBookTickerUpdate.h new file mode 100644 index 000000000..cf7f6660b --- /dev/null +++ b/src/lib/md/binance/BinanceBookTickerUpdate.h @@ -0,0 +1,23 @@ +#pragma once +#include +#include + +namespace bts::md::binance { + +class BinanceBookTickerUpdate { + public: + // json name: u + std::size_t order_book_update_id; + // json name: s + std::string symbol; + // json name: b + double best_bid_price; + // json name: B + double best_bid_quantity; + // json name: a + double best_ask_price; + // json name: A + double best_ask_quantity; +}; + +} // namespace bts::md::binance diff --git a/src/lib/md/binance/BinanceDiffDepthUpdate.h b/src/lib/md/binance/BinanceDiffDepthUpdate.h new file mode 100644 index 000000000..ed66aa9c6 --- /dev/null +++ b/src/lib/md/binance/BinanceDiffDepthUpdate.h @@ -0,0 +1,29 @@ +#pragma once +#include "BinanceBookLevel.h" +#include +#include +#include + +namespace bts::md::binance { + +class BinanceDiffDepthUpdate { + public: + // json name: e + std::string event_type; + // json name: E + std::size_t event_time; + // json name: s + std::string symbol; + // The first event whose transition is included in this update + // json name: U + std::size_t first_update_id; + // The last event whose transition is included in this update + // json name: u + std::size_t last_update_id; + // json name: b + std::vector changed_bids; + // json name: a + std::vector changed_asks; +}; + +} // namespace bts::md::binance diff --git a/src/lib/md/binance/BinanceMiniTickerUpdate.h b/src/lib/md/binance/BinanceMiniTickerUpdate.h new file mode 100644 index 000000000..b351af5a8 --- /dev/null +++ b/src/lib/md/binance/BinanceMiniTickerUpdate.h @@ -0,0 +1,29 @@ +#pragma once +#include +#include + +namespace bts::md::binance { + +class BinanceMiniTickerUpdate { + public: + // json name: e + std::string event_type; + // json name: E + std::size_t event_time; + // json name: s + std::string symbol; + // json name: c + double close_price; + // json name: o + double open_price; + // json name: h + double high_price; + // json name: l + double low_price; + // json name: v + std::size_t base_asset_volume; + // json name: q + std::size_t quote_asset_volume; +}; + +} // namespace bts::md::binance diff --git a/src/lib/md/binance/BinancePartialBookDepthUpdate.h b/src/lib/md/binance/BinancePartialBookDepthUpdate.h new file mode 100644 index 000000000..023040f65 --- /dev/null +++ b/src/lib/md/binance/BinancePartialBookDepthUpdate.h @@ -0,0 +1,17 @@ +#pragma once +#include "BinanceBookLevel.h" +#include + +namespace bts::md::binance { + +class BinancePartialBookDepthUpdate { + public: + // json name: lastUpdateId + std::size_t last_update_id; + // json name: bids + std::vector bids; + // json name: asks + std::vector asks; +}; + +} // namespace bts::md::binance diff --git a/src/lib/md/binance/BinanceTickerUpdate.h b/src/lib/md/binance/BinanceTickerUpdate.h new file mode 100644 index 000000000..c9b041174 --- /dev/null +++ b/src/lib/md/binance/BinanceTickerUpdate.h @@ -0,0 +1,57 @@ +#pragma once +#include +#include + +namespace bts::md::binance { + +class BinanceTickerUpdate { + public: + // json name: e + std::string event_type; + // json name: E + std::size_t event_time; + // json name: s + std::string symbol; + // json name: p + double price_change; + // json name: P + double price_change_pct; + // json name: w + double weighted_avg; + // json name: x + double win_first_price; + // json name: c + double last_price; + // json name: Q + double last_quantityu; + // json name: b + double best_bid_price; + // json name: B + double best_bid_quantity; + // json name: a + double bet_ask_price; + // json name: A + double best_ask_quantity; + // json name: o + double open_price; + // json name: h + double high_price; + // json name: l + double low_price; + // json name: v + std::size_t base_asset_volume; + // json name: q + std::size_t quote_asset_volume; + // json name: O + std::size_t stat_open_time; + // json name: C + std::size_t stat_close_time; + // json name: F + std::size_t first_trade_id; + // json name: L + std::size_t last_trade_id; + // json name: n + std::size_t total_num_trades; +}; + +} // namespace bts::md::binance