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.
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
 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 68 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 76 of file fill.hpp.

Referenced by 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 91 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 104 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
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).
  • 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.
Returns
The decision. 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 220 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 116 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 151 of file fill.hpp.

Referenced by decide(), and rests_unfilled().

◆ 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 178 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 143 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 110 of file fill.hpp.

Referenced by NextBarFillModel(), and NextBarFillModel().

◆ 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 130 of file fill.hpp.


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