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<Executor, std::void_t<decltype(net::post(std::declval<const Executor&>(), std::declval<void (*)()>()))>>
71: std::true_type {};
72
73} // namespace detail
75
88template <typename Out, typename Handler>
89void post_refusal(Handler&& handler) {
90 auto executor = net::get_associated_executor(handler, net::system_executor());
91 auto complete = [handler = std::forward<Handler>(handler)]() mutable {
92 std::move(handler)(Result<Out>(refusal()));
93 };
94 if constexpr (detail::postable<decltype(executor)>::value) {
95 net::post(executor, std::move(complete));
96 } else {
97 net::post(net::system_executor(), net::bind_executor(executor, std::move(complete)));
98 }
99}
100
101} // namespace data_only
102
146template <typename Tag>
149 using BrokerTag = Tag;
160
162 static constexpr bool data_only = true;
163
166 return {};
167 }
168
170 // Every capability is false until the vendor's own specialization says otherwise.
171 static constexpr bool has_account_discovery = false;
172 static constexpr bool has_client_order_id = false;
173 static constexpr bool has_trigger_price = false;
174 static constexpr bool has_working_orders = false;
175 static constexpr bool has_amendable_quantity = false;
176 static constexpr bool has_order_by_id = false;
177 static constexpr bool has_partial_cancel = false;
178 static constexpr bool history_requires_range = false;
179 static constexpr bool has_history_cursor = false;
180 static constexpr bool has_history_limit = false;
181 static constexpr bool has_history_symbol_filter = false;
182 static constexpr bool has_depth = false;
183 static constexpr bool has_price_limits = false;
184 static constexpr bool has_trade_prints = false;
185 static constexpr bool has_batch_prices = false;
186 static constexpr bool has_buying_power = false;
187 static constexpr bool has_buying_power_quantity = false;
188 static constexpr SellableSource sellable_source = SellableSource::NONE;
189 static constexpr bool has_commission_schedule = false;
190 static constexpr bool has_reference_data = false;
191 static constexpr bool has_batch_reference_data = false;
192 static constexpr bool has_calendar = false;
193 static constexpr bool has_calendar_range = false;
194 static constexpr bool has_calendar_sessions = false;
196
202 template <typename Client>
203 static const network::RateLimitStats* rate_limit_stats(const Client&) {
204 return nullptr;
205 }
206
211 static std::optional<BrokerError> validate_place(const OrderRequest&) {
212 return data_only::refusal();
213 }
214
223
231
239
247
253 template <typename Client>
257
264 template <typename Client, typename Handler>
265 static void holdings(Client&, Account&, Handler&& handler) {
266 data_only::post_refusal<HoldingsSnapshot>(std::forward<Handler>(handler));
267 }
268
274 template <typename Client>
278
285 template <typename Client, typename Handler>
286 static void place_order(Client&, Account&, OrderRequest, Handler&& handler) {
287 data_only::post_refusal<PlaceAck>(std::forward<Handler>(handler));
288 }
289
295 template <typename Client>
299
306 template <typename Client, typename Handler>
307 static void replace_order(Client&, Account&, Amendment, Handler&& handler) {
308 data_only::post_refusal<PlaceAck>(std::forward<Handler>(handler));
309 }
310
316 template <typename Client>
320
327 template <typename Client, typename Handler>
328 static void cancel_order(Client&, Account&, Amendment, Handler&& handler) {
329 data_only::post_refusal<PlaceAck>(std::forward<Handler>(handler));
330 }
331
337 template <typename Client>
341
348 template <typename Client, typename Handler>
349 static void order_history(Client&, Account&, HistoryQuery, Handler&& handler) {
350 data_only::post_refusal<OrderPage>(std::forward<Handler>(handler));
351 }
352};
353
354} // namespace algotradelib
Either a value or a BrokerError.
Definition types.hpp:1058
Result(T value)
Constructs a successful result.
Definition types.hpp:1064
BrokerError & error()
Returns the error.
Definition types.hpp:1111
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:1362
@ NONE
No per-instrument inquiry: any figure comes on the holdings rows (Kiwoom), or there is none.
Definition types.hpp:1368
@ INVALID_ARGUMENT
The request was invalid, refused by the client before sending or by the broker.
Definition types.hpp:974
A modification or cancellation of a working order.
Definition types.hpp:2532
The error a broker or transport operation returns.
Definition types.hpp:1003
std::string message_
A human-readable description, for logs; do not parse it.
Definition types.hpp:1033
ErrorCategory category_
The kind of failure; the field to branch on.
Definition types.hpp:1005
std::string broker_code_
The broker's own error code, verbatim (for example KIS's EGW00201).
Definition types.hpp:1022
bool reached_broker_
Whether a request was sent before this error.
Definition types.hpp:1014
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 Account no_account()
Returns an empty account, which every account-side member refuses.
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:2617
A broker's reference to one order, used to amend or cancel it.
Definition types.hpp:2402
An order to place, in broker-neutral form.
Definition types.hpp:2216
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...