From b3500800c5de40e35ef97027db47d625815a8d47 Mon Sep 17 00:00:00 2001 From: Stephane Janel Date: Mon, 19 Feb 2024 09:07:36 +0100 Subject: [PATCH] Code cleaning - rename LastTradesVectors into TradesVector --- CMakeLists.txt | 4 +- alpine.Dockerfile | 2 +- .../include/exchangepublicapitypes.hpp | 2 +- src/api-objects/include/order.hpp | 1 - src/api/common/include/exchangepublicapi.hpp | 2 +- .../common/include/exchangepublicapi_mock.hpp | 2 +- .../exchanges/include/binancepublicapi.hpp | 2 +- .../exchanges/include/bithumbpublicapi.hpp | 2 +- src/api/exchanges/include/huobipublicapi.hpp | 2 +- src/api/exchanges/include/krakenpublicapi.hpp | 2 +- src/api/exchanges/include/kucoinpublicapi.hpp | 2 +- src/api/exchanges/include/upbitpublicapi.hpp | 2 +- src/api/exchanges/src/binancepublicapi.cpp | 8 +- src/api/exchanges/src/bithumbpublicapi.cpp | 4 +- src/api/exchanges/src/huobipublicapi.cpp | 4 +- src/api/exchanges/src/krakenpublicapi.cpp | 4 +- src/api/exchanges/src/kucoinpublicapi.cpp | 6 +- src/api/exchanges/src/upbitpublicapi.cpp | 6 +- .../exchanges/test/exchangecommonapi_test.hpp | 2 +- src/api/interface/include/exchange.hpp | 2 +- src/engine/include/coincenter.hpp | 3 +- src/engine/include/exchangesorchestrator.hpp | 2 +- src/engine/include/metricsexporter.hpp | 2 +- src/engine/include/queryresultprinter.hpp | 2 +- src/engine/include/queryresulttypes.hpp | 2 +- src/engine/src/coincenter.cpp | 3 +- src/engine/src/exchangesorchestrator.cpp | 6 +- src/engine/src/metricsexporter.cpp | 2 +- src/engine/src/queryresultprinter.cpp | 4 +- src/engine/test/exchangedata_test.hpp | 62 ++++++------- .../test/queryresultprinter_public_test.cpp | 24 +++--- src/objects/include/market.hpp | 7 +- src/objects/include/marketorderbook.hpp | 2 +- src/objects/include/monetaryamount.hpp | 6 +- src/objects/src/marketorderbook.cpp | 86 ++++++++++--------- src/tech/include/stringhelpers.hpp | 6 +- 36 files changed, 142 insertions(+), 138 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a8d536a0..dc63a7a1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,6 +83,7 @@ if(CCT_ENABLE_TESTS) enable_testing() endif() +# nlohmann_json - coincenter json library find_package(nlohmann_json CONFIG) if(NOT nlohmann_json_FOUND) FetchContent_Declare( @@ -94,6 +95,7 @@ if(NOT nlohmann_json_FOUND) FetchContent_MakeAvailable(nlohmann_json) endif() +# spdlog - coincenter logging library find_package(spdlog CONFIG) if(NOT spdlog_FOUND) FetchContent_Declare( @@ -115,7 +117,7 @@ if(CCT_BUILD_PROMETHEUS_FROM_SRC) FetchContent_Declare( prometheus-cpp GIT_REPOSITORY https://github.com/jupp0r/prometheus-cpp.git - GIT_TAG v1.2.0 + GIT_TAG v1.2.3 ) FetchContent_MakeAvailable(prometheus-cpp) diff --git a/alpine.Dockerfile b/alpine.Dockerfile index f862cc90..b52cab0b 100644 --- a/alpine.Dockerfile +++ b/alpine.Dockerfile @@ -1,5 +1,5 @@ # Multi stage build to separate docker build image from executable (to make the latter smaller) -FROM alpine:3.19.0 AS build +FROM alpine:3.19.1 AS build # Install base & build dependencies, needed certificates for curl to work with https RUN apk add --update --upgrade --no-cache g++ libc-dev curl-dev cmake ninja git ca-certificates diff --git a/src/api-objects/include/exchangepublicapitypes.hpp b/src/api-objects/include/exchangepublicapitypes.hpp index 28fe37ef..f1e28e2f 100644 --- a/src/api-objects/include/exchangepublicapitypes.hpp +++ b/src/api-objects/include/exchangepublicapitypes.hpp @@ -16,5 +16,5 @@ using MarketSet = FlatSet; using MarketOrderBookMap = std::unordered_map; using MarketPriceMap = std::unordered_map; using MarketsPath = SmallVector; -using LastTradesVector = vector; +using TradesVector = vector; } // namespace cct \ No newline at end of file diff --git a/src/api-objects/include/order.hpp b/src/api-objects/include/order.hpp index 6dc689d2..2510148c 100644 --- a/src/api-objects/include/order.hpp +++ b/src/api-objects/include/order.hpp @@ -43,7 +43,6 @@ class Order { Market market() const { return Market(_matchedVolume.currencyCode(), _price.currencyCode()); } - /// default ordering by place time first, then matched volume, etc std::strong_ordering operator<=>(const Order &) const noexcept = default; using trivially_relocatable = is_trivially_relocatable::type; diff --git a/src/api/common/include/exchangepublicapi.hpp b/src/api/common/include/exchangepublicapi.hpp index c72cb8c2..ad445d85 100644 --- a/src/api/common/include/exchangepublicapi.hpp +++ b/src/api/common/include/exchangepublicapi.hpp @@ -96,7 +96,7 @@ class ExchangePublic : public ExchangeBase { virtual MonetaryAmount queryLast24hVolume(Market mk) = 0; /// Retrieve an ordered vector of recent last trades - virtual LastTradesVector queryLastTrades(Market mk, int nbTrades = kNbLastTradesDefault) = 0; + virtual TradesVector queryLastTrades(Market mk, int nbTrades = kNbLastTradesDefault) = 0; /// Retrieve the last price of given market. virtual MonetaryAmount queryLastPrice(Market mk) = 0; diff --git a/src/api/common/include/exchangepublicapi_mock.hpp b/src/api/common/include/exchangepublicapi_mock.hpp index 7856915c..4910b2ac 100644 --- a/src/api/common/include/exchangepublicapi_mock.hpp +++ b/src/api/common/include/exchangepublicapi_mock.hpp @@ -28,7 +28,7 @@ class MockExchangePublic : public ExchangePublic { MOCK_METHOD(MarketOrderBookMap, queryAllApproximatedOrderBooks, (int depth), (override)); MOCK_METHOD(MarketOrderBook, queryOrderBook, (Market mk, int depth), (override)); MOCK_METHOD(MonetaryAmount, queryLast24hVolume, (Market mk), (override)); - MOCK_METHOD(LastTradesVector, queryLastTrades, (Market mk, int nbTrades), (override)); + MOCK_METHOD(TradesVector, queryLastTrades, (Market mk, int nbTrades), (override)); MOCK_METHOD(MonetaryAmount, queryLastPrice, (Market mk), (override)); }; } // namespace cct::api \ No newline at end of file diff --git a/src/api/exchanges/include/binancepublicapi.hpp b/src/api/exchanges/include/binancepublicapi.hpp index 1b8b307b..96b6fbfc 100644 --- a/src/api/exchanges/include/binancepublicapi.hpp +++ b/src/api/exchanges/include/binancepublicapi.hpp @@ -63,7 +63,7 @@ class BinancePublic : public ExchangePublic { MonetaryAmount queryLast24hVolume(Market mk) override { return _tradedVolumeCache.get(mk); } - LastTradesVector queryLastTrades(Market mk, int nbTrades = kNbLastTradesDefault) override; + TradesVector queryLastTrades(Market mk, int nbTrades = kNbLastTradesDefault) override; MonetaryAmount queryLastPrice(Market mk) override { return _tickerCache.get(mk); } diff --git a/src/api/exchanges/include/bithumbpublicapi.hpp b/src/api/exchanges/include/bithumbpublicapi.hpp index 9e354640..a804852c 100644 --- a/src/api/exchanges/include/bithumbpublicapi.hpp +++ b/src/api/exchanges/include/bithumbpublicapi.hpp @@ -59,7 +59,7 @@ class BithumbPublic : public ExchangePublic { MonetaryAmount queryLast24hVolume(Market mk) override { return _tradedVolumeCache.get(mk); } - LastTradesVector queryLastTrades(Market mk, int nbTrades = kNbLastTradesDefault) override; + TradesVector queryLastTrades(Market mk, int nbTrades = kNbLastTradesDefault) override; MonetaryAmount queryLastPrice(Market mk) override; diff --git a/src/api/exchanges/include/huobipublicapi.hpp b/src/api/exchanges/include/huobipublicapi.hpp index e1c7636f..04bceff6 100644 --- a/src/api/exchanges/include/huobipublicapi.hpp +++ b/src/api/exchanges/include/huobipublicapi.hpp @@ -58,7 +58,7 @@ class HuobiPublic : public ExchangePublic { MonetaryAmount queryLast24hVolume(Market mk) override { return _tradedVolumeCache.get(mk); } - LastTradesVector queryLastTrades(Market mk, int nbTrades = kNbLastTradesDefault) override; + TradesVector queryLastTrades(Market mk, int nbTrades = kNbLastTradesDefault) override; MonetaryAmount queryLastPrice(Market mk) override { return _tickerCache.get(mk); } diff --git a/src/api/exchanges/include/krakenpublicapi.hpp b/src/api/exchanges/include/krakenpublicapi.hpp index 637fd2cf..091578e6 100644 --- a/src/api/exchanges/include/krakenpublicapi.hpp +++ b/src/api/exchanges/include/krakenpublicapi.hpp @@ -56,7 +56,7 @@ class KrakenPublic : public ExchangePublic { MonetaryAmount queryLast24hVolume(Market mk) override { return _tickerCache.get(mk).first; } - LastTradesVector queryLastTrades(Market mk, int nbLastTrades = kNbLastTradesDefault) override; + TradesVector queryLastTrades(Market mk, int nbLastTrades = kNbLastTradesDefault) override; MonetaryAmount queryLastPrice(Market mk) override { return _tickerCache.get(mk).second; } diff --git a/src/api/exchanges/include/kucoinpublicapi.hpp b/src/api/exchanges/include/kucoinpublicapi.hpp index f3b03b7a..34975510 100644 --- a/src/api/exchanges/include/kucoinpublicapi.hpp +++ b/src/api/exchanges/include/kucoinpublicapi.hpp @@ -56,7 +56,7 @@ class KucoinPublic : public ExchangePublic { MonetaryAmount queryLast24hVolume(Market mk) override { return _tradedVolumeCache.get(mk); } - LastTradesVector queryLastTrades(Market mk, int nbTrades = kNbLastTradesDefault) override; + TradesVector queryLastTrades(Market mk, int nbTrades = kNbLastTradesDefault) override; MonetaryAmount queryLastPrice(Market mk) override { return _tickerCache.get(mk); } diff --git a/src/api/exchanges/include/upbitpublicapi.hpp b/src/api/exchanges/include/upbitpublicapi.hpp index 28c59ec8..39d924fe 100644 --- a/src/api/exchanges/include/upbitpublicapi.hpp +++ b/src/api/exchanges/include/upbitpublicapi.hpp @@ -50,7 +50,7 @@ class UpbitPublic : public ExchangePublic { MonetaryAmount queryLast24hVolume(Market mk) override { return _tradedVolumeCache.get(mk); } - LastTradesVector queryLastTrades(Market mk, int nbTrades = kNbLastTradesDefault) override; + TradesVector queryLastTrades(Market mk, int nbTrades = kNbLastTradesDefault) override; MonetaryAmount queryLastPrice(Market mk) override { return _tickerCache.get(mk); } diff --git a/src/api/exchanges/src/binancepublicapi.cpp b/src/api/exchanges/src/binancepublicapi.cpp index 3dd08486..4f189340 100644 --- a/src/api/exchanges/src/binancepublicapi.cpp +++ b/src/api/exchanges/src/binancepublicapi.cpp @@ -319,7 +319,7 @@ MonetaryAmount BinancePublic::sanitizePrice(Market mk, MonetaryAmount pri) { MonetaryAmount BinancePublic::computePriceForNotional(Market mk, int avgPriceMins) { if (avgPriceMins == 0) { // price should be the last matched price - LastTradesVector lastTrades = queryLastTrades(mk, 1); + TradesVector lastTrades = queryLastTrades(mk, 1); if (!lastTrades.empty()) { return lastTrades.front().price(); } @@ -517,7 +517,7 @@ MonetaryAmount BinancePublic::TradedVolumeFunc::operator()(Market mk) { return {last24hVol, mk.base()}; } -LastTradesVector BinancePublic::queryLastTrades(Market mk, int nbTrades) { +TradesVector BinancePublic::queryLastTrades(Market mk, int nbTrades) { if (nbTrades > kMaxNbLastTrades) { log::warn("{} is larger than maximum number of last trades of {} on {}", nbTrades, kMaxNbLastTrades, _name); nbTrades = kMaxNbLastTrades; @@ -525,8 +525,8 @@ LastTradesVector BinancePublic::queryLastTrades(Market mk, int nbTrades) { json result = PublicQuery(_commonInfo._curlHandle, "/api/v3/trades", {{"symbol", mk.assetsPairStrUpper()}, {"limit", nbTrades}}); - LastTradesVector ret; - ret.reserve(static_cast(result.size())); + TradesVector ret; + ret.reserve(static_cast(result.size())); for (const json& detail : result) { MonetaryAmount amount(detail["qty"].get(), mk.base()); MonetaryAmount price(detail["price"].get(), mk.quote()); diff --git a/src/api/exchanges/src/bithumbpublicapi.cpp b/src/api/exchanges/src/bithumbpublicapi.cpp index c354becc..888681f2 100644 --- a/src/api/exchanges/src/bithumbpublicapi.cpp +++ b/src/api/exchanges/src/bithumbpublicapi.cpp @@ -301,9 +301,9 @@ TimePoint EpochTime(std::string&& dateStr) { } } // namespace -LastTradesVector BithumbPublic::queryLastTrades(Market mk, [[maybe_unused]] int nbTrades) { +TradesVector BithumbPublic::queryLastTrades(Market mk, [[maybe_unused]] int nbTrades) { json result = PublicQuery(_curlHandle, "/public/transaction_history/", mk.base(), mk.quote()); - LastTradesVector ret; + TradesVector ret; ret.reserve(result.size()); for (const json& detail : result) { MonetaryAmount amount(detail["units_traded"].get(), mk.base()); diff --git a/src/api/exchanges/src/huobipublicapi.cpp b/src/api/exchanges/src/huobipublicapi.cpp index ccd55ab7..e68efe87 100644 --- a/src/api/exchanges/src/huobipublicapi.cpp +++ b/src/api/exchanges/src/huobipublicapi.cpp @@ -458,12 +458,12 @@ MonetaryAmount HuobiPublic::TradedVolumeFunc::operator()(Market mk) { return MonetaryAmount(last24hVol, mk.base()); } -LastTradesVector HuobiPublic::queryLastTrades(Market mk, int nbTrades) { +TradesVector HuobiPublic::queryLastTrades(Market mk, int nbTrades) { nbTrades = std::min(nbTrades, 2000); // max authorized nbTrades = std::max(nbTrades, 1); // min authorized json result = PublicQuery(_curlHandle, "/market/history/trade", {{"symbol", mk.assetsPairStrLower()}, {"size", nbTrades}}); - LastTradesVector ret; + TradesVector ret; for (const json& detail : result) { auto dataDetails = detail.find("data"); if (dataDetails != detail.end()) { diff --git a/src/api/exchanges/src/krakenpublicapi.cpp b/src/api/exchanges/src/krakenpublicapi.cpp index 2a0fc9f6..79cd685a 100644 --- a/src/api/exchanges/src/krakenpublicapi.cpp +++ b/src/api/exchanges/src/krakenpublicapi.cpp @@ -338,10 +338,10 @@ KrakenPublic::TickerFunc::Last24hTradedVolumeAndLatestPricePair KrakenPublic::Ti throw exception("Invalid data retrieved from ticker information"); } -LastTradesVector KrakenPublic::queryLastTrades(Market mk, int nbLastTrades) { +TradesVector KrakenPublic::queryLastTrades(Market mk, int nbLastTrades) { Market krakenMarket(_tradableCurrenciesCache.get().getOrThrow(mk.base()).altCode(), _tradableCurrenciesCache.get().getOrThrow(mk.quote()).altCode()); - LastTradesVector ret; + TradesVector ret; json result = PublicQuery(_curlHandle, "/public/Trades", {{"pair", krakenMarket.assetsPairStrUpper()}, {"count", nbLastTrades}}); if (!result.empty()) { diff --git a/src/api/exchanges/src/kucoinpublicapi.cpp b/src/api/exchanges/src/kucoinpublicapi.cpp index ae7ba651..3ba25a50 100644 --- a/src/api/exchanges/src/kucoinpublicapi.cpp +++ b/src/api/exchanges/src/kucoinpublicapi.cpp @@ -348,10 +348,10 @@ MonetaryAmount KucoinPublic::TradedVolumeFunc::operator()(Market mk) { return {amountStr, mk.base()}; } -LastTradesVector KucoinPublic::queryLastTrades(Market mk, [[maybe_unused]] int nbTrades) { +TradesVector KucoinPublic::queryLastTrades(Market mk, [[maybe_unused]] int nbTrades) { json result = PublicQuery(_curlHandle, "/api/v1/market/histories", GetSymbolPostData(mk)); - LastTradesVector ret; - ret.reserve(static_cast(result.size())); + TradesVector ret; + ret.reserve(static_cast(result.size())); for (const json& detail : result) { MonetaryAmount amount(detail["size"].get(), mk.base()); MonetaryAmount price(detail["price"].get(), mk.quote()); diff --git a/src/api/exchanges/src/upbitpublicapi.cpp b/src/api/exchanges/src/upbitpublicapi.cpp index 98b0ca56..36cb8067 100644 --- a/src/api/exchanges/src/upbitpublicapi.cpp +++ b/src/api/exchanges/src/upbitpublicapi.cpp @@ -248,10 +248,10 @@ MonetaryAmount UpbitPublic::TradedVolumeFunc::operator()(Market mk) { return MonetaryAmount(last24hVol, mk.base()); } -LastTradesVector UpbitPublic::queryLastTrades(Market mk, int nbTrades) { +TradesVector UpbitPublic::queryLastTrades(Market mk, int nbTrades) { json result = PublicQuery(_curlHandle, "/v1/trades/ticks", {{"count", nbTrades}, {"market", ReverseMarketStr(mk)}}); - LastTradesVector ret; - ret.reserve(static_cast(result.size())); + TradesVector ret; + ret.reserve(static_cast(result.size())); for (const json& detail : result) { MonetaryAmount amount(detail["trade_volume"].get(), mk.base()); MonetaryAmount price(detail["trade_price"].get(), mk.quote()); diff --git a/src/api/exchanges/test/exchangecommonapi_test.hpp b/src/api/exchanges/test/exchangecommonapi_test.hpp index cd25877c..c0fe6e91 100644 --- a/src/api/exchanges/test/exchangecommonapi_test.hpp +++ b/src/api/exchanges/test/exchangecommonapi_test.hpp @@ -270,7 +270,7 @@ class TestAPI { } if (!sampleMarkets.empty()) { Market mk = sampleMarkets.front(); - LastTradesVector lastTrades = exchangePublic.queryLastTrades(mk); + TradesVector lastTrades = exchangePublic.queryLastTrades(mk); if (!lastTrades.empty() && exchangePrivateOpt) { auto compareTradedVolume = [](const PublicTrade &lhs, const PublicTrade &rhs) { return lhs.amount() < rhs.amount(); diff --git a/src/api/interface/include/exchange.hpp b/src/api/interface/include/exchange.hpp index 9d561f78..ba5510dd 100644 --- a/src/api/interface/include/exchange.hpp +++ b/src/api/interface/include/exchange.hpp @@ -93,7 +93,7 @@ class Exchange { MonetaryAmount queryLast24hVolume(Market mk) { return _exchangePublic.queryLast24hVolume(mk); } /// Retrieve an ordered vector of recent last trades - LastTradesVector queryLastTrades(Market mk, int nbTrades = ExchangePublic::kNbLastTradesDefault) { + TradesVector queryLastTrades(Market mk, int nbTrades = ExchangePublic::kNbLastTradesDefault) { return _exchangePublic.queryLastTrades(mk, nbTrades); } diff --git a/src/engine/include/coincenter.hpp b/src/engine/include/coincenter.hpp index 72d462c6..34ff2248 100644 --- a/src/engine/include/coincenter.hpp +++ b/src/engine/include/coincenter.hpp @@ -53,7 +53,7 @@ class Coincenter { MonetaryAmountPerExchange getLast24hTradedVolumePerExchange(Market mk, ExchangeNameSpan exchangeNames); /// Retrieve the last trades for each queried exchange - LastTradesPerExchange getLastTradesPerExchange(Market mk, ExchangeNameSpan exchangeNames, int nbLastTrades); + TradesPerExchange getLastTradesPerExchange(Market mk, ExchangeNameSpan exchangeNames, int nbLastTrades); /// Retrieve the last price for exchanges supporting given market. MonetaryAmountPerExchange getLastPricePerExchange(Market mk, ExchangeNameSpan exchangeNames); @@ -69,6 +69,7 @@ class Coincenter { BalancePerExchange getBalance(std::span privateExchangeNames, const BalanceOptions &balanceOptions); + /// Get deposit information for given accounts WalletPerExchange getDepositInfo(std::span privateExchangeNames, CurrencyCode depositCurrency); /// Get opened orders on given list of exchanges following given order constraints diff --git a/src/engine/include/exchangesorchestrator.hpp b/src/engine/include/exchangesorchestrator.hpp index 4b9efaaa..e1753af5 100644 --- a/src/engine/include/exchangesorchestrator.hpp +++ b/src/engine/include/exchangesorchestrator.hpp @@ -75,7 +75,7 @@ class ExchangesOrchestrator { MonetaryAmountPerExchange getLast24hTradedVolumePerExchange(Market mk, ExchangeNameSpan exchangeNames); - LastTradesPerExchange getLastTradesPerExchange(Market mk, ExchangeNameSpan exchangeNames, int nbLastTrades); + TradesPerExchange getLastTradesPerExchange(Market mk, ExchangeNameSpan exchangeNames, int nbLastTrades); MonetaryAmountPerExchange getLastPricePerExchange(Market mk, ExchangeNameSpan exchangeNames); diff --git a/src/engine/include/metricsexporter.hpp b/src/engine/include/metricsexporter.hpp index dbb6a7c6..d3679ab8 100644 --- a/src/engine/include/metricsexporter.hpp +++ b/src/engine/include/metricsexporter.hpp @@ -20,7 +20,7 @@ class MetricsExporter { void exportOrderbookMetrics(Market mk, const MarketOrderBookConversionRates &marketOrderBookConversionRates); - void exportLastTradesMetrics(Market mk, const LastTradesPerExchange &lastTradesPerExchange); + void exportLastTradesMetrics(Market mk, const TradesPerExchange &lastTradesPerExchange); private: void createSummariesAndHistograms(); diff --git a/src/engine/include/queryresultprinter.hpp b/src/engine/include/queryresultprinter.hpp index 88c63fac..12211eb6 100644 --- a/src/engine/include/queryresultprinter.hpp +++ b/src/engine/include/queryresultprinter.hpp @@ -81,7 +81,7 @@ class QueryResultPrinter { void printLast24hTradedVolume(Market mk, const MonetaryAmountPerExchange &tradedVolumePerExchange) const; - void printLastTrades(Market mk, int nbLastTrades, const LastTradesPerExchange &lastTradesPerExchange) const; + void printLastTrades(Market mk, int nbLastTrades, const TradesPerExchange &lastTradesPerExchange) const; void printLastPrice(Market mk, const MonetaryAmountPerExchange &pricePerExchange) const; diff --git a/src/engine/include/queryresulttypes.hpp b/src/engine/include/queryresulttypes.hpp index 345fa070..482831a2 100644 --- a/src/engine/include/queryresulttypes.hpp +++ b/src/engine/include/queryresulttypes.hpp @@ -37,7 +37,7 @@ using MonetaryAmountPerExchange = FixedCapacityVector, kNbSupportedExchanges>; -using LastTradesPerExchange = FixedCapacityVector, kNbSupportedExchanges>; +using TradesPerExchange = FixedCapacityVector, kNbSupportedExchanges>; using TradeResultPerExchange = SmallVector, kTypicalNbPrivateAccounts>; diff --git a/src/engine/src/coincenter.cpp b/src/engine/src/coincenter.cpp index 4e5c7ec3..efc6f163 100644 --- a/src/engine/src/coincenter.cpp +++ b/src/engine/src/coincenter.cpp @@ -351,8 +351,7 @@ MonetaryAmountPerExchange Coincenter::getLast24hTradedVolumePerExchange(Market m return _exchangesOrchestrator.getLast24hTradedVolumePerExchange(mk, exchangeNames); } -LastTradesPerExchange Coincenter::getLastTradesPerExchange(Market mk, ExchangeNameSpan exchangeNames, - int nbLastTrades) { +TradesPerExchange Coincenter::getLastTradesPerExchange(Market mk, ExchangeNameSpan exchangeNames, int nbLastTrades) { const auto ret = _exchangesOrchestrator.getLastTradesPerExchange(mk, exchangeNames, nbLastTrades); _metricsExporter.exportLastTradesMetrics(mk, ret); diff --git a/src/engine/src/exchangesorchestrator.cpp b/src/engine/src/exchangesorchestrator.cpp index f963c7ea..7aea0d7d 100644 --- a/src/engine/src/exchangesorchestrator.cpp +++ b/src/engine/src/exchangesorchestrator.cpp @@ -840,13 +840,13 @@ MonetaryAmountPerExchange ExchangesOrchestrator::getLast24hTradedVolumePerExchan return tradedVolumePerExchange; } -LastTradesPerExchange ExchangesOrchestrator::getLastTradesPerExchange(Market mk, ExchangeNameSpan exchangeNames, - int nbLastTrades) { +TradesPerExchange ExchangesOrchestrator::getLastTradesPerExchange(Market mk, ExchangeNameSpan exchangeNames, + int nbLastTrades) { log::info("Query {} last trades on {} volume from {}", nbLastTrades, mk, ConstructAccumulatedExchangeNames(exchangeNames)); UniquePublicSelectedExchanges selectedExchanges = getExchangesTradingMarket(mk, exchangeNames); - LastTradesPerExchange ret(selectedExchanges.size()); + TradesPerExchange ret(selectedExchanges.size()); _threadPool.parallelTransform( selectedExchanges.begin(), selectedExchanges.end(), ret.begin(), [mk, nbLastTrades](Exchange *exchange) { return std::make_pair(static_cast(exchange), exchange->queryLastTrades(mk, nbLastTrades)); diff --git a/src/engine/src/metricsexporter.cpp b/src/engine/src/metricsexporter.cpp index b83e762c..90d56b41 100644 --- a/src/engine/src/metricsexporter.cpp +++ b/src/engine/src/metricsexporter.cpp @@ -116,7 +116,7 @@ void MetricsExporter::exportOrderbookMetrics(Market mk, } } -void MetricsExporter::exportLastTradesMetrics(Market mk, const LastTradesPerExchange &lastTradesPerExchange) { +void MetricsExporter::exportLastTradesMetrics(Market mk, const TradesPerExchange &lastTradesPerExchange) { RETURN_IF_NO_MONITORING; MetricKey key = CreateMetricKey("", "All public trades that occurred on the market"); string marketLowerCase = mk.assetsPairStrLower('-'); diff --git a/src/engine/src/queryresultprinter.cpp b/src/engine/src/queryresultprinter.cpp index f4ce97c7..f256a184 100644 --- a/src/engine/src/queryresultprinter.cpp +++ b/src/engine/src/queryresultprinter.cpp @@ -545,7 +545,7 @@ json Last24hTradedVolumeJson(Market mk, const MonetaryAmountPerExchange &tradedV return ToJson(CoincenterCommandType::kLast24hTradedVolume, std::move(in), std::move(out)); } -json LastTradesJson(Market mk, int nbLastTrades, const LastTradesPerExchange &lastTradesPerExchange) { +json LastTradesJson(Market mk, int nbLastTrades, const TradesPerExchange &lastTradesPerExchange) { json in; json inOpt; inOpt.emplace("market", mk.str()); @@ -1115,7 +1115,7 @@ void QueryResultPrinter::printLast24hTradedVolume(Market mk, } void QueryResultPrinter::printLastTrades(Market mk, int nbLastTrades, - const LastTradesPerExchange &lastTradesPerExchange) const { + const TradesPerExchange &lastTradesPerExchange) const { json jsonData = LastTradesJson(mk, nbLastTrades, lastTradesPerExchange); switch (_apiOutputType) { case ApiOutputType::kFormattedTable: { diff --git a/src/engine/test/exchangedata_test.hpp b/src/engine/test/exchangedata_test.hpp index 3cd5baa6..1fce20cd 100644 --- a/src/engine/test/exchangedata_test.hpp +++ b/src/engine/test/exchangedata_test.hpp @@ -50,21 +50,20 @@ class ExchangesBaseTest : public ::testing::Test { Exchange exchange7{coincenterInfo.exchangeConfig(exchangePublic3.name()), exchangePublic3, exchangePrivate7}; Exchange exchange8{coincenterInfo.exchangeConfig(exchangePublic1.name()), exchangePublic1, exchangePrivate8}; - Market m1{"ETH", "EUR"}; - Market m2{"BTC", "EUR"}; - Market m3{"XRP", "BTC"}; + static constexpr Market m1{"ETH", "EUR"}; + static constexpr Market m2{"BTC", "EUR"}; + static constexpr Market m3{"XRP", "BTC"}; - VolAndPriNbDecimals volAndPriDec1{2, 2}; - int depth = 10; - int64_t nbSecondsSinceEpoch = 0; + static constexpr VolAndPriNbDecimals volAndPriDec1{2, 2}; + static constexpr int depth = 10; - MonetaryAmount askPrice1{"2300.45EUR"}; - MonetaryAmount bidPrice1{"2300.4EUR"}; + static constexpr MonetaryAmount askPrice1{230045, "EUR", 2}; + static constexpr MonetaryAmount bidPrice1{23004, "EUR", 1}; TimePoint time{}; MarketOrderBook marketOrderBook10{ - time, askPrice1, MonetaryAmount("1.09ETH"), bidPrice1, MonetaryAmount("41ETH"), volAndPriDec1, depth}; + time, askPrice1, MonetaryAmount(109, "ETH", 2), bidPrice1, MonetaryAmount(41, "ETH"), volAndPriDec1, depth}; MarketOrderBook marketOrderBook11{time, MonetaryAmount{"2301.15EUR"}, MonetaryAmount("0.4ETH"), @@ -73,34 +72,35 @@ class ExchangesBaseTest : public ::testing::Test { volAndPriDec1, depth - 2}; - VolAndPriNbDecimals volAndPriDec2{5, 2}; - MonetaryAmount askPrice2{"31056.67 EUR"}; - MonetaryAmount bidPrice2{"31056.66 EUR"}; + static constexpr VolAndPriNbDecimals volAndPriDec2{5, 2}; + static constexpr MonetaryAmount askPrice2{3105667, "EUR", 2}; + static constexpr MonetaryAmount bidPrice2{3105666, "EUR", 2}; MarketOrderBook marketOrderBook20{ - time, askPrice2, MonetaryAmount("0.12BTC"), bidPrice2, MonetaryAmount("0.00234 BTC"), volAndPriDec2, depth}; + time, askPrice2, MonetaryAmount(12, "BTC", 2), bidPrice2, MonetaryAmount(234, "BTC", 5), volAndPriDec2, depth}; MarketOrderBook marketOrderBook21{time, - MonetaryAmount{"31051.02 EUR"}, - MonetaryAmount("0.409BTC"), - MonetaryAmount{"31051.01 EUR"}, - MonetaryAmount("1.9087 BTC"), + MonetaryAmount{3105102, "EUR", 2}, + MonetaryAmount(409, "BTC", 3), + MonetaryAmount{3105101, "EUR", 2}, + MonetaryAmount(19087, "BTC", 4), volAndPriDec2, depth + 1}; - VolAndPriNbDecimals volAndPriDec3{1, 2}; - MonetaryAmount askPrice3{"0.37 BTC"}; - MonetaryAmount bidPrice3{"0.36 BTC"}; + static constexpr VolAndPriNbDecimals volAndPriDec3{1, 2}; + static constexpr MonetaryAmount askPrice3{37, "BTC", 2}; + static constexpr MonetaryAmount bidPrice3{36, "BTC", 2}; MarketOrderBook marketOrderBook3{ - time, askPrice3, MonetaryAmount("916.4XRP"), bidPrice3, MonetaryAmount("3494XRP"), volAndPriDec3, depth}; + time, askPrice3, MonetaryAmount(9164, "XRP", 1), bidPrice3, MonetaryAmount(3494, "XRP"), volAndPriDec3, depth}; - const MonetaryAmount amounts1[4] = {MonetaryAmount("1500XRP"), MonetaryAmount("15BTC"), MonetaryAmount("1.5ETH"), - MonetaryAmount("5000USDT")}; - const MonetaryAmount amounts2[4] = {MonetaryAmount("37SOL"), MonetaryAmount("1887565SHIB"), MonetaryAmount("0.5BTC"), - MonetaryAmount("6750USDT")}; - const MonetaryAmount amounts3[5] = {MonetaryAmount("0.6ETH"), MonetaryAmount("1000XLM"), MonetaryAmount("0.01AVAX"), - MonetaryAmount("1500EUR"), MonetaryAmount("4250USDT")}; - const MonetaryAmount amounts4[6] = {MonetaryAmount("147ADA"), MonetaryAmount("4.76DOT"), - MonetaryAmount("15004MATIC"), MonetaryAmount("155USD"), - MonetaryAmount("107.5USDT"), MonetaryAmount("1200EUR")}; + static constexpr MonetaryAmount amounts1[] = {MonetaryAmount(1500, "XRP"), MonetaryAmount(15, "BTC"), + MonetaryAmount(15, "ETH", 1), MonetaryAmount(5000, "USDT")}; + static constexpr MonetaryAmount amounts2[] = {MonetaryAmount(37, "SOL"), MonetaryAmount(1887565, "SHIB"), + MonetaryAmount(5, "BTC", 1), MonetaryAmount(6750, "USDT")}; + static constexpr MonetaryAmount amounts3[] = {MonetaryAmount(6, "ETH", 1), MonetaryAmount(1000, "XLM"), + MonetaryAmount(1, "AVAX", 2), MonetaryAmount(1500, "EUR"), + MonetaryAmount(4250, "USDT")}; + static constexpr MonetaryAmount amounts4[] = {MonetaryAmount(147, "ADA"), MonetaryAmount(476, "DOT", 2), + MonetaryAmount(15004, "MATIC"), MonetaryAmount(155, "USD"), + MonetaryAmount(1075, "USDT", 1), MonetaryAmount(1200, "EUR")}; BalancePortfolio balancePortfolio1{amounts1}; BalancePortfolio balancePortfolio2{amounts2}; @@ -108,4 +108,4 @@ class ExchangesBaseTest : public ::testing::Test { BalancePortfolio balancePortfolio4{amounts4}; BalancePortfolio emptyBalance; }; -} // namespace cct \ No newline at end of file +} // namespace cct diff --git a/src/engine/test/queryresultprinter_public_test.cpp b/src/engine/test/queryresultprinter_public_test.cpp index e73af89c..dac8af9a 100644 --- a/src/engine/test/queryresultprinter_public_test.cpp +++ b/src/engine/test/queryresultprinter_public_test.cpp @@ -796,21 +796,18 @@ class QueryResultPrinterLastTradesVolumeTest : public QueryResultPrinterTest { protected: Market marketLastTrades{"ETH", "USDT"}; int nbLastTrades = 3; - LastTradesPerExchange lastTradesPerExchange{ + TradesPerExchange lastTradesPerExchange{ {&exchange1, - LastTradesVector{ - PublicTrade(TradeSide::kBuy, MonetaryAmount{"0.13", "ETH"}, MonetaryAmount{"1500.5", "USDT"}, tp1), - PublicTrade(TradeSide::kSell, MonetaryAmount{"3.7", "ETH"}, MonetaryAmount{"1500.5", "USDT"}, tp2), - PublicTrade(TradeSide::kBuy, MonetaryAmount{"0.004", "ETH"}, MonetaryAmount{1501, "USDT"}, tp3)}}, + TradesVector{PublicTrade(TradeSide::kBuy, MonetaryAmount{"0.13", "ETH"}, MonetaryAmount{"1500.5", "USDT"}, tp1), + PublicTrade(TradeSide::kSell, MonetaryAmount{"3.7", "ETH"}, MonetaryAmount{"1500.5", "USDT"}, tp2), + PublicTrade(TradeSide::kBuy, MonetaryAmount{"0.004", "ETH"}, MonetaryAmount{1501, "USDT"}, tp3)}}, {&exchange3, - LastTradesVector{ - PublicTrade(TradeSide::kSell, MonetaryAmount{"0.13", "ETH"}, MonetaryAmount{"1500.5", "USDT"}, tp4), - PublicTrade(TradeSide::kBuy, MonetaryAmount{"0.004", "ETH"}, MonetaryAmount{1501, "USDT"}, tp2)}}, + TradesVector{PublicTrade(TradeSide::kSell, MonetaryAmount{"0.13", "ETH"}, MonetaryAmount{"1500.5", "USDT"}, tp4), + PublicTrade(TradeSide::kBuy, MonetaryAmount{"0.004", "ETH"}, MonetaryAmount{1501, "USDT"}, tp2)}}, {&exchange2, - LastTradesVector{ - PublicTrade(TradeSide::kSell, MonetaryAmount{"0.13", "ETH"}, MonetaryAmount{"1500.5", "USDT"}, tp4), - PublicTrade(TradeSide::kBuy, MonetaryAmount{"0.004", "ETH"}, MonetaryAmount{1501, "USDT"}, tp2), - PublicTrade(TradeSide::kBuy, MonetaryAmount{"47.78", "ETH"}, MonetaryAmount{1498, "USDT"}, tp1)}}}; + TradesVector{PublicTrade(TradeSide::kSell, MonetaryAmount{"0.13", "ETH"}, MonetaryAmount{"1500.5", "USDT"}, tp4), + PublicTrade(TradeSide::kBuy, MonetaryAmount{"0.004", "ETH"}, MonetaryAmount{1501, "USDT"}, tp2), + PublicTrade(TradeSide::kBuy, MonetaryAmount{"47.78", "ETH"}, MonetaryAmount{1498, "USDT"}, tp1)}}}; }; TEST_F(QueryResultPrinterLastTradesVolumeTest, FormattedTable) { @@ -848,8 +845,7 @@ TEST_F(QueryResultPrinterLastTradesVolumeTest, FormattedTable) { } TEST_F(QueryResultPrinterLastTradesVolumeTest, EmptyJson) { - basicQueryResultPrinter(ApiOutputType::kJson) - .printLastTrades(marketLastTrades, nbLastTrades, LastTradesPerExchange{}); + basicQueryResultPrinter(ApiOutputType::kJson).printLastTrades(marketLastTrades, nbLastTrades, TradesPerExchange{}); static constexpr std::string_view kExpected = R"( { "in": { diff --git a/src/objects/include/market.hpp b/src/objects/include/market.hpp index f4d3c007..2e88165d 100644 --- a/src/objects/include/market.hpp +++ b/src/objects/include/market.hpp @@ -17,9 +17,10 @@ class Market { public: enum class Type : int8_t { kRegularExchangeMarket, kFiatConversionMarket }; - Market() noexcept(std::is_nothrow_default_constructible_v) = default; + constexpr Market() noexcept(std::is_nothrow_default_constructible_v) = default; - Market(CurrencyCode first, CurrencyCode second, Type type = Type::kRegularExchangeMarket) : _assets({first, second}) { + constexpr Market(CurrencyCode first, CurrencyCode second, Type type = Type::kRegularExchangeMarket) + : _assets({first, second}) { setType(type); } @@ -68,7 +69,7 @@ class Market { private: string assetsPairStr(char sep, bool lowerCase) const; - void setType(Type type) { _assets[0].uncheckedSetAdditionalBits(static_cast(type)); } + constexpr void setType(Type type) { _assets[0].uncheckedSetAdditionalBits(static_cast(type)); } std::array _assets; }; diff --git a/src/objects/include/marketorderbook.hpp b/src/objects/include/marketorderbook.hpp index b25d7d21..9a065be2 100644 --- a/src/objects/include/marketorderbook.hpp +++ b/src/objects/include/marketorderbook.hpp @@ -46,7 +46,7 @@ class MarketOrderBook { MonetaryAmount price; }; - using AmountPerPriceVec = SmallVector; + using AmountPerPriceVec = SmallVector; /// Constructs an empty MarketOrderBook MarketOrderBook() noexcept = default; diff --git a/src/objects/include/monetaryamount.hpp b/src/objects/include/monetaryamount.hpp index 1285600d..084a3a32 100644 --- a/src/objects/include/monetaryamount.hpp +++ b/src/objects/include/monetaryamount.hpp @@ -100,9 +100,13 @@ class MonetaryAmount { setNbDecimals(monetaryAmount.nbDecimals()); } + /// Get an integral representation of this MonetaryAmount multiplied by current number of decimals. + /// Example: "5.6235" with 6 decimals will return 5623500 + [[nodiscard]] AmountType amount() const { return _amount; } + /// Get an integral representation of this MonetaryAmount multiplied by given number of decimals. /// If an overflow would occur for the resulting amount, return std::nullopt - /// Example : "5.6235" with 6 decimals will return 5623500 + /// Example: "5.6235" with 6 decimals will return 5623500 [[nodiscard]] std::optional amount(int8_t nbDecimals) const; /// Get the integer part of the amount of this MonetaryAmount. diff --git a/src/objects/src/marketorderbook.cpp b/src/objects/src/marketorderbook.cpp index 5ba5609a..93918c4a 100644 --- a/src/objects/src/marketorderbook.cpp +++ b/src/objects/src/marketorderbook.cpp @@ -213,15 +213,15 @@ std::optional MarketOrderBook::computeMaxPriceAtWhichAmountWould throw exception("Given amount should be in the base currency of this market"); } AmountType integralAmountRep = 0; - const std::optional integralTotalAmountToBuyOpt = ma.amount(_volAndPriNbDecimals.volNbDecimals); - if (!integralTotalAmountToBuyOpt) { + const std::optional integralTotalAmountOpt = ma.amount(_volAndPriNbDecimals.volNbDecimals); + if (!integralTotalAmountOpt) { return std::nullopt; } - const AmountType integralTotalAmountToBuy = *integralTotalAmountToBuyOpt; + const AmountType integralTotalAmount = *integralTotalAmountOpt; const int nbOrders = _orders.size(); - for (int pos = _highestBidPricePos + 1; pos < nbOrders && integralAmountRep <= integralTotalAmountToBuy; ++pos) { + for (int pos = _highestBidPricePos + 1; pos < nbOrders && integralAmountRep <= integralTotalAmount; ++pos) { integralAmountRep -= _orders[pos].amount; // -= because amount is < 0 here - if (integralAmountRep >= integralTotalAmountToBuy) { + if (integralAmountRep >= integralTotalAmount) { return priceAt(pos); } } @@ -234,23 +234,23 @@ MarketOrderBook::AmountPerPriceVec MarketOrderBook::computePricesAtWhichAmountWo throw exception("Given amount should be in the base currency of this market"); } AmountType integralAmountRep = 0; - const std::optional integralTotalAmountToBuyOpt = ma.amount(_volAndPriNbDecimals.volNbDecimals); + const std::optional integralTotalAmountOpt = ma.amount(_volAndPriNbDecimals.volNbDecimals); AmountPerPriceVec ret; - if (!integralTotalAmountToBuyOpt) { + if (!integralTotalAmountOpt) { log::warn("Not enough amount to buy {} on market {}", ma, _market); return ret; } - const AmountType integralTotalAmountToBuy = *integralTotalAmountToBuyOpt; + const AmountType integralTotalAmount = *integralTotalAmountOpt; const int nbOrders = _orders.size(); for (int pos = _highestBidPricePos + 1; pos < nbOrders; ++pos) { MonetaryAmount price(_orders[pos].price, _market.quote(), _volAndPriNbDecimals.priNbDecimals); if (_orders[pos].amount != 0) { - if (integralTotalAmountToBuy - integralAmountRep <= -_orders[pos].amount) { - ret.emplace_back(MonetaryAmount(integralTotalAmountToBuy - integralAmountRep, _market.base(), - _volAndPriNbDecimals.volNbDecimals), - price); + if (integralTotalAmount - integralAmountRep <= -_orders[pos].amount) { + ret.emplace_back( + MonetaryAmount(integralTotalAmount - integralAmountRep, _market.base(), _volAndPriNbDecimals.volNbDecimals), + price); return ret; } integralAmountRep -= _orders[pos].amount; // -= because amount is < 0 here @@ -278,8 +278,7 @@ inline std::optional ComputeAvgPrice(Market mk, ret += amountAtPrice.amount.toNeutral() * amountAtPrice.price; totalAmount += amountAtPrice.amount; } - ret /= totalAmount.toNeutral(); - return ret; + return ret / totalAmount.toNeutral(); } } // namespace @@ -294,16 +293,15 @@ std::optional MarketOrderBook::computeMinPriceAtWhichAmountWould throw exception("Given amount should be in the base currency of this market"); } AmountType integralAmountRep = 0; - const std::optional integralTotalAmountToBuyOpt = ma.amount(_volAndPriNbDecimals.volNbDecimals); - AmountPerPriceVec ret; - if (!integralTotalAmountToBuyOpt) { + const std::optional integralTotalAmountOpt = ma.amount(_volAndPriNbDecimals.volNbDecimals); + if (!integralTotalAmountOpt) { return std::nullopt; } - const AmountType integralTotalAmountToBuy = *integralTotalAmountToBuyOpt; + const AmountType integralTotalAmount = *integralTotalAmountOpt; - for (int pos = _lowestAskPricePos - 1; pos >= 0 && integralAmountRep <= integralTotalAmountToBuy; --pos) { + for (int pos = _lowestAskPricePos - 1; pos >= 0 && integralAmountRep <= integralTotalAmount; --pos) { integralAmountRep += _orders[pos].amount; - if (integralAmountRep >= integralTotalAmountToBuy) { + if (integralAmountRep >= integralTotalAmount) { return priceAt(pos); } } @@ -316,21 +314,21 @@ MarketOrderBook::AmountPerPriceVec MarketOrderBook::computePricesAtWhichAmountWo throw exception("Given amount should be in the base currency of this market"); } AmountType integralAmountRep = 0; - const std::optional integralTotalAmountToBuyOpt = ma.amount(_volAndPriNbDecimals.volNbDecimals); + const std::optional integralTotalAmountOpt = ma.amount(_volAndPriNbDecimals.volNbDecimals); AmountPerPriceVec ret; - if (!integralTotalAmountToBuyOpt) { + if (!integralTotalAmountOpt) { log::debug("Not enough amount to sell {} on market {}", ma, _market); return ret; } - const AmountType integralTotalAmountToBuy = *integralTotalAmountToBuyOpt; + const AmountType integralTotalAmount = *integralTotalAmountOpt; for (int pos = _lowestAskPricePos - 1; pos >= 0; --pos) { MonetaryAmount price = priceAt(pos); assert(_orders[pos].amount != 0); - if (integralTotalAmountToBuy - integralAmountRep <= _orders[pos].amount) { - ret.emplace_back(MonetaryAmount(integralTotalAmountToBuy - integralAmountRep, _market.base(), - _volAndPriNbDecimals.volNbDecimals), - price); + if (integralTotalAmount - integralAmountRep <= _orders[pos].amount) { + ret.emplace_back( + MonetaryAmount(integralTotalAmount - integralAmountRep, _market.base(), _volAndPriNbDecimals.volNbDecimals), + price); return ret; } integralAmountRep += _orders[pos].amount; @@ -354,9 +352,10 @@ std::optional MarketOrderBook::computeAvgPriceForTakerAmount(Mon MonetaryAmount remQuoteAmount = amountInBaseOrQuote; const int nbOrders = _orders.size(); for (int pos = _highestBidPricePos + 1; pos < nbOrders; ++pos) { - MonetaryAmount amount = negAmountAt(pos); - MonetaryAmount price = priceAt(pos); - MonetaryAmount maxAmountToTakeFromThisLine = amount.toNeutral() * price; + const MonetaryAmount amount = negAmountAt(pos); + const MonetaryAmount price = priceAt(pos); + const MonetaryAmount maxAmountToTakeFromThisLine = amount.toNeutral() * price; + if (maxAmountToTakeFromThisLine < remQuoteAmount) { // We can eat all from this line, take the max and continue avgPrice += maxAmountToTakeFromThisLine.toNeutral() * price; @@ -378,9 +377,10 @@ std::optional MarketOrderBook::computeWorstPriceForTakerAmount( MonetaryAmount remQuoteAmount = amountInBaseOrQuote; const int nbOrders = _orders.size(); for (int pos = _highestBidPricePos + 1; pos < nbOrders; ++pos) { - MonetaryAmount amount = negAmountAt(pos); - MonetaryAmount price = priceAt(pos); - MonetaryAmount maxAmountToTakeFromThisLine = amount.toNeutral() * price; + const MonetaryAmount amount = negAmountAt(pos); + const MonetaryAmount price = priceAt(pos); + const MonetaryAmount maxAmountToTakeFromThisLine = amount.toNeutral() * price; + if (maxAmountToTakeFromThisLine < remQuoteAmount) { // We can eat all from this line, take the max and continue remQuoteAmount -= maxAmountToTakeFromThisLine; @@ -406,16 +406,16 @@ std::optional MarketOrderBook::convert(MonetaryAmount amountInBa // NOLINTNEXTLINE(misc-no-recursion) std::pair MarketOrderBook::operator[](int relativePosToLimitPrice) const { if (relativePosToLimitPrice == 0) { - auto [v11, v12] = (*this)[-1]; - auto [v21, v22] = (*this)[1]; + const auto [v11, v12] = (*this)[-1]; + const auto [v21, v22] = (*this)[1]; return std::make_pair(v11 + (v21 - v11) / 2, v12 + (v22 - v12) / 2); } if (relativePosToLimitPrice < 0) { - int pos = _lowestAskPricePos + relativePosToLimitPrice; + const int pos = _lowestAskPricePos + relativePosToLimitPrice; return std::make_pair(priceAt(pos), amountAt(pos)); } // > 0 - int pos = _highestBidPricePos + relativePosToLimitPrice; + const int pos = _highestBidPricePos + relativePosToLimitPrice; return std::make_pair(priceAt(pos), negAmountAt(pos)); } @@ -425,8 +425,9 @@ std::optional MarketOrderBook::convertBaseAmountToQuote(Monetary } MonetaryAmount quoteAmount(0, _market.quote()); for (int pos = _lowestAskPricePos - 1; pos >= 0; --pos) { - MonetaryAmount amount = amountAt(pos); - MonetaryAmount price = priceAt(pos); + const MonetaryAmount amount = amountAt(pos); + const MonetaryAmount price = priceAt(pos); + if (amount < amountInBaseCurrency) { // We can eat all from this line, take the max and continue quoteAmount += amount.toNeutral() * price; @@ -446,9 +447,10 @@ std::optional MarketOrderBook::convertQuoteAmountToBase(Monetary MonetaryAmount baseAmount(0, _market.base()); const int nbOrders = _orders.size(); for (int pos = _highestBidPricePos + 1; pos < nbOrders; ++pos) { - MonetaryAmount amount = negAmountAt(pos); - MonetaryAmount price = priceAt(pos); - MonetaryAmount maxAmountToTakeFromThisLine = price * amount.toNeutral(); + const MonetaryAmount amount = negAmountAt(pos); + const MonetaryAmount price = priceAt(pos); + const MonetaryAmount maxAmountToTakeFromThisLine = price * amount.toNeutral(); + if (maxAmountToTakeFromThisLine < amountInQuoteCurrency) { // We can eat all from this line, take the max and continue baseAmount += amount; diff --git a/src/tech/include/stringhelpers.hpp b/src/tech/include/stringhelpers.hpp index 8b9364b6..7aea2712 100644 --- a/src/tech/include/stringhelpers.hpp +++ b/src/tech/include/stringhelpers.hpp @@ -22,10 +22,10 @@ void ToChars(char *first, SizeType s, std::integral auto i) { } } // namespace details -inline string ToString(std::integral auto i) { - const int nbDigitsInt = nchars(i); +inline string ToString(std::integral auto val) { + const int nbDigitsInt = nchars(val); string s(nbDigitsInt, '0'); - details::ToChars(s.data(), nbDigitsInt, i); + details::ToChars(s.data(), nbDigitsInt, val); return s; }