Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
coinbase_order_states.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <cstddef>
6#include <deque>
7#include <string>
8#include <unordered_map>
9
10namespace slick::trader {
11
12// What the Coinbase connector last told the engine of each order, so each change goes out once.
13// Coinbase says most things twice: the REST response and the user channel both ack a new order,
14// both report a cancel, and the channel can repeat an update. The channel knows no base size,
15// and a pending order no qty: a qty, price or stop of 0 (or NULL_PRICE) keeps the one known.
16// Used from the connector's worker thread only.
18public:
19 // How many ended orders are remembered, so their late repeats drop. The oldest go first.
20 static constexpr std::size_t ENDED_KEPT = 1024;
21
22 // Whether an ack with these tells something new of the order, and records it. A price, stop or
23 // qty the update doesn't know comes back as the known one. An ended order's acks are stale, and
24 // so is a pending one after the order opened: the REST response can come after the channel's.
25 bool ack(const std::string& order_id, OrderStatus status, price_t& price, price_t& stop_price, qty_t& qty) {
26 auto [it, added] = orders_.try_emplace(order_id, State{status, price, stop_price, qty, false});
27 if (added) {
28 return true;
29 }
30 auto& state = it->second;
31 if (state.ended || (status == OrderStatus::PENDING_NEW && state.status != OrderStatus::PENDING_NEW)) {
32 return false;
33 }
34 keep_known(price, state.price);
35 keep_known(stop_price, state.stop_price);
36 if (qty == 0) {
37 qty = state.qty;
38 }
39 const State next{status, price, stop_price, qty, false};
40 if (next == state) {
41 return false;
42 }
43 state = next;
44 return true;
45 }
46
47 // Whether the order was said to be gone, or filled.
48 bool ended(const std::string& order_id) const {
49 const auto it = orders_.find(order_id);
50 return it != orders_.end() && it->second.ended;
51 }
52
53 // Whether this is the first word that the order is gone, or filled.
54 bool end(const std::string& order_id) {
55 auto [it, added] = orders_.try_emplace(order_id, State{});
56 if (it->second.ended) {
57 return false;
58 }
59 it->second.ended = true;
60 ended_.push_back(order_id);
61 if (ended_.size() > ENDED_KEPT) {
62 orders_.erase(ended_.front());
63 ended_.pop_front();
64 }
65 return true;
66 }
67
68 std::size_t size() const { return orders_.size(); }
69
70private:
71 struct State {
73 price_t price{0};
74 price_t stop_price{0};
75 qty_t qty{0};
76 bool ended{false};
77
78 bool operator==(const State&) const = default;
79 };
80
81 static void keep_known(price_t& price, price_t known) {
82 if (price == 0 || price == NULL_PRICE) {
83 price = known;
84 }
85 }
86
87 std::unordered_map<std::string, State> orders_; // keyed by order_id
88 std::deque<std::string> ended_; // oldest first
89};
90
91} // namespace slick::trader
bool ack(const std::string &order_id, OrderStatus status, price_t &price, price_t &stop_price, qty_t &qty)
bool ended(const std::string &order_id) const
bool end(const std::string &order_id)
static constexpr std::size_t ENDED_KEPT
constexpr price_t NULL_PRICE
Definition types.hpp:19
int64_t qty_t
Definition types.hpp:17
int64_t price_t
Definition types.hpp:16