Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
queue_position.hpp
Go to the documentation of this file.
1#pragma once
2
6#include <charconv>
7#include <cstdint>
8#include <optional>
9#include <string_view>
10
11namespace slick::trader {
12
13// Where one resting order stands in the queue at its price, read from a market-by-order book.
15 uint32_t orders_ahead{0}; // orders at the same price that trade before this one
16 qty_t quantity_ahead{0}; // their displayed quantity: what must trade or cancel first
17 qty_t quantity{0}; // the order's own displayed quantity
18 qty_t level_quantity{0}; // everything displayed at the price, this order included
19 uint32_t level_orders{0}; // orders at the price, this one included
20 uint16_t level_index{0}; // 0 = the best price on its side
23};
24
25// The queue position of `order_id`, or nothing when the book does not hold it - not yet
26// acknowledged on the feed, filled, or cancelled. Costs one hop per order ahead.
27inline std::optional<QueuePosition> queue_position(const slick::orderbook::OrderBookL3& book,
28 slick::orderbook::OrderId order_id) noexcept {
29 const auto* order = book.findOrder(order_id);
30 if (order == nullptr) {
31 return std::nullopt;
32 }
33
34 QueuePosition position;
35 position.quantity = static_cast<qty_t>(order->quantity);
36 position.price = static_cast<price_t>(order->price);
37 position.side = order->side == slick::orderbook::Side::Buy ? Side::BUY : Side::SELL;
38
39 // A level keeps its orders in priority order: everything reached through `prev` trades first.
40 for (const auto* ahead = order->prev; ahead != nullptr; ahead = ahead->prev) {
41 ++position.orders_ahead;
42 position.quantity_ahead += static_cast<qty_t>(ahead->quantity);
43 }
44
45 const auto [level, index] = book.getLevel(order->side, order->price);
46 if (level != nullptr) [[likely]] {
47 position.level_quantity = static_cast<qty_t>(level->getTotalQuantity());
48 position.level_orders = static_cast<uint32_t>(level->orderCount());
49 position.level_index = index;
50 }
51 return position;
52}
53
54// The same, by the order id as order events carry it: text. Nothing when the id is not a
55// number, which is how brokers without a market-by-order feed name their orders.
56inline std::optional<QueuePosition> queue_position(const slick::orderbook::OrderBookL3& book,
57 std::string_view order_id) noexcept {
58 slick::orderbook::OrderId id = 0;
59 const auto* end = order_id.data() + order_id.size();
60 const auto [parsed, error] = std::from_chars(order_id.data(), end, id);
61 if (error != std::errc{} || parsed != end || order_id.empty()) {
62 return std::nullopt;
63 }
64 return queue_position(book, id);
65}
66
67} // namespace slick::trader
int64_t qty_t
Definition types.hpp:17
int64_t price_t
Definition types.hpp:16
std::optional< QueuePosition > queue_position(const slick::orderbook::OrderBookL3 &book, slick::orderbook::OrderId order_id) noexcept