pub struct KRatio { /* private fields */ }Expand description
K-Ratio over a trailing window of period returns.
Lars Kestner’s K-Ratio measures the consistency of an equity curve, not just its return. It builds the cumulative-return curve over the window, fits an ordinary-least-squares trend line through it against time, and divides the fitted slope by the standard error of that slope:
equity_t = Σ_{i<=t} return_i (cumulative curve, t = 1..period)
slope, intercept = OLS(equity_t ~ t)
SE(slope) = sqrt( (Σ residual² / (period − 2)) / Σ(t − t̄)² )
K-Ratio = slope / SE(slope)A high K-Ratio means the equity curve climbs steadily — a steep slope with
little scatter around the trend. A strategy that earns the same total return in
a few lucky jumps scores lower because its residual scatter inflates the
standard error. This is the original 1996 form; later Kestner revisions scale by
the number of periods (slope / (SE · period) in 2003, slope / (SE · √period)
in 2013) — apply that scaling downstream if you need to compare across window
lengths.
A perfectly straight window (e.g. constant returns) has zero residual scatter,
so the slope’s standard error is zero and the K-Ratio is undefined; the
indicator reports 0.0 in that degenerate case. The statistic therefore needs
some dispersion in the returns to be meaningful.
The first value lands after period returns; each update re-fits the line
over the window (O(period)), which is O(1) in the length of the overall series.
§Example
use wickra_core::{Indicator, KRatio};
let mut indicator = KRatio::new(30).unwrap();
let mut last = None;
for i in 0..60 {
last = indicator.update(0.001 + (f64::from(i) * 0.3).sin() * 0.01);
}
assert!(last.is_some());Implementations§
Trait Implementations§
Source§impl Indicator for KRatio
impl Indicator for KRatio
Source§fn update(&mut self, ret: f64) -> Option<f64>
fn update(&mut self, ret: f64) -> 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 KRatio
impl RefUnwindSafe for KRatio
impl Send for KRatio
impl Sync for KRatio
impl Unpin for KRatio
impl UnsafeUnpin for KRatio
impl UnwindSafe for KRatio
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> BatchNanExt for T
impl<T> BatchNanExt for T
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