![]() |
algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
|
An exact signed decimal: an int64 coefficient times 10^-scale. More...
#include <algotradelib/src/core/types.hpp>
Public Member Functions | |
| Decimal64 ()=default | |
| Constructs zero. | |
| Decimal64 (std::int64_t coefficient, std::uint8_t scale) | |
| Constructs coefficient * 10^-scale, keeping the scale as given. | |
| std::int64_t | coefficient () const noexcept |
| Returns the coefficient. | |
| std::uint8_t | scale () const noexcept |
| Returns the scale. | |
| std::string | str () const |
| Formats the value as a decimal string. | |
Static Public Member Functions | |
| static Decimal64 | parse (std::string_view input) |
| Parses a decimal string exactly (no binary floating point). | |
Friends | |
| bool | operator== (const Decimal64 &lhs, const Decimal64 &rhs) |
| Compares the coefficient and the scale. | |
An exact signed decimal: an int64 coefficient times 10^-scale.
Prices, quantities and money are Decimal64 wherever they come from or go to a broker. The arithmetic is the decimal_* free functions, which return std::nullopt when a coefficient, scale or intermediate value does not fit (a result whose final value would fit can still fail).
|
default |
Constructs zero.
Referenced by algotradelib::decimal_add(), algotradelib::decimal_divide(), algotradelib::decimal_floor_to_multiple(), algotradelib::decimal_halve(), algotradelib::decimal_multiply(), algotradelib::decimal_normalized(), algotradelib::decimal_subtract(), algotradelib::strategy::RiskGuard::evaluate(), algotradelib::strategy::vanilla::EqualWeightRebalance::on_phase(), operator==, parse(), algotradelib::analytics::round_scalar_to_tick(), and algotradelib::analytics::to_decimal().
|
inline |
Constructs coefficient * 10^-scale, keeping the scale as given.
| coefficient | The integer coefficient. |
| scale | The number of fractional digits. |
|
inlinenoexcept |
Returns the coefficient.
Definition at line 399 of file types.hpp.
Referenced by algotradelib::backtest::SlippageModel::apply(), Decimal64(), algotradelib::decimal_halve(), algotradelib::decimal_less(), algotradelib::decimal_multiply(), algotradelib::decimal_normalized(), parse(), algotradelib::strategy::reconcile(), algotradelib::analytics::to_scalar(), and algotradelib::kiwoom::valid_trigger_value().
|
inlinestatic |
Parses a decimal string exactly (no binary floating point).
Accepts an optional sign, digits and at most one .. Trailing fractional zeros are removed, so parse("1.50") == parse("1.5").
| input | The text, such as "-12.345". |
| std::invalid_argument | if input is empty, has no digits, has more than one ., or contains any other character. |
| std::out_of_range | if the digits as written (before trailing zeros are removed) or the scale do not fit. |
Definition at line 323 of file types.hpp.
Referenced by algotradelib::backtest::ReplayLedger::apply(), algotradelib::backtest::SlippageModel::apply(), algotradelib::backtest::CommissionPolicy::charge(), algotradelib::strategy::closes_toward_flat(), algotradelib::backtest::NextBarFillModel::decide(), algotradelib::backtest::ReplayLedger::equity_in(), algotradelib::strategy::RiskGuard::evaluate(), algotradelib::PriceRule::krx_equity(), algotradelib::strategy::Portfolio< Broker >::net_quantity_of(), algotradelib::backtest::CommissionModel::of(), algotradelib::backtest::CommissionModel::on(), algotradelib::strategy::vanilla::DonchianBreakout::on_bar(), algotradelib::strategy::vanilla::MovingAverageCrossover::on_bar(), algotradelib::strategy::vanilla::RsiThreshold::on_bar(), algotradelib::strategy::vanilla::ZScoreReversion::on_bar(), algotradelib::strategy::vanilla::EqualWeightRebalance::on_phase(), algotradelib::strategy::projected_gross_exposure(), algotradelib::backtest::ReplayLedger::realized_in(), algotradelib::strategy::reconcile(), algotradelib::runner::ChargedCosts::since(), algotradelib::strategy::weight_target(), algotradelib::backtest::NextBarFillModel::with_sell_tax(), and algotradelib::strategy::Portfolio< Broker >::working_quantity().
|
inlinenoexcept |
Returns the scale.
Definition at line 405 of file types.hpp.
Referenced by Decimal64(), algotradelib::decimal_halve(), algotradelib::decimal_less(), algotradelib::decimal_multiply(), algotradelib::decimal_normalized(), parse(), algotradelib::analytics::to_scalar(), and algotradelib::kiwoom::valid_trigger_value().
|
inline |
Formats the value as a decimal string.
Definition at line 411 of file types.hpp.
Referenced by algotradelib::runner::journal_entry_to_line().
Compares the coefficient and the scale.
Values from parse and the decimal_* functions are normalized, so for them this compares the numbers. A value built with an explicit scale is not: Decimal64(150, 2) does not equal Decimal64(15, 1). To compare numbers at any scale, use decimal_less both ways or decimal_normalized first.
| lhs | The first value. |
| rhs | The second value. |