pub struct Price(/* private fields */);Expand description
Implementations§
Source§impl Price
impl Price
Sourcepub fn from_f64(f: f64) -> Option<Self>
pub fn from_f64(f: f64) -> Option<Self>
Constructs a Price from an f64. Returns None if f is not finite or <= 0.
Sourcepub fn to_string_with_dp(&self, dp: u32) -> String
pub fn to_string_with_dp(&self, dp: u32) -> String
Returns a String representation rounded to dp decimal places.
Useful for display/logging without losing the underlying decimal precision.
Source§impl Price
impl Price
Sourcepub fn pct_change_to(self, other: Price) -> Decimal
pub fn pct_change_to(self, other: Price) -> Decimal
Returns the percentage change from self to other: (other - self) / self * 100.
Positive values indicate a price increase; negative values indicate a decrease.
Source§impl Price
impl Price
Sourcepub fn abs_diff(self, other: Price) -> Decimal
pub fn abs_diff(self, other: Price) -> Decimal
Returns the absolute difference between self and other: |self - other|.
Sourcepub fn snap_to_tick(self, tick_size: Decimal) -> Option<Price>
pub fn snap_to_tick(self, tick_size: Decimal) -> Option<Price>
Rounds this price to the nearest multiple of tick_size.
Returns None if tick_size <= 0 or if the rounded value is not a valid
Price (i.e. the result is zero or negative).
§Example
use fin_primitives::types::Price;
use rust_decimal_macros::dec;
let p = Price::new(dec!(10.3)).unwrap();
let snapped = p.snap_to_tick(dec!(0.5)).unwrap();
assert_eq!(snapped.value(), dec!(10.5));Source§impl Price
impl Price
Source§impl Price
impl Price
Sourcepub fn checked_add(self, other: Price) -> Option<Price>
pub fn checked_add(self, other: Price) -> Option<Price>
Adds other to self, returning the result as a Price, or None on overflow.
Useful when combining two price levels and needing a validated result.
Source§impl Price
impl Price
Sourcepub fn checked_mul(self, qty: Quantity) -> Option<Decimal>
pub fn checked_mul(self, qty: Quantity) -> Option<Decimal>
Multiplies this price by qty, returning None if the result overflows.
Prefer this over the * operator when overflow is a concern (e.g., large
notional values with many decimal digits).
Source§impl Price
impl Price
Sourcepub fn midpoint(bid: Price, ask: Price) -> Decimal
pub fn midpoint(bid: Price, ask: Price) -> Decimal
Returns the midpoint between bid and ask: (bid + ask) / 2.
Useful for computing the theoretical fair value between two prices.
Sourcepub fn pct_move(self, pct: Decimal) -> Option<Price>
pub fn pct_move(self, pct: Decimal) -> Option<Price>
Applies a percentage move to this price: self * (1 + pct / 100).
Returns None if the result is not a valid price (e.g., a large negative pct
would drive the price to zero or below).
Sourcepub fn lerp(self, other: Price, t: Decimal) -> Option<Price>
pub fn lerp(self, other: Price, t: Decimal) -> Option<Price>
Linearly interpolates between self and other by factor t in [0, 1].
Returns self + (other - self) * t. Returns None if t is outside [0, 1]
or if the result is not a valid price (i.e., not strictly positive).
Sourcepub fn is_within_pct(self, other: Price, pct: Decimal) -> bool
pub fn is_within_pct(self, other: Price, pct: Decimal) -> bool
Returns true if other is within pct percent of self.
Computes |self - other| / self * 100 <= pct.
Returns false if pct is negative.
Sourcepub fn distance_pct(self, other: Price) -> Decimal
pub fn distance_pct(self, other: Price) -> Decimal
Signed percentage distance from self to other: (other - self) / self * 100.
Positive when other > self, negative when other < self.
Sourcepub fn round_to_tick(self, tick_size: Decimal) -> Option<Price>
pub fn round_to_tick(self, tick_size: Decimal) -> Option<Price>
Rounds this price to the nearest multiple of tick_size using standard rounding.
Equivalent to snap_to_tick but named for readability at call sites that
want explicit rounding (e.g. order routing) vs. snapping (e.g. display).
Returns None if tick_size <= 0 or the result is zero/negative.
Trait Implementations§
Source§impl Add for Price
Price + Price yields a raw Decimal (sum is not necessarily a valid price in all contexts).
impl Add for Price
Price + Price yields a raw Decimal (sum is not necessarily a valid price in all contexts).
Source§impl<'de> Deserialize<'de> for Price
impl<'de> Deserialize<'de> for Price
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Mul<Decimal> for Price
Price * Decimal scales a price; returns None if the result is not a valid Price.
impl Mul<Decimal> for Price
Price * Decimal scales a price; returns None if the result is not a valid Price.