algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::PriceRule Struct Reference

The price grid (tick sizes) an instrument's order prices must sit on. More...

#include <algotradelib/src/core/instrument.hpp>

Public Types

enum class  Kind { STATED , UNCONSTRAINED , UNKNOWN }
 Whether the rule states a grid, imposes none, or does not know it. More...

Public Member Functions

constexpr bool grid_known () const noexcept
 Tests whether the rule says anything about the grid: a stated one, or none at all.
std::optional< Decimal64 > tick_at (const Decimal64 &price) const
 Returns the tick size at a price.
std::optional< bool > on_tick (const Decimal64 &price) const
 Tests whether a price is on the grid.
std::optional< Decimal64 > round_to_tick (const Decimal64 &price, OrderSide side) const
 Rounds a price to its band's tick in the order's favour: a buy rounds down, a sell up.

Static Public Member Functions

static PriceRule unconstrained ()
 Returns a rule with no grid, for a market quoted in free decimals.
static PriceRule unknown (std::string reason)
 Returns a rule that knows no grid and refuses to round.

Public Attributes

Kind kind_ {Kind::UNKNOWN}
 The kind of rule; UNKNOWN by default, so an unset rule refuses to round.
std::vector< TickBand > bands_
 The bands, ascending by from_; empty unless kind_ is STATED.
std::string source_
 Where the table came from and its revision, or why there is none.

Detailed Description

The price grid (tick sizes) an instrument's order prices must sit on.

A broker states at most the tick at one price, not the ladder, so the library maintains the tables, beside the adapters that attach them (source_ names the table and its revision). Replace a rule if the exchange revises its grid. A rule of kind UNKNOWN refuses to check or round a price. A custom table needs positive ticks and bands sorted by from_.

Definition at line 150 of file instrument.hpp.

Member Enumeration Documentation

◆ Kind

enum class algotradelib::PriceRule::Kind
strong

Whether the rule states a grid, imposes none, or does not know it.

Enumerator
STATED 

bands_ describe the market's grid.

UNCONSTRAINED 

The market imposes no grid (free decimals).

UNKNOWN 

The library has no grid for this instrument and will not guess one.

Definition at line 152 of file instrument.hpp.

Member Function Documentation

◆ grid_known()

bool algotradelib::PriceRule::grid_known ( ) const
inlineconstexprnoexcept

Tests whether the rule says anything about the grid: a stated one, or none at all.

Returns
false for UNKNOWN. A STATED and an UNCONSTRAINED rule are both read from bands_.

Definition at line 174 of file instrument.hpp.

Referenced by on_tick(), and round_to_tick().

◆ on_tick()

std::optional< bool > algotradelib::PriceRule::on_tick ( const Decimal64 & price) const
inline

Tests whether a price is on the grid.

Parameters
priceThe price.
Returns
std::nullopt if the grid is unknown (kind UNKNOWN) or the remainder does not fit a Decimal64; true if price is on its band's tick, or there is no band at price (an unconstrained rule); otherwise false.

Definition at line 209 of file instrument.hpp.

Referenced by algotradelib::strategy::Price::checked(), and algotradelib::strategy::reconcile().

◆ round_to_tick()

std::optional< Decimal64 > algotradelib::PriceRule::round_to_tick ( const Decimal64 & price,
OrderSide side ) const
inline

Rounds a price to its band's tick in the order's favour: a buy rounds down, a sell up.

The tick is the one at price; a rounded price that crosses into the next band is not re-rounded. That is correct when every band boundary is a multiple of the ticks on both sides of it; with a custom table that is not, check the result with on_tick.

Parameters
priceThe price.
sideThe side of the order.
Returns
The rounded price (unchanged if already on the grid or there is no band at price), or std::nullopt if the grid is unknown or the arithmetic does not fit.

Definition at line 235 of file instrument.hpp.

Referenced by algotradelib::analytics::round_scalar_to_tick(), and algotradelib::strategy::Price::rounded().

◆ tick_at()

std::optional< Decimal64 > algotradelib::PriceRule::tick_at ( const Decimal64 & price) const
inline

Returns the tick size at a price.

Parameters
priceThe price.
Returns
The tick of the band containing price, or std::nullopt if there are no bands or price is below the first.

Definition at line 191 of file instrument.hpp.

Referenced by algotradelib::backtest::SlippageModel::apply(), on_tick(), and round_to_tick().

◆ unconstrained()

PriceRule algotradelib::PriceRule::unconstrained ( )
inlinestatic

Returns a rule with no grid, for a market quoted in free decimals.

Returns
The rule, of kind UNCONSTRAINED.

Definition at line 264 of file instrument.hpp.

◆ unknown()

PriceRule algotradelib::PriceRule::unknown ( std::string reason)
inlinestatic

Returns a rule that knows no grid and refuses to round.

Parameters
reasonWhy there is no grid; stored in source_.
Returns
The rule, of kind UNKNOWN.

Definition at line 276 of file instrument.hpp.

Referenced by algotradelib::krx::price_rule_for().

Member Data Documentation

◆ bands_

std::vector<TickBand> algotradelib::PriceRule::bands_

The bands, ascending by from_; empty unless kind_ is STATED.

Definition at line 166 of file instrument.hpp.

Referenced by algotradelib::krx::equity_price_rule(), algotradelib::us::equity_price_rule(), algotradelib::krx::fund_price_rule(), and tick_at().

◆ kind_

Kind algotradelib::PriceRule::kind_ {Kind::UNKNOWN}

The kind of rule; UNKNOWN by default, so an unset rule refuses to round.

Definition at line 164 of file instrument.hpp.

Referenced by algotradelib::krx::equity_price_rule(), algotradelib::us::equity_price_rule(), algotradelib::krx::fund_price_rule(), grid_known(), unconstrained(), and unknown().

◆ source_

std::string algotradelib::PriceRule::source_

Where the table came from and its revision, or why there is none.

Definition at line 168 of file instrument.hpp.

Referenced by algotradelib::krx::equity_price_rule(), algotradelib::us::equity_price_rule(), algotradelib::krx::fund_price_rule(), unconstrained(), and unknown().


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