algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
broker_verbs.hpp
1#pragma once
2
3#include "./broker_caps.hpp"
4#include "./broker_session.hpp"
5#include "./broker_traits.hpp"
6
7#include <boost/asio/use_future.hpp>
8
9#include <optional>
10#include <vector>
11
12namespace algotradelib {
13
31namespace conformance {
32
39 template <typename T>
40 void operator()(T) const {}
41};
42
46 MoveOnlyCallback() = default;
54 MoveOnlyCallback(const MoveOnlyCallback&) = delete;
56
61 template <typename T>
62 void operator()(T) {}
63};
64
69inline InstrumentId probe_instrument() { return InstrumentId{Country::KR, Venue::COMBINED, "000000"}; }
70
78template <typename Client, typename Token>
80 BrokerSession<Client>& session, const typename BrokerSession<Client>::Ref& ref
81) {
82 using Caps = BrokerCaps<Client>;
83 session.async_quote(probe_instrument(), Token{});
84 session.async_holdings(Token{});
85 session.async_bars(BarQuery{}, Token{});
86 session.async_place(OrderRequest{}, Token{});
87 session.async_replace(ref, AmendInstruction{}, Token{});
88 session.async_cancel(ref, AmendInstruction{}, Token{});
89 session.async_order_history(OrderHistoryQuery{}, Token{});
90 if constexpr (Caps::has_buying_power) {
91 session.async_buying_power(Currency{}, Token{});
92 }
93 if constexpr (Caps::sellable_source == SellableSource::INQUIRY) {
94 session.async_sellable_quantity(probe_instrument(), Token{});
95 }
96 if constexpr (Caps::has_commission_schedule) {
97 session.async_commissions(Token{});
98 }
99 if constexpr (Caps::has_working_orders) {
100 session.async_working_orders(Token{});
101 }
102 if constexpr (Caps::has_reference_data) {
103 session.async_instrument(probe_instrument(), Token{});
104 }
105 if constexpr (Caps::has_calendar) {
106 session.async_calendar(Country::KR, std::nullopt, Token{});
107 }
108 if constexpr (Caps::has_price_limits) {
109 session.async_price_limits(probe_instrument(), Token{});
110 }
111 if constexpr (Caps::has_trade_prints) {
112 session.async_trades(probe_instrument(), 1, Token{});
113 }
114 if constexpr (Caps::has_batch_prices) {
115 session.async_prices(std::vector<InstrumentId>{probe_instrument()}, Token{});
116 }
117 if constexpr (Caps::has_depth) {
118 session.async_depth(probe_instrument(), Token{});
119 }
120 if constexpr (Caps::has_order_by_id) {
121 session.async_order(ref, Token{});
122 }
123 if constexpr (Caps::has_account_discovery) {
124 async_accounts(session.client(), Token{});
125 }
126}
127
134template <typename Client>
136 BlockingSession<Client>& session, const typename BlockingSession<Client>::Ref& ref
137) {
138 using Caps = BrokerCaps<Client>;
139 static_cast<void>(session.quote(probe_instrument()));
140 static_cast<void>(session.holdings());
141 static_cast<void>(session.bars(BarQuery{}));
142 static_cast<void>(session.place(OrderRequest{}));
143 static_cast<void>(session.replace(ref, AmendInstruction{}));
144 static_cast<void>(session.cancel(ref, AmendInstruction{}));
145 static_cast<void>(session.order_history(OrderHistoryQuery{}));
146 if constexpr (Caps::has_buying_power) {
147 static_cast<void>(session.buying_power(Currency{}));
148 }
149 if constexpr (Caps::sellable_source == SellableSource::INQUIRY) {
150 static_cast<void>(session.sellable_quantity(probe_instrument()));
151 }
152 if constexpr (Caps::has_commission_schedule) {
153 static_cast<void>(session.commissions());
154 }
155 if constexpr (Caps::has_working_orders) {
156 static_cast<void>(session.working_orders());
157 }
158 if constexpr (Caps::has_reference_data) {
159 static_cast<void>(session.instrument(probe_instrument()));
160 }
161 if constexpr (Caps::has_calendar) {
162 static_cast<void>(session.calendar(Country::KR));
163 }
164 if constexpr (Caps::has_price_limits) {
165 static_cast<void>(session.price_limits(probe_instrument()));
166 }
167 if constexpr (Caps::has_trade_prints) {
168 static_cast<void>(session.trades(probe_instrument(), 1));
169 }
170 if constexpr (Caps::has_batch_prices) {
171 static_cast<void>(session.prices(std::vector<InstrumentId>{probe_instrument()}));
172 }
173 if constexpr (Caps::has_depth) {
174 static_cast<void>(session.depth(probe_instrument()));
175 }
176 if constexpr (Caps::has_order_by_id) {
177 static_cast<void>(session.order(ref));
178 }
179 if constexpr (Caps::has_account_discovery) {
180 static_cast<void>(session.accounts());
181 static_cast<void>(list_accounts(session.client()));
182 }
183}
184
190template <typename Client>
191constexpr bool claimed_verbs_compile() {
192 if constexpr (BrokerTraits<Client>::blocking) {
193 static_cast<void>(&call_claimed_blocking_verbs<Client>);
194 } else {
198 }
199 return true;
200}
201
202} // namespace conformance
203
213#define ATL_ASSERT_BROKER(Client) \
214 static_assert( \
215 ::algotradelib::conformance::claimed_verbs_compile<Client>() && \
216 ::algotradelib::answers_every_capability<Client>(), \
217 "every port verb this client's capabilities claim must compile, and every capability " \
218 "must be answered" \
219 )
220
221} // namespace algotradelib
A blocking broker client bound to one account, with the same broker operations as BrokerSession,...
Result< OrderPage > order_history(const OrderHistoryQuery &query)
Returns order history: placed orders and their executions.
Result< Placed > place(OrderRequest order)
Places a new order.
Result< WorkingSet > working_orders()
Returns the orders still open to modification or cancellation; requires Caps::has_working_orders.
Result< OrderSnapshot > order(const Ref &ref)
Returns one order by its reference; requires Caps::has_order_by_id.
Result< BarSeries > bars(BarQuery query)
Returns bars for an instrument, oldest first, paging until count_ bars are read.
Result< QuoteSnapshot > quote(InstrumentId instrument)
Returns the current quote for an instrument.
Result< Placed > replace(const Ref &ref, const AmendInstruction &instruction)
Modifies a working order.
Result< PriceLimits > price_limits(InstrumentId instrument)
Returns an instrument's price limits for today; requires Caps::has_price_limits.
Result< std::vector< TradePrint > > trades(InstrumentId instrument, int count)
Returns recent trades for an instrument, newest first; requires Caps::has_trade_prints.
Result< BuyingPower > buying_power(Currency currency)
Returns what the account may spend in a currency; requires Caps::has_buying_power.
Result< Placed > cancel(const Ref &ref, const AmendInstruction &instruction)
Cancels a working order, in full or in part.
Result< HoldingsSnapshot > holdings()
Returns the account's holdings.
Result< Decimal64 > sellable_quantity(InstrumentId instrument)
Returns how much of an instrument the account may sell; only where Caps::sellable_source is INQUIRY.
Client & client() noexcept
Returns the broker client, for broker-specific operations the session does not offer.
OrderRef< BrokerTag > Ref
The order reference type this session issues and accepts.
Result< DepthSnapshot > depth(InstrumentId instrument)
Returns an instrument's order book, best levels first; requires Caps::has_depth.
Result< TradingCalendar > calendar(Country country, std::optional< Date > from=std::nullopt)
Returns the trading calendar; requires Caps::has_calendar.
Result< CommissionSchedule > commissions()
Returns the account's commission rates; requires Caps::has_commission_schedule.
Result< std::vector< QuoteSnapshot > > prices(std::vector< InstrumentId > instruments)
Returns quotes for several instruments in one request; requires Caps::has_batch_prices.
Result< Instrument > instrument(InstrumentId subject)
Returns an instrument's reference data; requires Caps::has_reference_data.
Result< std::vector< AccountInfo< Client > > > accounts()
Lists the broker's accounts; requires Caps::has_account_discovery.
An asynchronous broker client bound to one account, with broker-neutral operations.
auto async_quote(InstrumentId instrument, CompletionToken &&token)
Requests the current quote for an instrument.
auto async_order_history(OrderHistoryQuery query, CompletionToken &&token)
Requests order history: placed orders and their executions.
OrderRef< BrokerTag > Ref
The order reference type this session issues and accepts.
auto async_place(OrderRequest order, CompletionToken &&token)
Places a new order.
auto async_bars(BarQuery query, CompletionToken &&token)
Requests bars for an instrument, paging until count_ bars are read.
auto async_sellable_quantity(InstrumentId instrument, CompletionToken &&token)
Requests how much of an instrument the account may sell.
auto async_buying_power(Currency currency, CompletionToken &&token)
Requests what the account may spend in a currency.
auto async_price_limits(InstrumentId instrument, CompletionToken &&token)
Requests an instrument's price limits for today.
Client & client() noexcept
Returns the broker client, for broker-specific operations the session does not offer.
auto async_replace(Ref ref, AmendInstruction instruction, CompletionToken &&token)
Modifies a working order.
auto async_commissions(CompletionToken &&token)
Requests the account's commission rates.
auto async_instrument(InstrumentId instrument, CompletionToken &&token)
Requests an instrument's reference data.
auto async_holdings(CompletionToken &&token)
Requests the account's holdings.
auto async_working_orders(CompletionToken &&token)
Requests the orders still open to modification or cancellation.
auto async_trades(InstrumentId instrument, int count, CompletionToken &&token)
Requests recent trades for an instrument, newest first.
auto async_order(Ref ref, CompletionToken &&token)
Requests one order by its reference.
auto async_prices(std::vector< InstrumentId > instruments, CompletionToken &&token)
Requests quotes for several instruments in one request.
auto async_cancel(Ref ref, AmendInstruction instruction, CompletionToken &&token)
Cancels a working order, in full or in part.
auto async_calendar(Country country, std::optional< Date > from, CompletionToken &&token)
Requests the trading calendar.
auto async_depth(InstrumentId instrument, CompletionToken &&token)
Requests an instrument's order book, best levels first.
A three-letter currency code, such as KRW or USD (ISO 4217 style).
Definition types.hpp:234
The compile-time check behind ATL_ASSERT_BROKER(Client).
constexpr bool claimed_verbs_compile()
Compiles the operation checks for a client.
void call_claimed_blocking_verbs(BlockingSession< Client > &session, const typename BlockingSession< Client >::Ref &ref)
Calls every operation a blocking session's capabilities claim; compiled, never run.
void call_claimed_async_verbs(BrokerSession< Client > &session, const typename BrokerSession< Client >::Ref &ref)
Calls every operation an asynchronous session's capabilities claim; compiled, never run.
InstrumentId probe_instrument()
Returns the placeholder instrument the checks call operations with.
The library: broker-neutral types (core), and the modules below.
auto async_accounts(Client &client, CompletionToken &&token)
Lists a broker's accounts, before a session exists; requires BrokerCaps<Client>::has_account_discover...
Result< std::vector< AccountInfo< Client > > > list_accounts(Client &client)
Lists a broker's accounts with a blocking client, before a session exists; requires BrokerCaps<Client...
@ INQUIRY
From a per-instrument inquiry, made only where the snapshot has no figure.
Definition types.hpp:1096
A modification or cancellation of a working order.
Definition types.hpp:2240
What bars to fetch.
What one broker client implements, as compile-time constants.
Describes one broker client to the library: its types, capabilities, call thunks and normalizers.
Identifies an instrument: its country, venue and symbol.
Definition types.hpp:78
The filter for an order-history inquiry.
Definition types.hpp:2325
An order to place, in broker-neutral form.
Definition types.hpp:1925
void operator()(T)
Accepts and discards any completion argument.
MoveOnlyCallback(MoveOnlyCallback &&)=default
Move constructor (the callback has no state).
MoveOnlyCallback()=default
Constructs the callback.
MoveOnlyCallback & operator=(MoveOnlyCallback &&)=default
Move assignment.
A completion handler with no associated executor (a plain callback).
void operator()(T) const
Accepts and discards any completion argument.