Skip to main content

BidAskSpread

Struct BidAskSpread 

Source
pub struct BidAskSpread { /* private fields */ }
Expand description

Rolling average bid-ask spread tracker, expressed in basis points.

Feed bid/ask prices via update. Once window samples have been seen, average_spread_bps returns the rolling average.

Basis points = (ask - bid) / mid * 10_000.

§Example

use fin_primitives::microstructure::BidAskSpread;
use rust_decimal_macros::dec;

let mut tracker = BidAskSpread::new(5).unwrap();
for _ in 0..5 {
    tracker.update(dec!(99.90), dec!(100.10)).unwrap();
}
let spread_bps = tracker.average_spread_bps().unwrap();
// spread = 0.20, mid = 100.0 → 20 bps
assert!((spread_bps - 20.0).abs() < 0.01);

Implementations§

Source§

impl BidAskSpread

Source

pub fn new(window: usize) -> Result<Self, FinError>

Constructs a BidAskSpread tracker.

§Errors

Returns FinError::InvalidPeriod if window == 0.

Source

pub fn update(&mut self, bid: Decimal, ask: Decimal) -> Result<(), FinError>

Records a bid/ask quote.

§Errors

Returns FinError::InvalidInput if bid >= ask or bid <= 0.

Source

pub fn average_spread_bps(&self) -> Option<f64>

Returns the rolling average spread in basis points, or None if not yet ready.

Source

pub fn is_ready(&self) -> bool

Returns true when the window is full.

Source

pub fn window(&self) -> usize

Returns the configured window size.

Source

pub fn sample_count(&self) -> usize

Returns the number of samples buffered.

Source

pub fn reset(&mut self)

Resets the tracker.

Trait Implementations§

Source§

impl Debug for BidAskSpread

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.