3#include <algotradelib/core>
99 const auto selection = schedule.
rate_for(country, date);
100 if (!selection.rate_) {
106 if (selection.bounds_unreadable_) {
109 "the published schedule holds a commission rate for that market whose date "
110 "bounds could not be read, so whether it applies on that date is unknown";
114 error.
message_ =
"the published schedule states no commission rate for that market and date";
117 const auto& rate = selection.
rate_;
197 policy.flat_ = std::move(model);
215 "commission_tariff_unnamed",
216 "a caller-stated commission schedule must name the tariff it was read from, because the "
217 "published table is channel by venue and nothing here can select the channel"
222 "commission_venue_not_an_execution",
223 "COMBINED is consolidated routing rather than a venue an order matched on, so it cannot be "
224 "asserted as the venue a fill happened at"
228 policy.schedule_ = std::move(
schedule);
229 policy.asserted_ = std::move(asserted);
239 return schedule_.has_value();
254 std::string_view
tariff() const noexcept {
255 return schedule_ ? std::string_view(schedule_->tariff_) : std::string_view();
270 const std::optional<CommissionSchedule>&
schedule() const noexcept {
279 return dating_market_;
299 const auto charged = flat_.on(quantity, price);
302 refusal(
"commission_not_representable",
"the commission on that execution does not fit an exact decimal")
318 if (country != dating_market_ && schedule_->any_dated_rate_for(country)) {
322 error.
broker_code_ =
"commission_date_not_market_local";
323 error.
message_ =
"this market's rates have dated bounds, and the replay's date " + named(date) +
324 " is that of another market's trading day, so which rate applies cannot be "
325 "decided here; price it in a replay whose session is that market's, or state rates with "
330 const auto selection = schedule_->rate_for(country, asserted_, date);
331 const auto& rate = selection.rate_;
337 if (selection.bounds_unreadable_) {
339 error.
message_ =
"the schedule holds a commission rate for " + described(asserted_) +
340 " in this instrument's market whose date bounds could not be read, so whether it applies "
342 named(date) +
" is unknown; refetch the schedule or state the rates";
346 error.
message_ =
"the schedule states no commission rate for " + named(date) +
" at venue " +
347 described(asserted_) +
" in the market this instrument is listed in";
352 const auto broker = component(quantity, price, rate->rate_,
Decimal64::parse(
"0"));
355 refusal(
"commission_not_representable",
"the commission on that execution does not fit an exact decimal")
360 if (rate->institution_rate_) {
362 const auto institution = component(quantity, price, *rate->institution_rate_,
Decimal64::parse(
"0"));
365 refusal(
"commission_not_representable",
"the institution fee on that execution does not fit an exact decimal")
382 static std::string described(
const VenueKey& venue) {
383 switch (venue.
kind()) {
385 return "(none asserted)";
395 static std::string named(
const Date& date) {
396 const auto pad = [](
unsigned value) {
397 return value < 10 ?
"0" + std::to_string(value) : std::to_string(value);
399 return std::to_string(date.year_) +
"-" + pad(date.month_) +
"-" + pad(date.day_);
402 static BrokerError refusal(std::string code, std::string message) {
406 error.reached_broker_ =
false;
407 error.broker_code_ = std::move(code);
408 error.message_ = std::move(message);
413 static std::optional<Decimal64> component(
414 const Decimal64& quantity,
const Decimal64& price,
const Decimal64& rate,
const Decimal64& minimum
416 CommissionModel model;
418 model.minimum_ = minimum;
419 return model.on(quantity, price);
423 CommissionModel flat_{};
425 std::optional<CommissionSchedule> schedule_;
429 Country dating_market_{Country::KR};
459 std::optional<std::pair<Decimal64, Decimal64>>
apply(
485 for (
int step = 0; step <
ticks_; ++step) {
486 const auto tick = side == OrderSide::BUY ? rule.
tick_at(walked) : tick_below(rule, walked);
516 return std::pair<Decimal64, Decimal64>{moved, *given_up};
552 static std::optional<Decimal64> tick_below(
const PriceRule& rule,
const Decimal64& price) {
553 std::optional<Decimal64> tick;
554 for (
const auto& band : rule.
bands_) {
592 return "at_open_if_better";
std::string str() const
Returns the code as a string.
An exact signed decimal: an int64 coefficient times 10^-scale.
std::int64_t coefficient() const noexcept
Returns the coefficient.
static Decimal64 parse(std::string_view input)
Parses a decimal string exactly (no binary floating point).
Either a value or a BrokerError.
Result(T value)
Constructs a successful result.
Which venue a commission rate applies to: any venue, a normalized Venue, or a raw code.
const Code< 8 > & raw_code() const noexcept
Returns the raw venue code.
VenueKeyKind kind() const noexcept
Returns the key's kind.
Venue venue() const noexcept
Returns the normalized venue.
CommissionPolicy()=default
Constructs a policy that charges nothing (the report says so).
static Result< CommissionPolicy > scheduled(CommissionSchedule schedule, VenueKey asserted, Country dating_market)
Makes a policy that picks a rate from a schedule for each execution.
const VenueKey & asserted_venue() const noexcept
Returns the asserted venue.
std::string_view tariff() const noexcept
Returns the schedule's tariff name.
static CommissionPolicy stated(CommissionModel model)
Makes a policy that charges one model on every execution.
const std::optional< CommissionSchedule > & schedule() const noexcept
Returns the schedule.
Result< CommissionCharge > charge(const Decimal64 &quantity, const Decimal64 &price, const Date &date, Country country) const
Computes the commission on one execution.
Country dating_market() const noexcept
Returns the market whose local trading day a scheduled policy's dates are.
const CommissionModel & stated_model() const noexcept
Returns the stated model.
bool is_scheduled() const noexcept
Tests whether the policy resolves a schedule.
Replays a strategy over stored bars: fill and cost models, the replay ledger, and the report.
Result< CommissionModel > commission_model_from(const CommissionSchedule &schedule, Country country, const Date &date)
Builds a flat commission model from a schedule's rate for a market on a date.
LimitFillPrice
Which price a backtest credits a limit order that fills.
@ AT_OPEN_IF_BETTER
Fill at the bar's open when it opened through the limit (assumes the order was resting before the ope...
@ AT_LIMIT
The default: fill at the limit, or at the bar's worst traded price when the whole bar was better than...
constexpr std::string_view limit_fill_price_name(LimitFillPrice choice) noexcept
Returns a lowercase name for a choice, such as "at_limit", for reports.
The library: broker-neutral types (core), and the modules below.
@ NORMALIZED
A Venue the library normalizes.
@ RAW
A broker venue code the library does not normalize, kept verbatim.
@ ANY
Any venue (a venue-neutral rate).
CommissionOrigin
Where a commission schedule's numbers came from.
@ CALLER_STATED
The caller stated them, from a fee page or their own agreement.
@ BROKER_PUBLISHED
The broker published them, through an inquiry the library made.
constexpr bool commission_origin_names_tariff(CommissionOrigin origin) noexcept
Tests whether a schedule of this origin must name the tariff it was read from.
std::optional< Decimal64 > decimal_add(const Decimal64 &lhs, const Decimal64 &rhs)
Adds two decimals exactly.
@ INVALID_ARGUMENT
The request was invalid, refused by the client before sending or by the broker.
Country
A country (market) the library supports.
constexpr bool commission_origin_is_broker(CommissionOrigin origin) noexcept
Tests whether commission rates of this origin came from the broker.
constexpr std::string_view venue_name(Venue venue) noexcept
Returns a display name for a venue, such as "KRX".
std::optional< Decimal64 > decimal_subtract(const Decimal64 &lhs, const Decimal64 &rhs)
Subtracts two decimals exactly.
std::optional< Decimal64 > decimal_divide(const Decimal64 &lhs, const Decimal64 &rhs, std::uint8_t scale)
Divides two decimals, truncating toward zero at a given scale.
bool decimal_less(const Decimal64 &lhs, const Decimal64 &rhs)
Compares two decimals numerically, whatever their scales.
std::optional< Decimal64 > decimal_multiply(const Decimal64 &lhs, const Decimal64 &rhs)
Multiplies two decimals exactly; the scales add.
OrderSide
The side of an order: its economic direction, buy or sell.
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.
std::optional< CommissionRate > rate_
The rate in force, if one definitely is.
The commission rates a broker published for an account, or a caller stated.
RateSelection rate_for(Country country, const VenueKey &asserted, const Date &date) const
Finds the rate in force for a market and venue on a date.
CommissionOrigin origin_
Whether a broker published the rates or a caller stated them.
std::string tariff_
Which tariff the rates are, in the source's own words.
A calendar date with no time of day or time zone.
The price grid (tick sizes) an instrument's order prices must sit on.
std::optional< Decimal64 > tick_at(const Decimal64 &price) const
Returns the tick size at a price.
std::vector< TickBand > bands_
The bands, ascending by from_; empty unless kind_ is STATED.
The commission rate that charged an execution, as the backtest report lists it.
VenueKey venue_
The venue the rate was keyed to.
std::optional< Decimal64 > institution_rate_
The institution fee rate; empty if the source gave one combined rate.
std::optional< Date > until_
The last date of the rate's window; empty if open-ended.
friend bool operator==(const AppliedRate &lhs, const AppliedRate &rhs)
Compares every field except executions_.
std::optional< Date > from_
The first date of the rate's window; empty if open-ended.
Decimal64 minimum_
The per-execution minimum on the broker's commission; zero if none.
CommissionOrigin origin_
Whether a broker published the rate or a caller stated it.
Decimal64 rate_
The broker's commission, as a fraction of traded value.
std::size_t executions_
How many executions the rate charged, counted in the report's table.
Country country_
The market the rate was selected for.
What one execution was charged: the broker's commission and the institution fee.
std::optional< Decimal64 > institution_
The institution fee (유관기관제비용), if the rate is split.
Decimal64 broker_
The broker's commission, after its minimum.
AppliedRate applied_
The rate that charged it.
A flat commission: a rate on traded value, with an optional per-execution minimum.
std::optional< Decimal64 > on(const Decimal64 &quantity, const Decimal64 &price) const
Computes the commission on one execution.
Decimal64 rate_
The fraction of traded value charged per execution (0.00015, not 0.015).
static CommissionModel of(Decimal64 rate, Decimal64 minimum=Decimal64::parse("0"))
Returns a rate-based model.
Decimal64 minimum_
The smallest commission on any execution.
bool from_broker_
Whether the rate came from a broker's published schedule; commission_model_from sets it from the sche...
static CommissionModel none()
Returns a model that charges nothing (the report says so).
Slippage: how far a fill price moves against the order (a buy pays more, a sell gets less).
Decimal64 basis_points_
The price given up per execution, in ten-thousandths of the price.
static SlippageModel of_basis_points(Decimal64 points)
Returns a basis-point slippage model.
static SlippageModel of_ticks(int count)
Returns a tick slippage model.
static SlippageModel none()
Returns a model with no slippage (the report says so).
int ticks_
Grid steps given up per execution, each a step to the next grid price against the order.
std::optional< std::pair< Decimal64, Decimal64 > > apply(const Decimal64 &price, OrderSide side, const PriceRule &rule) const
Moves a price against the order.