algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::backtest::NextBarFillModel Class Reference

The fill model: market orders at the next bar's open, limit orders when the next bar's range reaches them, stop-limit orders once released. More...

#include <algotradelib/src/backtest/fill.hpp>

Public Member Functions

 NextBarFillModel (CommissionModel commission=CommissionModel::none(), SlippageModel slippage=SlippageModel::none(), LimitFillPrice limit_price=LimitFillPrice::AT_LIMIT)
 Constructs a model with a flat commission.
 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.
const CommissionPolicy & commission () const noexcept
 Returns the commission policy.
const SlippageModel & slippage () const noexcept
 Returns the slippage model.
LimitFillPrice limit_price () const noexcept
 Returns which price a filled limit order is credited.
NextBarFillModel with_sell_tax (SellTax tax) const
 Returns a copy of this model that charges a tax on sales.
const std::optional< SellTax > & sell_tax () const noexcept
 Returns the tax on sales.
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.
NextBarFillModel with_market_price (MarketPrice price) const
 Returns a copy of this model that prices market orders by a rule the library ships.
const std::string & market_price_rule () const noexcept
 Returns the market-price rule's name.
bool has_market_price_rule () const noexcept
 Tests whether a market-price rule is set.
const std::optional< MarketPrice > & library_market_price () const noexcept
 Returns which of the library's market-price rules prices market orders.
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.

Static Public Member Functions

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 released).
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.

Detailed Description

The fill model: market orders at the next bar's open, limit orders when the next bar's range reaches them, stop-limit orders once released.

Works the same for any bar interval. Volume is not consulted: a fill is never partial or capped by what traded (the report states this).

Definition at line 142 of file fill.hpp.

Constructor & Destructor Documentation

◆ NextBarFillModel() [1/2]

algotradelib::backtest::NextBarFillModel::NextBarFillModel ( CommissionModel commission = CommissionModel::none(),
SlippageModel slippage = SlippageModel::none(),
LimitFillPrice limit_price = LimitFillPrice::AT_LIMIT )
inlineexplicit

Constructs a model with a flat commission.

Parameters
commissionThe commission model, charged on every execution.
slippageThe slippage model.
limit_priceWhich price a filled limit order is credited.

Definition at line 150 of file fill.hpp.

Referenced by with_market_price(), with_market_price(), and with_sell_tax().

◆ NextBarFillModel() [2/2]

algotradelib::backtest::NextBarFillModel::NextBarFillModel ( CommissionPolicy commission,
SlippageModel slippage = SlippageModel::none(),
LimitFillPrice limit_price = LimitFillPrice::AT_LIMIT )
inlineexplicit

Constructs a model with a commission policy, which may resolve a schedule per execution.

Parameters
commissionThe commission policy.
slippageThe slippage model.
limit_priceWhich price a filled limit order is credited.

Definition at line 164 of file fill.hpp.

Member Function Documentation

◆ commission()

const CommissionPolicy & algotradelib::backtest::NextBarFillModel::commission ( ) const
inlinenoexcept

Returns the commission policy.

Returns
The policy in force.

Definition at line 174 of file fill.hpp.

Referenced by NextBarFillModel(), and NextBarFillModel().

◆ decide()

FillDecision algotradelib::backtest::NextBarFillModel::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
inline

Decides whether an order trades against a bar, and at what price and cost.

  • Market: the bar's open (its close if it has no open), or the market-price rule's price when one is set (with_market_price).
  • Limit: only if the bar reached the limit (low at or below a buy limit, high at or above a sell limit). Credited the limit, or under AT_OPEN_IF_BETTER the open if better; the credit is kept within the bar's range and never through the limit, even after slippage.
  • Stop-limit: dormant until the bar reaches the trigger (a sell stop when the low reaches it, a buy stop when the high does). Released at the open, it is priced as a limit. Released inside the bar, it fills only when the bar proves a price after the release: at the worst bar price the limit allows if the limit was marketable at the trigger, at the limit if the close is through the limit; otherwise it rests. No favourable slippage applies after an intrabar release.

Slippage, then commission and any sale tax, are applied to a fill. The execution is stamped at next.period_end_.

Parameters
orderThe queued order.
nextThe bar after the one the order was decided on.
ruleThe instrument's price grid, for tick slippage.
dateThe market-local trading date of the fill, for the commission rate (see SessionWindow::local_date).
open_dateThe market-local date of the bar's open, if known; used instead of date for a fill priced at the open. For a fill inside a multi-day bar date is used, which can be a later day than the fill.
releasedWhether an earlier bar already released this stop-limit order.
decision_priceThe instrument's last price when the order was decided, for a market-price rule.
decision_price_atWhen that price was observed, for a market-price rule.
decided_atWhen the order was decided, for a market-price rule.
Returns
The decision. A market-price rule's price that is not positive is a costing error (code market_price_not_positive), so the replay halts. A stop-limit order not yet released without both a trigger and a limit price neither trades nor rests (a released stop must still have its limit price).

Definition at line 393 of file fill.hpp.

◆ has_market_price_rule()

bool algotradelib::backtest::NextBarFillModel::has_market_price_rule ( ) const
inlinenoexcept

