algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
traits.hpp
1#pragma once
2
8#define ATL_BROKER_ADAPTER_PRESENT 1
9
10#include "./client.hpp"
11
12#include "../../core/broker_session.hpp"
13#include "../../core/broker_traits.hpp"
14
15#include <optional>
16#include <string_view>
17
18namespace algotradelib {
19
29template <>
30struct BrokerTraits<mock::MockClient> {
47
49 static constexpr std::string_view name = mock::Broker::name;
51 static constexpr bool blocking = true;
53 static constexpr bool data_only = false;
54
56 static constexpr bool has_account_discovery = false;
58 static constexpr bool has_client_order_id = false;
60 static constexpr bool has_working_orders = true;
62 static constexpr bool has_amendable_quantity = true;
64 static constexpr bool working_orders_state_terminal = false;
66 static constexpr bool has_trigger_price = false;
68 static constexpr bool has_order_by_id = false;
70 static constexpr bool has_partial_cancel = false;
72 static constexpr bool history_requires_range = false;
74 static constexpr bool has_history_cursor = false;
76 static constexpr bool has_history_limit = false;
78 static constexpr bool has_history_symbol_filter = false;
80 static constexpr bool has_depth = false;
82 static constexpr bool has_price_limits = false;
84 static constexpr bool has_trade_prints = false;
86 static constexpr bool has_batch_prices = false;
88 static constexpr bool has_buying_power = true;
90 static constexpr bool has_buying_power_quantity = false;
94 static constexpr bool has_commission_schedule = false;
96 static constexpr bool has_reference_data = true;
98 static constexpr bool has_batch_reference_data = false;
100 static constexpr bool has_calendar = true;
102 static constexpr bool has_calendar_range = true;
104 static constexpr bool has_calendar_sessions = false;
105
114 static constexpr bool supports_interval(BarInterval interval) {
115 return bar_interval_length(interval).has_value() || interval == BarInterval::ONE_DAY;
116 }
117
123 static std::optional<BrokerError> validate_bars(const BarQuery& query) {
124 if (supports_interval(query.interval_)) {
125 return std::nullopt;
126 }
127 return refusal("unsupported_interval", "the mock broker holds minute to daily bars only");
128 }
129
134 static std::optional<BrokerError> validate_calendar(Country, std::optional<Date>) {
135 return std::nullopt;
136 }
137
143 static std::optional<BrokerError> validate_place(const OrderRequest& order) {
144 if (!order.client_order_id_) {
145 return std::nullopt;
146 }
147 return refusal("idempotency_key_unsupported", "the mock broker takes no client order id");
148 }
149
158
167
173 return Result<Amendment>(
174 refusal("replace_unsupported", "the mock broker does not modify orders; cancel and place")
175 );
176 }
177
185 if (!instruction.all_remaining_) {
186 return Result<Amendment>(
187 refusal("partial_cancel_unsupported", "the mock broker cancels the whole remainder only")
188 );
189 }
191 }
192
199 if (query.symbol_ || query.limit_) {
201 refusal("history_filter_unsupported", "the mock broker's history takes no symbol filter or row limit")
202 );
203 }
204 return Result<HistoryQuery>(query);
205 }
206
212 return nullptr;
213 }
214
222 return client.quote(instrument);
223 }
224
234
241 static Result<TradingCalendar> calendar(Client& client, Country country, std::optional<Date>) {
242 return client.calendar(country);
243 }
244
251 static Result<BarSeries> bars(Client& client, BarQuery query) {
252 return client.bars(query);
253 }
254
261 return client.holdings();
262 }
263
270 static Result<BuyingPower> buying_power(Client& client, const Account&, Currency currency) {
271 return client.buying_power(currency);
272 }
273
280 static Result<PlaceAck> place_order(Client& client, const Account&, OrderRequest order) {
281 return client.place(order);
282 }
283
289 return Result<PlaceAck>(refusal("replace_unsupported", "the mock broker does not modify orders; cancel and place"));
290 }
291
298 static Result<PlaceAck> cancel_order(Client& client, const Account&, Amendment amendment) {
299 return client.cancel(amendment);
300 }
301
308 return client.working_orders();
309 }
310
318 return client.order_history(query);
319 }
320
321private:
323 static BrokerError refusal(const char* code, const char* message) {
324 BrokerError error;
326 error.reached_broker_ = false;
327 error.broker_code_ = code;
328 error.message_ = message;
329 return error;
330 }
331};
332
333} // namespace algotradelib
A three-letter currency code, such as KRW or USD (ISO 4217 style).
Definition types.hpp:374
Either a value or a BrokerError.
Definition types.hpp:1058
Result(T value)
Constructs a successful result.
Definition types.hpp:1064
The mock broker's client: blocking, answering from a Market, after any faults scripted for the call.
Definition client.hpp:53
Result< HoldingsSnapshot > holdings()
Answers a holdings request.
Definition client.hpp:196
Result< Instrument > instrument(const InstrumentId &instrument)
Answers a reference-data request.
Definition client.hpp:161
Result< TradingCalendar > calendar(Country country)
Answers a calendar request.
Definition client.hpp:173
Result< OrderPage > order_history(const OrderHistoryQuery &query)
Answers an order-history request.
Definition client.hpp:265
Result< PlacedOrder< Broker > > cancel(const MockAmendment &amendment)
Cancels an order.
Definition client.hpp:238
Result< WorkingOrders< Broker > > working_orders()
Answers a working-order request.
Definition client.hpp:254
Result< PlacedOrder< Broker > > place(const OrderRequest &order)
Places an order.
Definition client.hpp:221
Result< BuyingPower > buying_power(const Currency &currency)
Answers a buying-power request.
Definition client.hpp:207
Result< BarSeries > bars(const BarQuery &query)
Answers a bar request.
Definition client.hpp:185
Result< QuoteSnapshot > quote(const InstrumentId &instrument)
Answers a quote request.
Definition client.hpp:149
The mock broker: an in-memory market over bars the caller gives, bound like any broker.
Definition client.hpp:11
The library: broker-neutral types (core), and the modules below.
constexpr std::optional< std::chrono::seconds > bar_interval_length(BarInterval interval) noexcept
Returns an interval's fixed length, for an intraday interval.
SellableSource
Where the library gets a broker's sellable quantity.
Definition types.hpp:1362
@ HOLDINGS
On the holdings snapshot, at no extra request.
Definition types.hpp:1364
@ INVALID_ARGUMENT
The request was invalid, refused by the client before sending or by the broker.
Definition types.hpp:974
Country
A country (market) the library supports.
Definition types.hpp:32
BarInterval
The period one bar covers.
A modification or cancellation of a working order.
Definition types.hpp:2532
bool all_remaining_
Whether the whole remaining quantity is affected, rather than quantity_.
Definition types.hpp:2544
What bars to fetch.
BarInterval interval_
The period of each bar; an interval the broker does not publish is refused.
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
static Result< WorkingOrdersResult > working_orders(Client &client, const Account &)
Forwards a working-order request.
Definition traits.hpp:307
static Result< WorkingOrders< mock::Broker > > normalize_working_orders(Result< WorkingOrdersResult > raw)
Returns the listing as it is.
Definition traits.hpp:164
static Result< Amendment > to_cancellation(const OrderRef< mock::Broker > &ref, const AmendInstruction &instruction)
Converts a cancellation of the whole remainder; refuses a stated quantity.
Definition traits.hpp:184
static Result< Instrument > instrument(Client &client, InstrumentId instrument)
Forwards a reference-data request.
Definition traits.hpp:231
static constexpr bool data_only
It takes orders.
Definition traits.hpp:53
static constexpr bool has_working_orders
Lists open orders.
Definition traits.hpp:60
static constexpr bool has_partial_cancel
Cancels the whole remainder only.
Definition traits.hpp:70
static Result< PlaceAck > place_order(Client &client, const Account &, OrderRequest order)
Forwards a placement.
Definition traits.hpp:280
static constexpr bool has_account_discovery
No account listing: there is one account.
Definition traits.hpp:56
static constexpr bool has_price_limits
No price band.
Definition traits.hpp:82
static Result< HistoryQuery > to_history_query(const Account &, const OrderHistoryQuery &query)
Passes a history query through; refuses a symbol filter or a row limit.
Definition traits.hpp:198
static constexpr bool has_batch_prices
One quote per request.
Definition traits.hpp:86
static constexpr bool has_batch_reference_data
No reference data.
Definition traits.hpp:98
static constexpr bool has_commission_schedule
No commission schedule; costs come from the market's fill model.
Definition traits.hpp:94
static Result< TradingCalendar > calendar(Client &client, Country country, std::optional< Date >)
Forwards a calendar request; the answer covers every date the market has, whatever from.
Definition traits.hpp:241
static std::optional< BrokerError > validate_bars(const BarQuery &query)
Refuses a bar request for an interval no market can hold, before the client.
Definition traits.hpp:123
static constexpr bool has_calendar_range
One answer covers every date the market has.
Definition traits.hpp:102
static constexpr bool has_depth
No order book.
Definition traits.hpp:80
static constexpr bool has_trigger_price
No stop-limit orders.
Definition traits.hpp:66
static constexpr std::string_view name
The broker's name, "mock".
Definition traits.hpp:49
static constexpr bool has_buying_power_quantity
No affordable quantity.
Definition traits.hpp:90
mock::MockClient Client
The client.
Definition traits.hpp:32
static Result< BarSeries > bars(Client &client, BarQuery query)
Forwards a bar request.
Definition traits.hpp:251
static const network::RateLimitStats * rate_limit_stats(const Client &)
The mock broker is never paced.
Definition traits.hpp:211
OrderHistoryQuery HistoryQuery
The history query, the library's own.
Definition traits.hpp:44
static constexpr bool has_history_symbol_filter
No symbol filter.
Definition traits.hpp:78
static constexpr bool has_trade_prints
No trade prints.
Definition traits.hpp:84
static Result< PlaceAck > cancel_order(Client &client, const Account &, Amendment amendment)
Forwards a cancellation.
Definition traits.hpp:298
static Result< OrderPage > order_history(Client &client, const Account &, HistoryQuery query)
Forwards an order-history request.
Definition traits.hpp:317
static constexpr bool has_history_cursor
The whole history comes in one answer.
Definition traits.hpp:74
static constexpr bool history_requires_range
The history needs no date range.
Definition traits.hpp:72
WorkingOrders< mock::Broker > WorkingOrdersResult
The working-order listing, already normalized.
Definition traits.hpp:46
mock::MarketSettings Config
The configuration: the market's settings.
Definition traits.hpp:34
static Result< QuoteSnapshot > quote(Client &client, InstrumentId instrument)
Forwards a quote request.
Definition traits.hpp:221
static constexpr bool has_history_limit
No row limit.
Definition traits.hpp:76
PlacedOrder< mock::Broker > PlaceAck
The acknowledgement, already normalized.
Definition traits.hpp:40
mock::MockAccount Account
The account; the mock has one.
Definition traits.hpp:36
static constexpr bool supports_interval(BarInterval interval)
Tests whether the mock can hold bars of an interval: minute to daily.
Definition traits.hpp:114
mock::Broker BrokerTag
The broker tag.
Definition traits.hpp:38
static constexpr bool has_reference_data
Answers reference data: what the market was given, or its default.
Definition traits.hpp:96
static constexpr bool has_calendar
Publishes a calendar: what the market was given, or the dates of its bars.
Definition traits.hpp:100
static constexpr bool has_calendar_sessions
No session hours.
Definition traits.hpp:104
static Result< BuyingPower > buying_power(Client &client, const Account &, Currency currency)
Forwards a buying-power request.
Definition traits.hpp:270
static std::optional< BrokerError > validate_place(const OrderRequest &order)
Refuses an idempotency key, before the client.
Definition traits.hpp:143
static Result< PlaceAck > replace_order(Client &, const Account &, Amendment)
Never called: to_replacement refuses every modification.
Definition traits.hpp:288
static constexpr bool has_buying_power
Reports buying power.
Definition traits.hpp:88
static constexpr bool blocking
The client returns each result.
Definition traits.hpp:51
mock::MockAmendment Amendment
A cancellation.
Definition traits.hpp:42
static constexpr bool has_client_order_id
No idempotency key.
Definition traits.hpp:58
static Result< PlacedOrder< mock::Broker > > normalize_place(Result< PlaceAck > raw)
Returns the acknowledgement as it is: the market answers in the library's own shape.
Definition traits.hpp:155
static constexpr SellableSource sellable_source
The holdings rows carry the sellable quantity.
Definition traits.hpp:92
static constexpr bool has_order_by_id
No single-order inquiry.
Definition traits.hpp:68
static constexpr bool working_orders_state_terminal
The listing shows open orders only, as every shipped broker's does.
Definition traits.hpp:64
static Result< HoldingsSnapshot > holdings(Client &client, const Account &)
Forwards a holdings request.
Definition traits.hpp:260
static std::optional< BrokerError > validate_calendar(Country, std::optional< Date >)
Admits every calendar request; the market refuses another country itself.
Definition traits.hpp:134
static constexpr bool has_amendable_quantity
Reports each open order's quantity as amendable.
Definition traits.hpp:62
static Result< Amendment > to_replacement(const OrderRef< mock::Broker > &, const AmendInstruction &)
Refuses every modification: the mock broker cancels but does not modify orders.
Definition traits.hpp:172
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:85
The filter for an order-history inquiry.
Definition types.hpp:2617
std::optional< std::string > symbol_
Only this symbol, where the broker supports the filter.
Definition types.hpp:2623
std::optional< int > limit_
The most rows to return, where the broker supports a limit.
Definition types.hpp:2627
A broker's reference to one order, used to amend or cancel it.
Definition types.hpp:2402
std::string primary_
The order number or id.
Definition types.hpp:2404
An order to place, in broker-neutral form.
Definition types.hpp:2216
std::optional< std::string > client_order_id_
An idempotency key, if the broker offers one; never generated by the library.
Definition types.hpp:2257
An accepted order: the acknowledgement and the reference to address it with.
Definition types.hpp:2460
The orders open to modification or cancellation at one point in time.
Definition types.hpp:2604
The mock broker's tag: its order references are typed by it, so they work with no other broker.
Definition market.hpp:31
static constexpr std::string_view name
The broker's name, "mock".
Definition market.hpp:33
How a Market starts and what it charges.
Definition market.hpp:85
The mock broker's one account; it has no selector.
Definition client.hpp:14
A cancellation for the mock broker: the order id.
Definition client.hpp:17
A rate limiter's counters per traffic class, its current budget, and the broker's refusals (from Rate...