pub struct SmaState { /* private fields */ }Expand description
Incremental SMA. After warm-up, each SmaState::push is O(1).
Batch sma is implemented as SmaState::new + SmaState::push_bars.
§Examples
use finance_solution::stocks::ta::SmaState;
let mut s = SmaState::new(3).unwrap();
assert_eq!(s.push(1.0).unwrap(), None);
assert_eq!(s.push(2.0).unwrap(), None);
assert!((s.push(3.0).unwrap().unwrap() - 2.0).abs() < 1e-12);
assert!((s.push(6.0).unwrap().unwrap() - 3.666666666666).abs() < 1e-9);Implementations§
Source§impl SmaState
impl SmaState
Sourcepub fn new(period: usize) -> FinanceResult<Self>
pub fn new(period: usize) -> FinanceResult<Self>
Fallible constructor (period >= 1). Named new → FinanceResult (not try_new).
pub fn from_history(period: usize, closes: &[f64]) -> FinanceResult<Self>
pub fn period(&self) -> usize
pub fn reset(&mut self)
Sourcepub fn push(&mut self, close: f64) -> FinanceResult<Option<f64>>
pub fn push(&mut self, close: f64) -> FinanceResult<Option<f64>>
Push one close. None until period samples seen.
Sourcepub fn push_bars(&mut self, closes: &[f64]) -> FinanceResult<Vec<Option<f64>>>
pub fn push_bars(&mut self, closes: &[f64]) -> FinanceResult<Vec<Option<f64>>>
Push many closes (one streaming payload). One output per input.
pub fn last(&self) -> Option<f64>
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SmaState
impl RefUnwindSafe for SmaState
impl Send for SmaState
impl Sync for SmaState
impl Unpin for SmaState
impl UnsafeUnpin for SmaState
impl UnwindSafe for SmaState
Blanket Implementations§
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
Mutably borrows from an owned value. Read more
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> ⓘ
Converts
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> ⓘ
Converts
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