Tests whether a market-price rule is set.

Returns
true if one is.

Definition at line 300 of file fill.hpp.

◆ library_market_price()

const std::optional< MarketPrice > & algotradelib::backtest::NextBarFillModel::library_market_price ( ) const
inlinenoexcept

Returns which of the library's market-price rules prices market orders.

Returns
The rule, MarketPrice::NEXT_OPEN when none is set; std::nullopt when a caller's own function (with_market_price(std::string, MarketPriceFunction)) does, whatever its name.

Definition at line 309 of file fill.hpp.

◆ limit_price()

LimitFillPrice algotradelib::backtest::NextBarFillModel::limit_price ( ) const
inlinenoexcept

Returns which price a filled limit order is credited.

Returns
The choice.

Definition at line 190 of file fill.hpp.

Referenced by NextBarFillModel(), and NextBarFillModel().

◆ limit_priced()

constexpr bool algotradelib::backtest::NextBarFillModel::limit_priced ( OrderType type)
inlinestaticconstexprnoexcept

Tests whether the model prices an order type against its limit (a limit order, and a stop-limit once released).

Parameters
typeThe order type.
Returns
true for LIMIT and STOP_LIMIT; the model fills every other type it accepts at the open.

Definition at line 319 of file fill.hpp.

Referenced by decide(), and rests_unfilled().

◆ market_price_rule()

const std::string & algotradelib::backtest::NextBarFillModel::market_price_rule ( ) const
inlinenoexcept

Returns the market-price rule's name.

Returns
The name, or empty when market orders take the next bar's open.

Definition at line 292 of file fill.hpp.

◆ rests_unfilled()

bool algotradelib::backtest::NextBarFillModel::rests_unfilled ( const strategy::PlannedOrder & order)
inlinestaticnoexcept

Tests whether an order that did not fill stays queued for a later bar of its session.

Only DAY limit and stop-limit orders rest; immediate-or-cancel, fill-or-kill and market orders do not. Backtester withdraws a resting DAY order when its session ends (on daily bars, after one bar).

Parameters
orderThe order.
Returns
true if it rests.

Definition at line 346 of file fill.hpp.

Referenced by decide().

◆ sell_tax()

const std::optional< SellTax > & algotradelib::backtest::NextBarFillModel::sell_tax ( ) const
inlinenoexcept

Returns the tax on sales.

Returns
The tax, or std::nullopt if none is modelled.

Definition at line 219 of file fill.hpp.

◆ slippage()

const SlippageModel & algotradelib::backtest::NextBarFillModel::slippage ( ) const
inlinenoexcept

Returns the slippage model.

Returns
The model in force.

Definition at line 182 of file fill.hpp.

Referenced by NextBarFillModel(), and NextBarFillModel().

◆ with_market_price() [1/2]

NextBarFillModel algotradelib::backtest::NextBarFillModel::with_market_price ( MarketPrice price) const
inline

Returns a copy of this model that prices market orders by a rule the library ships.

MarketPrice::NEXT_OPEN removes any rule set before; MarketPrice::DECISION_CLOSE and MarketPrice::NEXT_CLOSE set the rules named decision-close and next-close, priced, costed and dated as with_market_price(std::string, MarketPriceFunction) describes.

Parameters
priceThe rule.
Returns
The new model.

Definition at line 262 of file fill.hpp.

◆ with_market_price() [2/2]

NextBarFillModel algotradelib::backtest::NextBarFillModel::with_market_price ( std::string name,
MarketPriceFunction price ) const
inline

Returns a copy of this model that prices market orders by a rule instead of the next bar's open.

The rule is called for every order the model does not price against a limit (limit_priced false), with the order, the instrument's previous bar and the bar it fills against. Its price is then slipped, costed and taxed as an open would be, and the fill is stamped at next.period_end_ and charged on the fill bar's date. When it returns nothing, a DAY order rests for a later bar of its session and any other order is withdrawn. Limit and stop-limit orders are untouched.

The rule may look at next_ beyond its open (its close, high or low): that is a choice the rule makes, not one the model can check, and the report names the rule so a reader can judge it.

Parameters
nameA name for the report (ReportCaveats::market_price_rule_).
priceThe rule.
Returns
The new model.
Exceptions
std::invalid_argumentif the name is empty or the rule is empty.

Definition at line 240 of file fill.hpp.

◆ with_sell_tax()

NextBarFillModel algotradelib::backtest::NextBarFillModel::with_sell_tax ( SellTax tax) const
inline

Returns a copy of this model that charges a tax on sales.

The library has no tax rate of its own (Korean securities transaction tax changes yearly); one rate applies to the whole replay. The tax is truncated to whole currency units and reported apart from commission (Execution::tax_, Report::tax_paid_). Only sales of instruments in tax.country_ pay it; exempt instruments (Korean ETFs and ETNs) need a replay without it.

Parameters
taxThe rate and market.
Returns
The new model.
Exceptions
std::invalid_argumentif the rate is below 0 or not below 1.

Definition at line 206 of file fill.hpp.


The documentation for this class was generated from the following file: