Skip to main content

Module param

Module param 

Source
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

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

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) — addition
  • checked_sub(other) — subtraction
  • checked_mul_i64(scalar) — multiplication by i64
  • checked_mul_u64(scalar) — multiplication by u64
  • checked_mul_f64(scalar) — multiplication by f64
  • checked_div_i64(divisor) — division by i64
  • checked_div_u64(divisor) — division by u64
  • checked_div_f64(divisor) — division by f64
  • checked_rem_i64(divisor) — remainder by i64
  • checked_rem_u64(divisor) — remainder by u64
  • checked_rem_f64(divisor) — remainder by f64

§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.
ErrorCode
Stable error code for parameter validation and arithmetic failures.
ParamKind
Identifies a parameter type that caused a validation or arithmetic error.
RoundingStrategy
Rounding strategy for decimal values.