Skip to main content

Price

Struct Price 

Source
pub struct Price(/* private fields */);
Expand description

A strictly positive price value backed by Decimal.

§Example

use fin_primitives::types::Price;
use rust_decimal_macros::dec;
let p = Price::new(dec!(100.50)).unwrap();
assert_eq!(p.value(), dec!(100.50));

Implementations§

Source§

impl Price

Source

pub fn new(d: Decimal) -> Result<Self, FinError>

Construct a validated Price.

§Errors

Returns FinError::InvalidPrice if d <= 0.

Source

pub fn value(&self) -> Decimal

Returns the inner Decimal value.

Source

pub fn to_f64(&self) -> f64

Converts to f64 with possible precision loss.

Source

pub fn from_f64(f: f64) -> Option<Self>

Constructs a Price from an f64. Returns None if f is not finite or <= 0.

Source

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

Source

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

pub fn mid(self, other: Price) -> Price

Returns the midpoint between self and other: (self + other) / 2.

Source§

impl Price

Source

pub fn abs_diff(self, other: Price) -> Decimal

Returns the absolute difference between self and other: |self - other|.

Source

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

pub fn clamp(self, lo: Price, hi: Price) -> Price

Clamps this price to the inclusive range [lo, hi].

Returns lo if self < lo, hi if self > hi, otherwise self.

Source§

impl Price

Source

pub fn round_to(self, dp: u32) -> Option<Price>

Rounds this price to dp decimal places using banker’s rounding.

Returns None if the rounded value is zero or negative (invalid price).

Source

pub fn round_half_up(self, dp: u32) -> Option<Price>

Rounds this price to dp decimal places using round-half-up (conventional rounding).

Unlike round_to which uses banker’s rounding, this always rounds 0.5 away from zero. Returns None if the rounded result is zero or negative.

Source§

impl Price

Source

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

Source

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

Source

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.

Source

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

Source

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

Source

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.

Source

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.

Source

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

Source§

type Output = Decimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Price) -> Decimal

Performs the + operation. Read more
Source§

impl Clone for Price

Source§

fn clone(&self) -> Price

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Price

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Price

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Price

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Mul<Decimal> for Price

Price * Decimal scales a price; returns None if the result is not a valid Price.

Source§

type Output = Option<Price>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Decimal) -> Option<Price>

Performs the * operation. Read more
Source§

impl Mul<Quantity> for Price

Price * Quantity yields the notional value as Decimal.

Source§

type Output = Decimal

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Quantity) -> Decimal

Performs the * operation. Read more
Source§

impl Ord for Price

Source§

fn cmp(&self, other: &Price) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Price

Source§

fn eq(&self, other: &Price) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Price

Source§

fn partial_cmp(&self, other: &Price) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Price

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Sub for Price

Price - Price yields a raw Decimal (difference may be zero or negative).

Source§

type Output = Decimal

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Price) -> Decimal

Performs the - operation. Read more
Source§

impl Copy for Price

Source§

impl Eq for Price

Source§

impl StructuralPartialEq for Price

Auto Trait Implementations§

§

impl Freeze for Price

§

impl RefUnwindSafe for Price

§

impl Send for Price

§

impl Sync for Price

§

impl Unpin for Price

§

impl UnsafeUnpin for Price

§

impl UnwindSafe for Price

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,