Skip to content

Commit

Permalink
Return market data instead of void
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanel committed Sep 28, 2024
1 parent 9d42d18 commit a8b0580
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/engine/include/coincenter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ class Coincenter {
CurrencyCode equiCurrencyCode,
std::optional<int> depth = std::nullopt);

/// Query market data without returning it.
/// Query market data (order books and last trades).
/// This method is especially useful for serialization and metric exports.
void queryMarketDataPerExchange(std::span<const Market> marketPerPublicExchange);
MarketDataPerExchange queryMarketDataPerExchange(std::span<const Market> marketPerPublicExchangePos);

/// Retrieve the last 24h traded volume for exchanges supporting given market.
MonetaryAmountPerExchange getLast24hTradedVolumePerExchange(Market mk, ExchangeNameSpan exchangeNames);
Expand Down
8 changes: 5 additions & 3 deletions src/engine/src/coincenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,19 @@ MarketOrderBookConversionRates Coincenter::getMarketOrderBooks(Market mk, Exchan
return ret;
}

void Coincenter::queryMarketDataPerExchange(std::span<const Market> marketPerPublicExchange) {
MarketDataPerExchange Coincenter::queryMarketDataPerExchange(std::span<const Market> marketPerPublicExchangePos) {
ExchangeNames exchangeNames;

int exchangePos{};
for (Market market : marketPerPublicExchange) {
for (Market market : marketPerPublicExchangePos) {
if (market.isDefined()) {
exchangeNames.emplace_back(kSupportedExchanges[exchangePos]);
}
++exchangePos;
}

const auto marketDataPerExchange =
_exchangesOrchestrator.getMarketDataPerExchange(marketPerPublicExchange, exchangeNames);
_exchangesOrchestrator.getMarketDataPerExchange(marketPerPublicExchangePos, exchangeNames);

// Transform data structures to export metrics input format
MarketOrderBookConversionRates marketOrderBookConversionRates(marketDataPerExchange.size());
Expand All @@ -104,6 +104,8 @@ void Coincenter::queryMarketDataPerExchange(std::span<const Market> marketPerPub

_metricsExporter.exportOrderbookMetrics(marketOrderBookConversionRates);
_metricsExporter.exportLastTradesMetrics(lastTradesPerExchange);

return marketDataPerExchange;
}

BalancePerExchange Coincenter::getBalance(std::span<const ExchangeName> privateExchangeNames,
Expand Down
5 changes: 1 addition & 4 deletions src/engine/src/exchangesorchestrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -968,9 +968,6 @@ MarketDataPerExchange ExchangesOrchestrator::getMarketDataPerExchange(std::span<

MarketDataPerExchange ret(selectedExchanges.size());
_threadPool.parallelTransform(selectedExchanges, ret.begin(), [&marketPerPublicExchange](Exchange *exchange) {
if (!exchange->exchangeConfig().withMarketDataSerialization()) {
log::warn("Calling market-data on {} with data serialization disabled", exchange->name());
}
// Call order book and last trades sequentially for this exchange
Market market = marketPerPublicExchange[exchange->publicExchangePos()];

Expand Down Expand Up @@ -1071,4 +1068,4 @@ MarketTradingGlobalResultPerExchange ExchangesOrchestrator::getMarketTraderResul
return marketTradingGlobalResultPerExchange;
}

} // namespace cct
} // namespace cct
5 changes: 3 additions & 2 deletions src/engine/test/coincenteroptions_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ TEST_F(CoincenterCmdLineOptionsTest, DefaultConstructorShouldValueInitializeAll)

EXPECT_EQ(opts, *pRhs);

static_assert(std::is_trivially_destructible_v<CoincenterCmdLineOptions>,
"then destructor should be called (but ideally this type should stay trivially destructible)");
if constexpr (!std::is_trivially_destructible_v<CoincenterCmdLineOptions>) {
std::destroy_at(pRhs);
}
}

TEST_F(CoincenterCmdLineOptionsTest, MergeGlobal) {
Expand Down
5 changes: 5 additions & 0 deletions src/objects/include/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class File : public Reader, public Writer {
enum class Type : int8_t { kCache, kSecret, kStatic, kLog };
enum class IfError : int8_t { kThrow, kNoThrow };

/// Creates a File directly from a file path.
File(std::string_view filePath, IfError ifError);

/// Creates a File from the coincenter data directory, with the type of the file and its name in the main data
/// directory.
File(std::string_view dataDir, Type type, std::string_view name, IfError ifError);

[[nodiscard]] string readAll() const override;
Expand Down
4 changes: 3 additions & 1 deletion src/objects/src/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ string FullFileName(std::string_view dataDir, std::string_view fileName, File::T
}
} // namespace

File::File(std::string_view filePath, IfError ifError) : _filePath(filePath), _ifError(ifError) {}

File::File(std::string_view dataDir, Type type, std::string_view name, IfError ifError)
: _filePath(FullFileName(dataDir, name, type)), _ifError(ifError) {}

Expand All @@ -49,7 +51,7 @@ string File::readAll() const {
throw exception("Unable to open {} for reading", _filePath);
}
try {
data = string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
data = string(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
} catch (const std::exception& e) {
if (_ifError == IfError::kThrow) {
throw e;
Expand Down
3 changes: 3 additions & 0 deletions src/tech/include/cct_const.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <algorithm>
#include <iterator>
#include <string_view>

Expand All @@ -18,6 +19,8 @@ static constexpr std::string_view kDepositAddressesFileName = "depositaddresses.

static constexpr std::string_view kSupportedExchanges[] = {"binance", "bithumb", "huobi", "kraken", "kucoin", "upbit"};

static_assert(std::ranges::is_sorted(kSupportedExchanges));

static constexpr int kNbSupportedExchanges = static_cast<int>(std::size(kSupportedExchanges));

static constexpr int kTypicalNbPrivateAccounts = kNbSupportedExchanges;
Expand Down

0 comments on commit a8b0580

Please sign in to comment.