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 bfb722e commit a6f2515
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
10 changes: 5 additions & 5 deletions include/dxfeed_graal_cpp_api/model/MarketDepthModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ template <Derived<OrderBase> O> struct DXFCPP_EXPORT MarketDepthModel final : Re
}

std::shared_ptr<MarketDepthModel> build() {
return MarketDepthModel::createShared(this->template sharedAs<Builder>());
return MarketDepthModel::create(this->template sharedAs<Builder>());
}
};

Expand Down Expand Up @@ -197,11 +197,11 @@ template <Derived<OrderBase> O> struct DXFCPP_EXPORT MarketDepthModel final : Re
struct SellComparator {
int operator()(const std::shared_ptr<O> &o1, const std::shared_ptr<O> &o2) const {
if (o1->getPrice() < o2->getPrice()) {
return 1; // desc
return -1; // asc
}

if (o1->getPrice() > o2->getPrice()) {
return -1;
return 1;
}

return OrderComparator{}(o1, o2);
Expand Down Expand Up @@ -383,7 +383,7 @@ template <Derived<OrderBase> O> struct DXFCPP_EXPORT MarketDepthModel final : Re
std::shared_ptr<Timer> taskTimer_{};

static std::shared_ptr<MarketDepthModel> create(std::shared_ptr<Builder> builder) {
auto marketDepthModel = createShared(builder);
auto marketDepthModel = MarketDepthModel::createShared(builder);

marketDepthModel->indexedTxModel_ =
builder->builder_
Expand Down Expand Up @@ -422,7 +422,7 @@ template <Derived<OrderBase> O> struct DXFCPP_EXPORT MarketDepthModel final : Re
void notifyListeners() {
std::lock_guard guard(mtx_);

listener_->getHandler().handle(getBuyOrders(), getSellOrders());
listener_->getHandler()(getBuyOrders(), getSellOrders());
taskScheduled_ = false;
}

Expand Down
48 changes: 33 additions & 15 deletions samples/cpp/DxFeedConnect/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,37 +56,55 @@ int main(int argc, char *argv[]) {
try {
auto ep = DXEndpoint::getInstance();
auto feed = ep->getFeed();
MarketDepthModel<Order>::newBuilder()
->withFeed(feed)
->withSources({OrderSource::NTV})
->withSymbol("AAPL")
->withDepthLimit(10)
->withListener(
[](const std::vector<std::shared_ptr<Order>> &buy, const std::vector<std::shared_ptr<Order>> &sell) {
std::size_t size = std::max(buy.size(), sell.size());

// 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("{:^30}", std::format("{}@{}", (*buyIt)->getPrice(), (*buyIt)->getSize()));
row += std::format("{:>14.4f} | {:<14.2f}", (*buyIt)->getPrice(), (*buyIt)->getSize());

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

row += " | ";
row += " || ";

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

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

row += "\n";
std::cout << row << std::endl;
}
});

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

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

Expand Down
14 changes: 11 additions & 3 deletions tests/model/MarketDepthModelTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,26 @@ class MarketDepthModelTestFixture {
}

static int oneIfBuy(const std::shared_ptr<Order> &order) {
return order->getOrderSide() == Side::BUY && !!math::equals(order->getSize(), 0) ? 1 : 0;
return order && order->getOrderSide() == Side::BUY && !math::equals(order->getSize(), 0) ? 1 : 0;
}

static int oneIfSell(const std::shared_ptr<Order> &order) {
return order->getOrderSide() == Side::SELL && !!math::equals(order->getSize(), 0) ? 1 : 0;
return order && order->getOrderSide() == Side::SELL && !math::equals(order->getSize(), 0) ? 1 : 0;
}

static bool same(const std::shared_ptr<Order> &order, const std::shared_ptr<Order> &old) {
if (order->getSize() == 0) {
if (order && order->getSize() == 0) {
return true; // order with zero size is the same as null (missing)
}

if (!order && !old) {
return true;
}

if (!order || !old) {
return false;
}

// Check just relevant attributes
return order->getScope() == old->getScope() && order->getOrderSide() == old->getOrderSide() &&
order->getIndex() == old->getIndex() && math::equals(order->getSize(), old->getSize()) &&
Expand Down

0 comments on commit a6f2515

Please sign in to comment.