Skip to content

Commit

Permalink
[MDAPI-82][C++] Implement MarketDepthModel
Browse files Browse the repository at this point in the history
  • Loading branch information
AnatolyKalin committed Dec 11, 2024
1 parent a6f2515 commit 3a643cd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 72 deletions.
22 changes: 7 additions & 15 deletions include/dxfeed_graal_cpp_api/internal/Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -932,24 +932,16 @@ inline void checkChar(char c, std::uint32_t mask, const std::string &name) {

namespace math {

template <typename ComparatorResultType, typename ResultType = int>
ResultType normalizeComparatorResult(ComparatorResultType r) {
if constexpr (std::is_signed_v<ComparatorResultType>) {
return r < 0 ? -1 : r > 0 ? 1 : 0;
} else {
auto result = static_cast<std::make_signed_t<ComparatorResultType>>(r);

return result < 0 ? -1 : result > 0 ? 1 : 0;
template <Integral Type, typename ResultType = int> ResultType compare(Type v1, Type v2) {
if (v1 < v2) {
return -1;
}
}

template <Integral Type, typename ResultType = int> ResultType compare(Type v1, Type v2) {
if constexpr (std::is_signed_v<Type>) {
return normalizeComparatorResult<ResultType>(v1 - v2);
} else {
return normalizeComparatorResult<ResultType>(static_cast<std::make_signed_t<Type>>(v1) -
static_cast<std::make_signed_t<Type>>(v2));
if (v2 < v1) {
return 1;
}

return 0;
}

inline int compare(double d1, double d2) {
Expand Down
57 changes: 0 additions & 57 deletions samples/cpp/DxFeedConnect/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,63 +54,6 @@ DxFeedConnect <address> <types> <symbols> [<time>]

int main(int argc, char *argv[]) {
try {
auto ep = DXEndpoint::getInstance();
auto feed = ep->getFeed();

// IndexedTxModel::newBuilder(Order::TYPE)->withFeed(feed)->withSources({OrderSource::NTV})->withSymbol("AAPL")->withListener<Order>([](const
// IndexedEventSource & IndexedEventSource, const std::vector<std::shared_ptr<Order>> & SharedPtrs, bool Cond) {
// })

auto model =
MarketDepthModel<Order>::newBuilder()
->withFeed(feed)
->withSources({OrderSource::NTV})
->withSymbol("AAPL")
->withDepthLimit(10)
->withAggregationPeriod(5s)
->withListener([](const std::vector<std::shared_ptr<Order>> &buy,
const std::vector<std::shared_ptr<Order>> &sell) {
if (buy.empty() && sell.empty()) {
return;
}

std::cout << std::format("{:=^66}\n", "");
std::cout << std::format("{:^31} || {:^31}\n", "ASK", "BID");
std::cout << std::format("{0:^15}|{1:^15} || {0:^15}|{1:^15}\n", "Price", "Size");
std::cout << std::format("{:-^66}\n", "");

for (auto buyIt = buy.begin(), sellIt = sell.begin(); buyIt != buy.end() && sellIt != sell.end();) {
std::string row{};
if (buyIt != buy.end()) {
row += std::format("{:>14.4f} | {:<14.2f}", (*buyIt)->getPrice(), (*buyIt)->getSize());

++buyIt;
} else {
row += std::format("{:>14} | {:<14}", "", "");
}

row += " || ";

if (sellIt != sell.end()) {
row += std::format("{:>14.4f} | {:<14.2f}", (*sellIt)->getPrice(), (*sellIt)->getSize());

++sellIt;
} else {
row += std::format("{:>14} | {:<14}", "", "");
}

std::cout << row << std::endl;
}

std::cout << std::format("{:=^66}\n", "");
})
->build();

ep->connect("demo.dxfeed.com:7300");

std::cin.get();

return 0;
if (argc < 4) {
printUsage();

Expand Down

0 comments on commit 3a643cd

Please sign in to comment.