90 throw std::invalid_argument(
"a fault is scripted for at least one call");
92 for (
int k = 0; k < times; ++k) {
93 faults_.push_back(Fault{call, error, Effect::FAIL});
106 throw std::invalid_argument(
"only a placement or a cancellation has a reply to lose");
112 error.
message_ =
"the mock broker carried this out and lost the reply";
113 faults_.push_back(Fault{call, std::move(error), Effect::CARRY_OUT});
134 throw std::invalid_argument(
"only a read of the account's state can be repeated");
137 throw std::invalid_argument(
"a fault is scripted for at least one call");
139 for (
int k = 0; k < times; ++k) {
140 faults_.push_back(Fault{call,
BrokerError{}, Effect::REPEAT});
177 return market_->calendar(country);
189 return market_->bars(query);
198 return market_->holdings();
208 if (last_buying_power_ && !(last_buying_power_->cash_.currency_ == currency)) {
209 last_buying_power_.reset();
212 return market_->buying_power(currency);
223 if (fault && fault->effect_ == Effect::FAIL) {
226 auto placed = market_->place(order);
240 if (fault && fault->effect_ == Effect::FAIL) {
243 auto canceled = market_->cancel(amendment.
order_id_);
293 template <
typename T,
typename Ask>
294 Result<T> read(
Call call, std::optional<T>& last, Ask ask) {
295 auto fault = take(call);
296 if (fault && fault->effect_ == Effect::FAIL) {
297 return Result<T>(std::move(fault->error_));
300 auto repeated = *last;
301 repeated.received_at_ = market_->now();
306 last = answer.value();
312 std::optional<Fault> take(
Call call) {
313 for (
auto it = faults_.begin(); it != faults_.end(); ++it) {
314 if (it->call_ == call) {
315 auto fault = std::move(*it);
324 std::deque<Fault> faults_;
325 std::optional<HoldingsSnapshot> last_holdings_;
326 std::optional<BuyingPower> last_buying_power_;
327 std::optional<WorkingOrders<Broker>> last_working_orders_;
328 std::optional<OrderPage> last_history_;
A three-letter currency code, such as KRW or USD (ISO 4217 style).
Either a value or a BrokerError.
Result(T value)
Constructs a successful result.
A market in memory: bars the caller gives, a clock the caller moves, one account's cash,...
Result< HoldingsSnapshot > holdings()
Answers a holdings request.
Result< Instrument > instrument(const InstrumentId &instrument)
Answers a reference-data request.
Result< TradingCalendar > calendar(Country country)
Answers a calendar request.
MockClient(Market &market)
Builds a client over a market.
Market & market() const noexcept
Returns the market.
void fail_next(Call call, BrokerError error, int times=1)
Answers the next calls of a kind with an error instead of asking the market.
void lose_next_reply(Call call)
Carries out the next placement or cancellation in the market, then answers it with a transport error ...
Result< OrderPage > order_history(const OrderHistoryQuery &query)
Answers an order-history request.
Result< PlacedOrder< Broker > > cancel(const MockAmendment &amendment)
Cancels an order.
static constexpr std::string_view environment() noexcept
Returns the environment this client trades in.
Result< WorkingOrders< Broker > > working_orders()
Answers a working-order request.
Result< PlacedOrder< Broker > > place(const OrderRequest &order)
Places an order.
Result< BuyingPower > buying_power(const Currency ¤cy)
Answers a buying-power request.
Result< BarSeries > bars(const BarQuery &query)
Answers a bar request.
Result< QuoteSnapshot > quote(const InstrumentId &instrument)
Answers a quote request.
void repeat_last(Call call, int times=1)
Answers the next reads of an account's state with what the last one answered, stamped now,...
The mock broker: an in-memory market over bars the caller gives, bound like any broker.
Call
A call to the mock broker, for scripting a fault (MockClient::fail_next).
@ ORDER_HISTORY
order_history.
@ WORKING_ORDERS
working_orders.
@ BUYING_POWER
buying_power.
constexpr std::string_view environment
The environment the mock reports: always "mock", never "demo" or "real".
The library: broker-neutral types (core), and the modules below.
@ TRANSPORT
The connection failed (DNS, connect, TLS, write or read).
Country
A country (market) the library supports.
The error a broker or transport operation returns.
std::string message_
A human-readable description, for logs; do not parse it.
ErrorCategory category_
The kind of failure; the field to branch on.
std::string broker_code_
The broker's own error code, verbatim (for example KIS's EGW00201).
bool reached_broker_
Whether a request was sent before this error.
Identifies an instrument: its country, venue and symbol.
The filter for an order-history inquiry.
An order to place, in broker-neutral form.
The mock broker's one account; it has no selector.
A cancellation for the mock broker: the order id.
std::string order_id_
The order id.