algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
fill.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "./cost.hpp"
4#include "./ledger.hpp"
5
8
9#include <algotradelib/core>
10
11#include <functional>
12#include <optional>
13#include <stdexcept>
14#include <string>
15#include <utility>
16
17namespace algotradelib {
18namespace backtest {
19
28
32 std::optional<Execution> execution_;
34 bool still_working_{false};
40 std::optional<BrokerError> costing_error_;
42 bool released_{false};
52 bool bounded_{false};
54 bool priced_by_rule_{false};
55};
56
66 const std::optional<Decimal64>& decision_price_;
71 const std::optional<std::chrono::system_clock::time_point>& decision_price_at_;
73 const std::optional<std::chrono::system_clock::time_point>& decided_at_;
78};
79
87using MarketPriceFunction = std::function<std::optional<Decimal64>(const MarketPriceInput&)>;
88
126
128struct SellTax {
132 Country country_{Country::KR};
133};
134
143public:
153 )
154 : commission_(CommissionPolicy::stated(std::move(commission))),
155 slippage_(std::move(slippage)),
156 limit_price_(limit_price) {}
157
167 )
168 : commission_(std::move(commission)), slippage_(std::move(slippage)), limit_price_(limit_price) {}
169
174 const CommissionPolicy& commission() const noexcept {
175 return commission_;
176 }
177
182 const SlippageModel& slippage() const noexcept {
183 return slippage_;
184 }
185
190 LimitFillPrice limit_price() const noexcept {
191 return limit_price_;
192 }
193
208 throw std::invalid_argument("a sell tax rate must be at least 0 and below 1");
209 }
210 auto model = *this;
211 model.sell_tax_ = std::move(tax);
212 return model;
213 }
214
219 const std::optional<SellTax>& sell_tax() const noexcept {
220 return sell_tax_;
221 }
222
241 if (name.empty() || !price) {
242 throw std::invalid_argument("a market-price rule needs a name and a function");
243 }
244 auto model = *this;
245 model.market_price_name_ = std::move(name);
246 model.market_price_ = std::move(price);
247 model.next_close_ = false;
248 model.library_rule_.reset();
249 return model;
250 }
251
263 auto model = *this;
264 model.next_close_ = false;
265 model.library_rule_ = price;
266 switch (price) {
268 model.market_price_name_.clear();
269 model.market_price_ = nullptr;
270 break;
272 model.market_price_name_ = "decision-close";
273 model.market_price_ = [](const MarketPriceInput& in) {
274 return in.decision_price_ ? in.decision_price_ : in.next_.bar_.open_;
275 };
276 break;
278 model.market_price_name_ = "next-close";
279 model.market_price_ = [](const MarketPriceInput& in) {
280 return std::optional<Decimal64>(in.next_.bar_.close_);
281 };
282 model.next_close_ = true;
283 break;
284 }
285 return model;
286 }
287
292 const std::string& market_price_rule() const noexcept {
293 return market_price_name_;
294 }
295
300 bool has_market_price_rule() const noexcept {
301 return static_cast<bool>(market_price_);
302 }
303
309 const std::optional<MarketPrice>& library_market_price() const noexcept {
310 return library_rule_;
311 }
312
319 static constexpr bool limit_priced(OrderType type) noexcept {
320 switch (type) {
321 case OrderType::LIMIT:
323 return true;
324 case OrderType::UNKNOWN:
331 return false;
332 }
333 return false;
334 }
335
346 static bool rests_unfilled(const strategy::PlannedOrder& order) noexcept {
347 switch (order.time_in_force_) {
348 case TimeInForce::DAY:
349 break;
350 // Filled or not on the next bar, and not kept after it.
353 case TimeInForce::UNKNOWN:
354 return false;
355 }
356 // A stop rests while dormant and, once released, as the limit it has become.
357 return limit_priced(order.type_);
358 }
359
394 const strategy::PlannedOrder& order, const strategy::BarEvent& next, const PriceRule& rule, const Date& date,
395 const std::optional<Date>& open_date = std::nullopt, bool released = false,
396 const std::optional<Decimal64>& decision_price = std::nullopt,
397 const std::optional<std::chrono::system_clock::time_point>& decision_price_at = std::nullopt,
398 const std::optional<std::chrono::system_clock::time_point>& decided_at = std::nullopt
399 ) const {
400 FillDecision decision;
401 bool at_open = false;
402 std::optional<Decimal64> reference;
403 if (market_price_ && !limit_priced(order.type_)) {
404 if (next_close_ && longer_than_a_day(next.interval_)) {
405 // The close of a calendar bar has no known date or session to charge and expire it by.
406 BrokerError error;
408 error.reached_broker_ = false;
409 error.broker_code_ = "next_close_bar_too_long";
410 error.message_ =
411 "the market-price rule 'next-close' prices bars of a day or less, not a weekly, monthly or "
412 "yearly bar";
413 decision.costing_error_ = std::move(error);
414 return decision;
415 }
416 reference = market_price_(MarketPriceInput{order, decision_price, decision_price_at, decided_at, next, rule});
417 if (!reference) {
418 // Not this bar: a DAY order rests for the rest of its session; any other is withdrawn.
420 return decision;
421 }
422 if (reference->coefficient() <= 0) {
423 // A defect in the rule, not a price: filling at it would book a trade no market made.
424 BrokerError error;
426 error.reached_broker_ = false;
427 error.broker_code_ = "market_price_not_positive";
428 error.message_ = "the market-price rule '" + market_price_name_ + "' named a price that is not positive";
429 decision.costing_error_ = std::move(error);
430 return decision;
431 }
432 decision.priced_by_rule_ = true;
433 // Charged on the fill bar's own day, as an open's fill is.
434 at_open = true;
435 } else if (carries_trigger_price(order.type_) && !released) {
436 if (!order.trigger_price_ || !order.price_) {
437 // Not a stop this model can release (a caller driving the model directly).
438 return decision;
439 }
440 switch (release_of(order, next)) {
441 case Release::NONE:
442 // Dormant: the bar never reached the trigger.
443 decision.still_working_ = true;
444 return decision;
445 case Release::AT_OPEN:
446 // The whole bar is after the release, so it is priced as the limit it now is.
447 decision.released_ = true;
448 reference = reference_price(order, next, at_open);
449 // Dated from the open whatever the price policy credits: it traded at the open.
450 if (reference && limit_marketable_at(order, *next.bar_.open_)) {
451 at_open = true;
452 }
453 break;
454 case Release::INTRABAR:
455 decision.released_ = true;
456 reference = intrabar_release_price(order, next);
457 if (!reference) {
458 // The bar cannot show the price reached the limit after the release. Rests for the rest of
459 // its session (on daily bars, it expires unfilled).
460 decision.still_working_ = true;
461 return decision;
462 }
463 decision.intrabar_release_fill_ = true;
464 break;
465 }
466 } else {
467 reference = reference_price(order, next, at_open);
468 }
469 if (!reference) {
470 // A limit the bar never reached stays queued for the rest of its session.
471 decision.still_working_ = rests_unfilled(order);
472 return decision;
473 }
474
475 // A limit fill is credited a price its bar traded and never one through the limit: the bound is a
476 // sell's max(low, limit) or a buy's min(high, limit), applied before and after slippage.
477 std::optional<Decimal64> bound;
478 const bool limit_basis = limit_priced(order.type_);
479 if (limit_basis && order.price_) {
480 const auto& limit = *order.price_;
481 if (order.side_ == OrderSide::SELL) {
482 bound = decimal_less(limit, next.bar_.low_) ? next.bar_.low_ : limit;
483 if (decimal_less(*reference, *bound)) {
484 reference = bound;
485 decision.bounded_ = true;
486 }
487 } else {
488 bound = decimal_less(next.bar_.high_, limit) ? next.bar_.high_ : limit;
489 if (decimal_less(*bound, *reference)) {
490 reference = bound;
491 decision.bounded_ = true;
492 }
493 }
494 }
495 auto slipped = slippage_.apply(*reference, order.side_, rule);
496 if (slipped && decision.intrabar_release_fill_) {
497 // No favourable slippage after an intrabar release: the bar's other extreme may have come first.
498 const bool favourable = order.side_ == OrderSide::SELL ?
499 decimal_less(*reference, slipped->first) :
500 decimal_less(slipped->first, *reference);
501 if (favourable) {
502 slipped->first = *reference;
503 slipped->second = Decimal64::parse("0");
504 }
505 }
506 if (slipped && bound) {
507 // Negative slippage cannot carry a fill past the bar's opposite extreme either.
508 const auto& ceiling = order.side_ == OrderSide::SELL ? next.bar_.high_ : next.bar_.low_;
509 const bool past =
510 order.side_ == OrderSide::SELL ? decimal_less(ceiling, slipped->first) : decimal_less(slipped->first, ceiling);
511 if (past) {
512 const auto moved = order.side_ == OrderSide::SELL ?
513 decimal_subtract(*reference, ceiling) :
514 decimal_subtract(ceiling, *reference);
515 if (!moved) {
516 slipped.reset();
517 } else {
518 slipped->first = ceiling;
519 slipped->second = *moved;
520 decision.bounded_ = true;
521 }
522 }
523 }
524 if (slipped && bound) {
525 const bool beyond =
526 order.side_ == OrderSide::SELL ? decimal_less(slipped->first, *bound) : decimal_less(*bound, slipped->first);
527 if (beyond) {
528 const auto moved =
529 order.side_ == OrderSide::SELL ? decimal_subtract(*reference, *bound) : decimal_subtract(*bound, *reference);
530 if (!moved) {
531 slipped.reset();
532 } else {
533 slipped->first = *bound;
534 slipped->second = *moved;
535 decision.bounded_ = true;
536 }
537 }
538 }
539 if (!slipped) {
540 decision.still_working_ = rests_unfilled(order);
541 return decision;
542 }
543 const auto& charged_on = at_open && open_date ? *open_date : date;
544 auto charged = commission_.charge(order.quantity_, slipped->first, charged_on, order.instrument_.country_);
545 if (!charged) {
546 // A cost that cannot be resolved halts the engine; it is neither a non-fill nor silent.
547 decision.costing_error_ = std::move(charged.error());
548 return decision;
549 }
550
551 // The tax on a sale, where a rate was stated: of the sale's value, truncated to whole units.
552 std::optional<Decimal64> tax;
553 if (sell_tax_ && order.side_ == OrderSide::SELL && order.instrument_.country_ == sell_tax_->country_) {
554 const auto value = decimal_multiply(order.quantity_, slipped->first);
555 const auto charged = value ? decimal_multiply(*value, sell_tax_->rate_) : std::nullopt;
556 tax = charged ? decimal_floor_to_multiple(*charged, Decimal64::parse("1")) : std::nullopt;
557 if (!tax) {
558 // Not a tax of zero: the cost is unknown, so the engine halts.
559 BrokerError error;
561 error.reached_broker_ = false;
562 error.broker_code_ = "tax_not_representable";
563 error.message_ = "the tax on that sale does not fit an exact decimal";
564 decision.costing_error_ = std::move(error);
565 return decision;
566 }
567 }
568
569 Execution execution;
570 execution.fill_.instrument_ = order.instrument_;
571 execution.fill_.side_ = order.side_;
572 execution.fill_.quantity_ = order.quantity_;
573 execution.fill_.price_ = slipped->first;
574 execution.fill_.tag_ = order.tag_;
575 execution.fill_.at_ = next.period_end_;
576 execution.commission_ = charged.value().broker_;
577 execution.institution_fee_ = charged.value().institution_;
578 execution.applied_ = std::move(charged.value().applied_);
579 execution.slippage_ = slipped->second;
580 if (tax) {
581 execution.tax_ = *tax;
582 }
583 decision.execution_ = std::move(execution);
584 return decision;
585 }
586
587private:
588 // Where in a bar a dormant stop released, if it did.
589 enum class Release {
590 // The bar never reached the trigger.
591 NONE,
592 // The open was already through the trigger, so the whole bar followed the release.
593 AT_OPEN,
594 // The bar's extreme reached the trigger and its open did not.
595 INTRABAR,
596 };
597
598 // Returns whether, and where, `next` released a dormant stop.
599 //
600 // **A sell stop releases when the price falls to its trigger and a buy stop when it rises to it.**
601 // That is an assumption about the broker -- neither KIS nor Kiwoom documents the direction -- and
602 // it is the universal convention. `ReportCaveats::stop_releases_` counts how often it was used.
603 //
604 // A bar with no open cannot establish a release *at* the open, so a release there is intrabar,
605 // which is the answer that credits less.
606 static Release release_of(const strategy::PlannedOrder& order, const strategy::BarEvent& next) {
607 const auto& trigger = *order.trigger_price_;
608 const bool sell = order.side_ == OrderSide::SELL;
609 const bool reached = sell ? !decimal_less(trigger, next.bar_.low_) : !decimal_less(next.bar_.high_, trigger);
610 if (!reached) {
611 return Release::NONE;
612 }
613 if (next.bar_.open_) {
614 const auto& open = *next.bar_.open_;
615 const bool through = sell ? !decimal_less(trigger, open) : !decimal_less(open, trigger);
616 if (through) {
617 return Release::AT_OPEN;
618 }
619 }
620 return Release::INTRABAR;
621 }
622
623 // Returns whether the order's limit would trade at `price` -- a sell at or above it, a buy at or below.
624 static bool limit_marketable_at(const strategy::PlannedOrder& order, const Decimal64& price) {
625 const auto& limit = *order.price_;
626 return order.side_ == OrderSide::SELL ? !decimal_less(price, limit) : !decimal_less(limit, price);
627 }
628
629 // Returns what a stop released inside `next` is credited with, or nothing when the bar cannot show
630 // it traded after the release.
631 //
632 // At the release the price *was* the trigger. Two cases, using only prices the bar proves came after
633 // it -- the trigger itself and the close, which is always last:
634 //
635 // - **Marketable at the trigger** (a sell limited at or below it, a buy at or above): it trades. It is
636 // credited the **worst price the bar traded that the limit allows** -- a sell the higher of its
637 // limit and the bar's low, a buy the lower of its limit and the bar's high -- so the price is inside
638 // the bar and never through the limit. Crediting the limit itself could name a price the bar never
639 // reached; crediting the trigger would assume the book was there.
640 // - **Not marketable at the trigger** (a sell limited above it): it trades only if the price came back
641 // to the limit after the release. The close proves that when it is at or through the limit; then it
642 // is credited the limit. Otherwise the bar's other extreme may have come first, and nothing is.
643 static std::optional<Decimal64> intrabar_release_price(
644 const strategy::PlannedOrder& order, const strategy::BarEvent& next
645 ) {
646 const auto& trigger = *order.trigger_price_;
647 const auto& limit = *order.price_;
648 const bool sell = order.side_ == OrderSide::SELL;
649 if (limit_marketable_at(order, trigger)) {
650 if (sell) {
651 return decimal_less(limit, next.bar_.low_) ? next.bar_.low_ : limit;
652 }
653 return decimal_less(next.bar_.high_, limit) ? next.bar_.high_ : limit;
654 }
655 if (limit_marketable_at(order, next.bar_.close_)) {
656 return limit;
657 }
658 return std::nullopt;
659 }
660
661 // Whether a bar spans more than one trading day (a week, a month, a year).
662 static bool longer_than_a_day(BarInterval interval) noexcept {
663 switch (interval) {
664 case BarInterval::ONE_WEEK:
665 case BarInterval::ONE_MONTH:
666 case BarInterval::ONE_YEAR:
667 return true;
668 case BarInterval::ONE_MINUTE:
673 case BarInterval::ONE_DAY:
674 return false;
675 }
676 return true;
677 }
678
679 // Returns the price before slippage, or nothing when the order does not trade against this bar.
680 //
681 // A market order takes the open. A limit trades only if the bar's range reached it — a buy needs the
682 // low at or below the limit, a sell needs the high at or above it — because a bar that never touched
683 // the price is a bar in which the order sat unfilled, however close it came.
684 //
685 // Sets `at_open` when the price is the bar's stated open -- a market order, or a limit under
686 // `AT_OPEN_IF_BETTER` whose open was better -- which is what lets `decide` date the charge from the
687 // open's day. False for a bar with no open, whose close stands in for it.
688 std::optional<Decimal64> reference_price(
689 const strategy::PlannedOrder& order, const strategy::BarEvent& next, bool& at_open
690 ) const {
691 at_open = false;
692 // A bar with no open prices a market order at its close, which is not the open's moment.
693 const bool has_open = next.bar_.open_.has_value();
694 const auto open = has_open ? *next.bar_.open_ : next.bar_.close_;
695
696 // A released stop is priced as the limit it has become.
697 const bool limit_basis = limit_priced(order.type_);
698 if (!limit_basis || !order.price_) {
699 at_open = has_open;
700 return open;
701 }
702 const auto& limit = *order.price_;
703 bool open_if_better = false;
704 switch (limit_price_) {
706 open_if_better = true;
707 break;
709 break;
710 }
711
712 if (order.side_ == OrderSide::BUY) {
713 if (decimal_less(limit, next.bar_.low_)) {
714 return std::nullopt;
715 }
716 if (open_if_better && decimal_less(open, limit)) {
717 at_open = has_open;
718 return open;
719 }
720 return limit;
721 }
722
723 if (decimal_less(next.bar_.high_, limit)) {
724 return std::nullopt;
725 }
726 if (open_if_better && decimal_less(limit, open)) {
727 at_open = has_open;
728 return open;
729 }
730 return limit;
731 }
732
733 CommissionPolicy commission_;
734 SlippageModel slippage_;
735 LimitFillPrice limit_price_;
736 std::optional<SellTax> sell_tax_;
737 std::string market_price_name_;
738 MarketPriceFunction market_price_;
739 // Set only by `MarketPrice::NEXT_CLOSE`: refuse to price a bar longer than a day.
740 bool next_close_{false};
741 // The library's rule in force; empty under a caller's own function.
742 std::optional<MarketPrice> library_rule_{MarketPrice::NEXT_OPEN};
743};
744
745} // namespace backtest
746} // namespace algotradelib
An exact signed decimal: an int64 coefficient times 10^-scale.
Definition types.hpp:443
static Decimal64 parse(std::string_view input)
Parses a decimal string exactly (no binary floating point).
Definition types.hpp:468
How a backtest charges commission: one stated model, or a schedule resolved per execution.
Definition cost.hpp:185
const CommissionPolicy & commission() const noexcept
Returns the commission policy.
Definition fill.hpp:174
NextBarFillModel(CommissionPolicy commission, SlippageModel slippage=SlippageModel::none(), LimitFillPrice limit_price=LimitFillPrice::AT_LIMIT)
Constructs a model with a commission policy, which may resolve a schedule per execution.
Definition fill.hpp:164
bool has_market_price_rule() const noexcept
Tests whether a market-price rule is set.
Definition fill.hpp:300
const std::optional< MarketPrice > & library_market_price() const noexcept
Returns which of the library's market-price rules prices market orders.
Definition fill.hpp:309
NextBarFillModel with_sell_tax(SellTax tax) const
Returns a copy of this model that charges a tax on sales.
Definition fill.hpp:206
NextBarFillModel with_market_price(std::string name, MarketPriceFunction price) const
Returns a copy of this model that prices market orders by a rule instead of the next bar's open.
Definition fill.hpp:240
const std::optional< SellTax > & sell_tax() const noexcept
Returns the tax on sales.
Definition fill.hpp:219
NextBarFillModel(CommissionModel commission=CommissionModel::none(), SlippageModel slippage=SlippageModel::none(), LimitFillPrice limit_price=LimitFillPrice::AT_LIMIT)
Constructs a model with a flat commission.
Definition fill.hpp:150
FillDecision decide(const strategy::PlannedOrder &order, const strategy::BarEvent &next, const PriceRule &rule, const Date &date, const std::optional< Date > &open_date=std::nullopt, bool released=false, const std::optional< Decimal64 > &decision_price=std::nullopt, const std::optional< std::chrono::system_clock::time_point > &decision_price_at=std::nullopt, const std::optional< std::chrono::system_clock::time_point > &decided_at=std::nullopt) const
Decides whether an order trades against a bar, and at what price and cost.
Definition fill.hpp:393
static bool rests_unfilled(const strategy::PlannedOrder &order) noexcept
Tests whether an order that did not fill stays queued for a later bar of its session.
Definition fill.hpp:346
NextBarFillModel with_market_price(MarketPrice price) const
Returns a copy of this model that prices market orders by a rule the library ships.
Definition fill.hpp:262
LimitFillPrice limit_price() const noexcept
Returns which price a filled limit order is credited.
Definition fill.hpp:190
static constexpr bool limit_priced(OrderType type) noexcept
Tests whether the model prices an order type against its limit (a limit order, and a stop-limit once ...
Definition fill.hpp:319
const std::string & market_price_rule() const noexcept
Returns the market-price rule's name.
Definition fill.hpp:292
const SlippageModel & slippage() const noexcept
Returns the slippage model.
Definition fill.hpp:182
Trading costs for a backtest: commission (CommissionModel, CommissionPolicy), slippage (SlippageModel...
The events a runner delivers to a strategy's handlers, and the market phase.
The replay ledger: positions, cash and realized profit a backtest keeps, in exact decimals.
Replays a strategy over stored bars: fill and cost models, the replay ledger, and the report.
Definition bootstrap.hpp:16
std::function< std::optional< Decimal64 >(const MarketPriceInput &)> MarketPriceFunction
A market-price rule: the price a market order trades at against next_, before slippage,...
Definition fill.hpp:87
MarketPrice
A market-price rule the library ships; see NextBarFillModel::with_market_price(MarketPrice).
Definition fill.hpp:90
@ NEXT_CLOSE
A market order fills at the close of the bar it fills against, named next-close in the report.
Definition fill.hpp:124
@ DECISION_CLOSE
A market order fills at the price it was decided on, named decision-close in the report.
Definition fill.hpp:106
@ NEXT_OPEN
The default: a market order fills at the next bar's open (its close if it has no open).
Definition fill.hpp:92
@ NONE
No points, for a run that does not read the curve; summarize then reports no ratio.
Definition engine.hpp:655
LimitFillPrice
Which price a backtest credits a limit order that fills.
Definition cost.hpp:565
@ AT_OPEN_IF_BETTER
Fill at the bar's open when it opened through the limit (assumes the order was resting before the ope...
Definition cost.hpp:579
@ AT_LIMIT
The default: fill at the limit, or at the bar's worst traded price when the whole bar was better than...
Definition cost.hpp:574
The library: broker-neutral types (core), and the modules below.
std::optional< Decimal64 > decimal_floor_to_multiple(const Decimal64 &value, const Decimal64 &step)
Rounds down to a multiple of a step (toward negative infinity).
Definition types.hpp:903
@ INVALID_ARGUMENT
The request was invalid, refused by the client before sending or by the broker.
Definition types.hpp:974
OrderType
How an order's price is decided (its price basis).
Definition types.hpp:1998
@ LIMIT
지정가: fills at the stated price or better.
Definition types.hpp:2001
@ BEST_LIMIT
최유리지정가: priced at the best quote on the opposite side.
Definition types.hpp:2007
@ PRIMARY_LIMIT
최우선지정가: priced at the best quote on the order's own side.
Definition types.hpp:2009
@ CONDITIONAL_LIMIT
조건부지정가: a limit that becomes a market order at the close.
Definition types.hpp:2005
@ MID_PRICE
중간가: priced at the midpoint of the spread.
Definition types.hpp:2011
@ STOP_LIMIT
스톱지정가: a limit released when a trigger price is reached.
Definition types.hpp:2013
@ CLOSING_PRICE
시간외종가: priced at a session's closing price.
Definition types.hpp:2015
@ MARKET
시장가: fills at whatever the book offers.
Definition types.hpp:2003
constexpr bool carries_trigger_price(OrderType type) noexcept
Tests whether an order type takes a trigger price.
Definition types.hpp:2065
Country
A country (market) the library supports.
Definition types.hpp:32
std::optional< Decimal64 > decimal_subtract(const Decimal64 &lhs, const Decimal64 &rhs)
Subtracts two decimals exactly.
Definition types.hpp:783
bool decimal_less(const Decimal64 &lhs, const Decimal64 &rhs)
Compares two decimals numerically, whatever their scales.
Definition types.hpp:926
std::optional< Decimal64 > decimal_multiply(const Decimal64 &lhs, const Decimal64 &rhs)
Multiplies two decimals exactly; the scales add.
Definition types.hpp:826
BarInterval
The period one bar covers.
@ FIVE_MINUTES
Five minutes.
@ THIRTY_MINUTES
Thirty minutes.
@ FIFTEEN_MINUTES
Fifteen minutes.
@ FILL_OR_KILL
Fills entirely and immediately, or not at all.
Definition types.hpp:2095
@ DAY
Rests until the session ends; an unfilled remainder is cancelled then.
Definition types.hpp:2091
@ IMMEDIATE_OR_CANCEL
Fills what it can immediately and cancels the remainder.
Definition types.hpp:2093
Reconciliation: turning a strategy's intents and targets into the orders and cancellations to send.
Decimal64 high_
The highest trade price.
Decimal64 close_
The last trade price.
std::optional< Decimal64 > open_
The first trade's price; empty if nothing traded in the period (never zero or the previous close).
Decimal64 low_
The lowest trade price.
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 calendar date with no time of day or time zone.
Definition types.hpp:1412
Country country_
The listing country, which selects the symbol convention and the price grid.
Definition types.hpp:87
The price grid (tick sizes) an instrument's order prices must sit on.
A flat commission: a rate on traded value, with an optional per-execution minimum.
Definition cost.hpp:28
static CommissionModel none()
Returns a model that charges nothing (the report says so).
Definition cost.hpp:67
One execution a backtest produced, and what it cost.
Definition ledger.hpp:37
std::optional< Decimal64 > institution_fee_
The institution fee (유관기관제비용), if the rate was split; empty is not zero.
Definition ledger.hpp:47
Decimal64 commission_
The broker's commission; includes the institution fee when institution_fee_ is empty.
Definition ledger.hpp:41
AppliedRate applied_
The commission rate that charged this execution.
Definition ledger.hpp:51
Decimal64 slippage_
The price given up to slippage (already included in the fill price).
Definition ledger.hpp:53
strategy::Fill fill_
The fill, as on_fill receives it.
Definition ledger.hpp:39
Decimal64 tax_
The sale tax charged; zero unless a sell tax applies (NextBarFillModel::with_sell_tax).
Definition ledger.hpp:59
What NextBarFillModel::decide concluded about one order and one bar.
Definition fill.hpp:30
std::optional< BrokerError > costing_error_
Why a fill could not be costed (no commission rate, or a tax that does not fit).
Definition fill.hpp:40
bool still_working_
Whether the order stays queued for a later bar (it did not trade but may still).
Definition fill.hpp:34
bool bounded_
Whether a limit-type fill price was moved to stay within the bar's range and the limit; counted by th...
Definition fill.hpp:52
bool released_
Whether this bar released a dormant stop-limit order (whether or not it also filled).
Definition fill.hpp:42
std::optional< Execution > execution_
The execution, if the order traded against this bar.
Definition fill.hpp:32
bool intrabar_release_fill_
Whether the fill depended on a stop released inside the bar, where the bar cannot show the order of p...
Definition fill.hpp:47
bool priced_by_rule_
Whether the price was chosen by the model's market-price rule; counted by the report.
Definition fill.hpp:54
What a market-price rule is given (see NextBarFillModel::with_market_price).
Definition fill.hpp:58
const std::optional< std::chrono::system_clock::time_point > & decided_at_
When the strategy decided the order, if known.
Definition fill.hpp:73
const std::optional< std::chrono::system_clock::time_point > & decision_price_at_
When decision_price_ was observed (the bar's period end, or the quote's time); older than decided_at_...
Definition fill.hpp:71
const strategy::PlannedOrder & order_
The order to price: a market order, or any other type the model does not price against a limit.
Definition fill.hpp:60
const strategy::BarEvent & next_
The bar the order fills against.
Definition fill.hpp:75
const std::optional< Decimal64 > & decision_price_
The instrument's last price when the strategy decided the order (the close of the bar it decided on,...
Definition fill.hpp:66
const PriceRule & rule_
The instrument's price grid.
Definition fill.hpp:77
A tax on sales, stated by the caller; see NextBarFillModel::with_sell_tax.
Definition fill.hpp:128
Country country_
The market whose sales pay it.
Definition fill.hpp:132
Decimal64 rate_
The fraction of a sale's value charged, such as 0.0020 for 0.20%.
Definition fill.hpp:130
Slippage: how far a fill price moves against the order (a buy pays more, a sell gets less).
Definition cost.hpp:440
static SlippageModel none()
Returns a model with no slippage (the report says so).
Definition cost.hpp:523
One bar, delivered to on_bar.
Definition event.hpp:313
std::chrono::system_clock::time_point period_end_
When the bar's period ended: the earliest moment the bar could be known.
Definition event.hpp:329
BarInterval interval_
The period length the bar covers.
Definition event.hpp:317
std::chrono::system_clock::time_point at_
When the execution happened.
Definition event.hpp:900
InstrumentId instrument_
The instrument traded.
Definition event.hpp:890
std::string tag_
The tag of the intent that produced the order.
Definition event.hpp:898
OrderSide side_
The side of the order.
Definition event.hpp:892
Decimal64 price_
The execution price.
Definition event.hpp:896
Decimal64 quantity_
The quantity of this execution, not the order's running total.
Definition event.hpp:894
One order the runner is to place.
TimeInForce time_in_force_
How long the order stays valid.
std::string tag_
The tag of the intent or target the order came from.
std::optional< Decimal64 > trigger_price_
The trigger price of a STOP_LIMIT order, exactly as stated; empty otherwise.
OrderType type_
The order type.
InstrumentId instrument_
The instrument to trade.
std::optional< Decimal64 > price_
The limit price, after clamping and rounding; empty for a market order.
Decimal64 quantity_
The quantity, after rounding onto the lot.