pub struct RealizedSpread { /* private fields */ }Expand description
Realized Spread — twice the signed deviation of a trade price from the mid
that prevails horizon trades later, expressed in basis points of the
trade’s contemporaneous mid.
realizedSpread = 2 · D · (tradePrice − mid_{t+horizon}) / mid_t · 10_000 (bps)where D is the aggressor sign (+1 for a buy, −1 for a sell), mid_t
is the mid at the time of the trade, and mid_{t+horizon} is the mid
horizon trade-quotes later. Where the effective spread measures the full
cost paid by the aggressor against the contemporaneous mid, the realized
spread measures the share of that cost a liquidity provider keeps after
the mid has moved: it is the effective spread net of the price impact
(effective = realized + 2 · priceImpact). A high realized spread means
the quote was not picked off; a low or negative one is the signature of
adverse selection, the trade preceding a move in its own direction.
The indicator buffers each incoming trade-quote and emits the realized
spread for the trade made horizon updates ago, once that future mid is
known. It warms up for horizon + 1 trade-quotes — update returns None
until the first trade can be resolved — and then emits one value per update
in O(1).
Input = TradeQuote, Output = f64.
§Example
use wickra_core::{Indicator, RealizedSpread, Side, Trade, TradeQuote};
let mut rs = RealizedSpread::new(1).unwrap();
let tq = |price: f64, side, mid| TradeQuote::new(Trade::new(price, 1.0, side, 0).unwrap(), mid).unwrap();
// First trade buffered; nothing to resolve yet.
assert_eq!(rs.update(tq(100.10, Side::Buy, 100.0)), None);
// One trade later the mid is 100.20, resolving the first buy:
// 2 · (+1) · (100.10 − 100.20) / 100.0 · 10_000 = −20 bps (adverse selection).
let out = rs.update(tq(99.90, Side::Sell, 100.20)).unwrap();
assert!((out - (-20.0)).abs() < 1e-9);Implementations§
Source§impl RealizedSpread
impl RealizedSpread
Trait Implementations§
Source§impl Clone for RealizedSpread
impl Clone for RealizedSpread
Source§fn clone(&self) -> RealizedSpread
fn clone(&self) -> RealizedSpread
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for RealizedSpread
impl Debug for RealizedSpread
Source§impl Indicator for RealizedSpread
impl Indicator for RealizedSpread
Source§type Input = TradeQuote
type Input = TradeQuote
f64 for a price, or Candle / Tick).Source§fn update(&mut self, quote: TradeQuote) -> Option<f64>
fn update(&mut self, quote: TradeQuote) -> Option<f64>
None if the indicator is still warming up.Source§fn reset(&mut self)
fn reset(&mut self)
Source§fn warmup_period(&self) -> usize
fn warmup_period(&self) -> usize
None output can be produced.Auto Trait Implementations§
impl Freeze for RealizedSpread
impl RefUnwindSafe for RealizedSpread
impl Send for RealizedSpread
impl Sync for RealizedSpread
impl Unpin for RealizedSpread
impl UnsafeUnpin for RealizedSpread
impl UnwindSafe for RealizedSpread
Blanket Implementations§
Source§impl<T> BatchExt for Twhere
T: Indicator,
impl<T> BatchExt for Twhere
T: Indicator,
Source§fn batch(&mut self, inputs: &[Self::Input]) -> Vec<Option<Self::Output>>
fn batch(&mut self, inputs: &[Self::Input]) -> Vec<Option<Self::Output>>
None during warmup) per input.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more