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 krx_equity ()
 Returns the KRX cash-equity tick table (January 2023 revision).
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 these tables (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 (for example a KRX ETF or ETN) 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 175 of file instrument.hpp.

Referenced by on_tick(), and round_to_tick().

◆ krx_equity()

PriceRule algotradelib::PriceRule::krx_equity ( )
inlinestatic

Returns the KRX cash-equity tick table (January 2023 revision).

Returns
The rule.

Definition at line 266 of file instrument.hpp.

Referenced by algotradelib::price_rule_for().

◆ 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 210 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 for the KRX table; with a custom table whose band boundaries are not multiples of both ticks, 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 237 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 192 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 286 of file instrument.hpp.

Referenced by algotradelib::price_rule_for().

◆ 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 298 of file instrument.hpp.

Referenced by algotradelib::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 167 of file instrument.hpp.

Referenced by krx_equity(), 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 165 of file instrument.hpp.

Referenced by grid_known(), krx_equity(), 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 169 of file instrument.hpp.

Referenced by krx_equity(), unconstrained(), and unknown().


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