Expand description
Parameter value types for trading operations.
This module provides domain-specific, type-safe financial values.
Decimal-based numeric types use exact decimal arithmetic and never use
floating-point arithmetic internally.
Leverage is represented as fixed-point integer with scale 10.
Prefer exact constructors such as from_str or from_decimal_rounded in
domain code. from_f64 and related helpers exist for integration
boundaries that already expose floating-point inputs.
§Type categories
- Unsigned types (
Quantity,Volume,Notional) — cannot be negative. - Signed types (
Price,Pnl,CashFlow,PositionSize,Fee) — can be negative. - Fixed-point types (
Leverage) — scaled integer domain values. - Identifiers (
Asset,AccountId,Side,PositionSide) — non-numeric types.
§Rounding
Value types support rounding during construction via RoundingStrategy.
Scale and rounding strategy must be explicitly provided when using rounded constructors.
use openpit::param::{Price, RoundingStrategy};
let price = Price::from_str_rounded("100.126", 2, RoundingStrategy::DEFAULT)?;
assert_eq!(price.to_string(), "100.13");
let price = Price::from_str_rounded("100.125", 2, RoundingStrategy::BANKER)?;
assert_eq!(price.to_string(), "100.12");
let profit = Price::from_str_rounded("123.456", 2, RoundingStrategy::CONSERVATIVE_PROFIT)?;
assert_eq!(profit.to_string(), "123.45");§Error model
Every checked arithmetic operation on decimal value types returns
Result<T, Error>.
Arithmetic and validation failures are contextual and always carry the
parameter type through ParamKind.
Error::Negativeincludes the failingParamKind.Error::DivisionByZeroincludes the failingParamKind.Error::Overflowincludes the failingParamKind.Error::Underflowincludes the failingParamKind.Error::InvalidLeveragereports invalid fixed-point leverage values.
There are no panicking arithmetic operators in this module.
All arithmetic is exposed through checked_* methods.
§Arithmetic operations
Numeric types provide checked operations:
checked_add(other)— additionchecked_sub(other)— subtractionchecked_mul_i64(scalar)— multiplication byi64checked_mul_u64(scalar)— multiplication byu64checked_mul_f64(scalar)— multiplication byf64checked_div_i64(divisor)— division byi64checked_div_u64(divisor)— division byu64checked_div_f64(divisor)— division byf64checked_rem_i64(divisor)— remainder byi64checked_rem_u64(divisor)— remainder byu64checked_rem_f64(divisor)— remainder byf64
§Examples
use openpit::param::{Error, ParamKind, Price, Quantity};
let price = Price::from_str("100")?;
let qty = Quantity::from_str("10")?;
let volume = price.calculate_volume(qty)?;
assert_eq!(volume.to_string(), "1000");
let err = Quantity::from_str("-1").expect_err("negative quantity must be rejected");
assert_eq!(err, Error::Negative { param: ParamKind::Quantity });Re-exports§
pub use account_group_id::AccountGroupId;pub use account_group_id::AccountGroupIdError;pub use account_group_id::DEFAULT_ACCOUNT_GROUP;pub use account_id::AccountId;pub use account_id::AccountIdError;pub use adjustment_amount::AdjustmentAmount;pub use asset::Asset;pub use asset::AssetError;pub use cash_flow::CashFlow;pub use fee::Fee;pub use fill_type::FillType;pub use leverage::Leverage;pub use notional::Notional;pub use pnl::Pnl;pub use position_effect::PositionEffect;pub use position_mode::PositionMode;pub use position_side::PositionSide;pub use position_size::PositionSize;pub use price::Price;pub use quantity::Quantity;pub use side::Side;pub use trade::Trade;pub use trade_amount::TradeAmount;pub use volume::Volume;
Modules§
- account_
group_ id - account_
id - adjustment_
amount - asset
- cash_
flow - fee
- fill_
type - leverage
- notional
- pnl
- position_
effect - position_
mode - position_
side - position_
size - price
- quantity
- side
- trade
- trade_
amount - volume
Enums§
- Error
- Errors for parameter value validation and arithmetic.
- Error
Code - Stable error code for parameter validation and arithmetic failures.
- Param
Kind - Identifies a parameter type that caused a validation or arithmetic error.
- Rounding
Strategy - Rounding strategy for decimal values.