pub struct KylesLambda { /* private fields */ }Expand description
Kyle’s Lambda — the rolling ordinary-least-squares slope of mid-price changes on signed trade volume, the canonical measure of market depth / price impact.
Each update receives a TradeQuote — a trade plus the mid prevailing at
execution. Internally the indicator forms, per trade, the mid change since
the previous trade (Δmid = midₜ − midₜ₋₁) and the signed volume
(q = size · D, with D the aggressor sign), then runs a rolling OLS
regression of Δmid on q over the trailing window of window trades:
cov = (1/n) · Σ q·Δmid − q̄·Δ̄mid
var = (1/n) · Σ q² − q̄²
λ = cov / varλ is the estimated price move per unit of signed volume: a deep, liquid
book absorbs flow with little movement and reads a small λ; a thin book
moves sharply per unit traded and reads a large λ. It is a direct,
model-light proxy for the slope of the demand curve in Kyle’s microstructure
model.
Each update is O(1): four running sums (Σq, ΣΔmid, Σq², Σq·Δmid)
are maintained as the window slides. A window of constant signed volume has
zero variance and λ is undefined; the indicator returns 0 in that case
rather than producing NaN.
Input = TradeQuote, Output = f64. It warms up for window + 1
trade-quotes: one to seed the previous mid, then window paired
observations.
§Example
use wickra_core::{Indicator, KylesLambda, Side, Trade, TradeQuote};
// A book where each trade moves the mid by exactly 0.5 per unit of signed
// volume gives λ = 0.5.
let mut lambda = KylesLambda::new(8).unwrap();
let mut mid = 100.0;
let mut last = None;
for i in 0..20 {
let side = if i % 2 == 0 { Side::Buy } else { Side::Sell };
let size = 1.0 + f64::from(i % 3);
let signed = size * side.sign();
mid += 0.5 * signed;
let trade = Trade::new(mid, size, side, 0).unwrap();
last = lambda.update(TradeQuote::new(trade, mid).unwrap());
}
assert!((last.unwrap() - 0.5).abs() < 1e-9);Implementations§
Source§impl KylesLambda
impl KylesLambda
Trait Implementations§
Source§impl Clone for KylesLambda
impl Clone for KylesLambda
Source§fn clone(&self) -> KylesLambda
fn clone(&self) -> KylesLambda
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 KylesLambda
impl Debug for KylesLambda
Source§impl Indicator for KylesLambda
impl Indicator for KylesLambda
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 KylesLambda
impl RefUnwindSafe for KylesLambda
impl Send for KylesLambda
impl Sync for KylesLambda
impl Unpin for KylesLambda
impl UnsafeUnpin for KylesLambda
impl UnwindSafe for KylesLambda
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