Add classes that mirror binance json structures

This commit is contained in:
John Colvin committed 2021-11-19 16:19:26 -05:00
1 parent 66f7474d62
commit 2e7bdbcac5
8 files changed
+268

No files matched your search

+44
View File
@@ -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<std::string>();
upd.price = std::stod(price_str);
const nlohmann::json &jqty = j.at(1);
const auto &qty_str = jqty.get<std::string>();
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
+23
View File
@@ -0,0 +1,23 @@
#pragma once
#include <cstddef>
#include <nlohmann/json.hpp>
#include <ostream>
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
+46
View File
@@ -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
@@ -0,0 +1,23 @@
#pragma once
#include <cstddef>
#include <string>
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
@@ -0,0 +1,29 @@
#pragma once
#include "BinanceBookLevel.h"
#include <cstddef>
#include <string>
#include <vector>
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<BinanceBookLevel> changed_bids;
// json name: a
std::vector<BinanceBookLevel> changed_asks;
};
} // namespace bts::md::binance
@@ -0,0 +1,29 @@
#pragma once
#include <cstdint>
#include <string>
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
@@ -0,0 +1,17 @@
#pragma once
#include "BinanceBookLevel.h"
#include <vector>
namespace bts::md::binance {
class BinancePartialBookDepthUpdate {
public:
// json name: lastUpdateId
std::size_t last_update_id;
// json name: bids
std::vector<BinanceBookLevel> bids;
// json name: asks
std::vector<BinanceBookLevel> asks;
};
} // namespace bts::md::binance
+57
View File
@@ -0,0 +1,57 @@
#pragma once
#include <cstdint>
#include <string>
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