algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
data_only_traits.hpp
1#pragma once
2
3#include "./broker_traits.hpp"
4#include "./types.hpp"
5
6#include "../network/rate_limit.hpp"
7
8#include <boost/asio/associated_executor.hpp>
9#include <boost/asio/bind_executor.hpp>
10#include <boost/asio/post.hpp>
11#include <boost/asio/system_executor.hpp>
12
13#include <optional>
14#include <type_traits>
15#include <utility>
16
17namespace algotradelib {
18
19namespace net = boost::asio;
20
26namespace data_only {
27
29struct NoAccount {};
31struct NoAck {};
33struct NoAmendment {};
38
48 BrokerError error;
50 error.reached_broker_ = false;
51 error.broker_code_ = "data_only";
52 error.message_ = "a market-data source holds no account and takes no order";
53 return error;
54}
55
57namespace detail {
58
66template <typename Executor, typename = void>
67struct postable : std::false_type {};
68
69template <typename Executor>
70struct postable<
71 Executor, std::void_t<decltype(net::post(std::declval<const Executor&>(), std::declval<void (*)()>()))>
72> : std::true_type {};
73
74} // namespace detail
76
89template <typename Out, typename Handler>
90void post_refusal(Handler&& handler) {
91 auto executor = net::get_associated_executor(handler, net::system_executor());
92 auto complete = [handler = std::forward<Handler>(handler)]() mutable {
93 std::move(handler)(Result<Out>(refusal()));
94 };
95 if constexpr (detail::postable<decltype(executor)>::value) {
96 net::post(executor, std::move(complete));
97 } else {
98 net::post(net::system_executor(), net::bind_executor(executor, std::move(complete)));
99 }
100}
101
102} // namespace data_only
103
147template <typename Tag>
150 using BrokerTag = Tag;
161
163 static constexpr bool data_only = true;
164
166 // Every capability is false until the vendor's own specialization says otherwise.
167 static constexpr bool has_account_discovery = false;
168 static constexpr bool has_client_order_id = false;
169 static constexpr bool has_trigger_price = false;
170 static constexpr bool has_working_orders = false;
171 static constexpr bool has_amendable_quantity = false;
172 static constexpr bool has_order_by_id = false;
173 static constexpr bool has_partial_cancel = false;
174 static constexpr bool history_requires_range = false;
175 static constexpr bool has_history_cursor = false;
176 static constexpr bool has_history_limit = false;
177 static constexpr bool has_history_symbol_filter = false;
178 static constexpr bool has_depth = false;
179 static constexpr bool has_price_limits = false;
180 static constexpr bool has_trade_prints = false;
181 static constexpr bool has_batch_prices = false;
182 static constexpr bool has_buying_power = false;
183 static constexpr bool has_buying_power_quantity = false;
184 static constexpr SellableSource sellable_source = SellableSource::NONE;
185 static constexpr bool has_commission_schedule = false;
186 static constexpr bool has_reference_data = false;
187 static constexpr bool has_batch_reference_data = false;
188 static constexpr bool has_calendar = false;
189 static constexpr bool has_calendar_range = false;
190 static constexpr bool has_calendar_sessions = false;
192
198 template <typename Client>
199 static const network::RateLimitStats* rate_limit_stats(const Client&) {
200 return nullptr;
201 }
202
207 static std::optional<BrokerError> validate_place(const OrderRequest&) { return data_only::refusal(); }
208
217
225
233
241
247 template <typename Client>
251
258 template <typename Client, typename Handler>
259 static void holdings(Client&, Account&, Handler&& handler) {
260 data_only::post_refusal<HoldingsSnapshot>(std::forward<Handler>(handler));
261 }
262
268 template <typename Client>
272
279 template <typename Client, typename Handler>
280 static void place_order(Client&, Account&, OrderRequest, Handler&& handler) {
281 data_only::post_refusal<PlaceAck>(std::forward<Handler>(handler));
282 }
283
289 template <typename Client>
293
300 template <typename Client, typename Handler>
301 static void replace_order(Client&, Account&, Amendment, Handler&& handler) {
302 data_only::post_refusal<PlaceAck>(std::forward<Handler>(handler));
303 }
304
310 template <typename Client>
314
321 template <typename Client, typename Handler>
322 static void cancel_order(Client&, Account&, Amendment, Handler&& handler) {
323 data_only::post_refusal<PlaceAck>(std::forward<Handler>(handler));
324 }
325
331 template <typename Client>
335
342 template <typename Client, typename Handler>
343 static void order_history(Client&, Account&, HistoryQuery, Handler&& handler) {
344 data_only::post_refusal<OrderPage>(std::forward<Handler>(handler));
345 }
346};
347
348} // namespace algotradelib
Either a value or a BrokerError.
Definition types.hpp:797
Result(T value)
Constructs a successful result.
Definition types.hpp:803
BrokerError & error()
Returns the error.
Definition types.hpp:842
Placeholder types and the refusal used by DataOnlyTraits.
void post_refusal(Handler &&handler)
Completes an asynchronous call with refusal(), posted through the handler's associated executor.
BrokerError refusal()
Returns the error every account-side member of DataOnlyTraits answers with.
The library: broker-neutral types (core), and the modules below.
SellableSource
Where the library gets a broker's sellable quantity.
Definition types.hpp:1092
@ NONE
No per-instrument inquiry: any figure comes on the holdings rows (Kiwoom), or there is none.
Definition types.hpp:1098
@ INVALID_ARGUMENT
The request was invalid, refused by the client before sending or by the broker.
Definition types.hpp:713
A modification or cancellation of a working order.
Definition types.hpp:2240
The error a broker or transport operation returns.
Definition types.hpp:742
std::string message_
A human-readable description, for logs; do not parse it.
Definition types.hpp:772
ErrorCategory category_
The kind of failure; the field to branch on.
Definition types.hpp:744
std::string broker_code_
The broker's own error code, verbatim (for example KIS's EGW00201).
Definition types.hpp:761
bool reached_broker_
Whether a request was sent before this error.
Definition types.hpp:753
A base for the BrokerTraits of a market-data client that cannot trade.
static std::optional< BrokerError > validate_place(const OrderRequest &)
Refuses every order before it is built.
static void place_order(Client &, Account &, OrderRequest, Handler &&handler)
Refuses a placement, asynchronously.
static void holdings(Client &, Account &, Handler &&handler)
Refuses holdings, asynchronously.
static constexpr bool data_only
true: the client is a market-data source; see BrokerCaps::data_only.
data_only::NoAck PlaceAck
Never produced: placement refuses.
static Result< PlaceAck > cancel_order(Client &, Account &, Amendment)
Refuses a cancellation.
data_only::NoWorkingOrders WorkingOrdersResult
Never produced: working orders are not claimed.
static Result< PlacedOrder< Tag > > normalize_place(Result< PlaceAck > raw)
Passes the refusal through (an acknowledgement is refused too).
static Result< PlaceAck > replace_order(Client &, Account &, Amendment)
Refuses a replacement.
static void replace_order(Client &, Account &, Amendment, Handler &&handler)
Refuses a replacement, asynchronously.
static Result< Amendment > to_replacement(const OrderRef< Tag > &, const AmendInstruction &)
Refuses an amendment.
static void order_history(Client &, Account &, HistoryQuery, Handler &&handler)
Refuses order history, asynchronously.
Tag BrokerTag
The vendor's tag type.
static Result< Amendment > to_cancellation(const OrderRef< Tag > &, const AmendInstruction &)
Refuses a cancellation.
data_only::NoHistoryQuery HistoryQuery
Never produced: history refuses.
static Result< OrderPage > order_history(Client &, Account &, HistoryQuery)
Refuses order history.
static Result< PlaceAck > place_order(Client &, Account &, OrderRequest)
Refuses a placement.
static void cancel_order(Client &, Account &, Amendment, Handler &&handler)
Refuses a cancellation, asynchronously.
data_only::NoAccount Account
No account; the session binds an empty one.
static Result< HoldingsSnapshot > holdings(Client &, Account &)
Refuses holdings.
static Result< HistoryQuery > to_history_query(const Account &, const OrderHistoryQuery &)
Refuses a history query.
data_only::NoAmendment Amendment
Never produced: amendment refuses.
static const network::RateLimitStats * rate_limit_stats(const Client &)
Reports that the client runs no rate limiter; declare your own if it does.
The filter for an order-history inquiry.
Definition types.hpp:2325
A broker's reference to one order, used to amend or cancel it.
Definition types.hpp:2111
An order to place, in broker-neutral form.
Definition types.hpp:1925
The (empty) account a data-only session binds.
Placeholder order acknowledgement; never produced.
Placeholder amendment; never produced.
Placeholder history query; never produced.
Placeholder working-order listing; never produced.
A rate limiter's counters per traffic class, its current budget, and the broker's refusals (from Rate...