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

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.

Detailed Description

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

Definition at line 297 of file types.hpp.

Constructor & Destructor Documentation

◆ Decimal64() [1/2]

◆ Decimal64() [2/2]

algotradelib::Decimal64::Decimal64 ( std::int64_t coefficient,
std::uint8_t scale )
inline

Constructs coefficient * 10^-scale, keeping the scale as given.

Parameters
coefficientThe integer coefficient.
scaleThe number of fractional digits.

Definition at line 307 of file types.hpp.

Member Function Documentation

◆ coefficient()

◆ parse()

Decimal64 algotradelib::Decimal64::parse ( std::string_view input)
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").

Parameters
inputThe text, such as "-12.345".
Returns
The value.
Exceptions
std::invalid_argumentif input is empty, has no digits, has more than one ., or contains any other character.
std::out_of_rangeif 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().

◆ scale()

std::uint8_t algotradelib::Decimal64::scale ( ) const
inlinenoexcept

◆ str()

std::string algotradelib::Decimal64::str ( ) const
inline

Formats the value as a decimal string.

Returns
The text, with exactly scale() fractional digits (no exponent).

Definition at line 411 of file types.hpp.

Referenced by algotradelib::runner::journal_entry_to_line().

◆ operator==

bool operator== ( const Decimal64 & lhs,
const Decimal64 & rhs )
friend

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.

Parameters
lhsThe first value.
rhsThe second value.
Returns
true if coefficient and scale are both equal.

Definition at line 441 of file types.hpp.


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