Skip to main content

Stepper

Struct Stepper 

Source
pub struct Stepper<S> { /* private fields */ }
Expand description

Runs a causal study one step per bar and keeps the state before the last bar, so a last-bar update or an append resumes instead of recomputing the whole series. calculate is run(data, 0, ..), so batch and streaming share one code path.

A custom study with a three-bar mean of squared closes, available without the studies feature:

use egui_charts::model::Bar;
use egui_charts::studies::{helpers, Indicator, IndicatorValue, IndicatorValues, Stepper};
use std::collections::VecDeque;

#[derive(Clone, Default)]
struct MeanSquare {
    driver: Stepper<VecDeque<f64>>,
    values: IndicatorValues,
}
impl Indicator for MeanSquare {
    fn name(&self) -> &str { "Mean square" }
    fn calculate(&mut self, bars: &[Bar]) { self.update(bars, 0); }
    fn update(&mut self, bars: &[Bar], from: usize) {
        self.driver.run(bars, from, &mut self.values, |window, bar| {
            window.push_back(bar.close * bar.close);
            if window.len() > 3 { window.pop_front(); }
            let mean = helpers::sma(window.make_contiguous(), 3);
            IndicatorValue::Single(*mean.last().unwrap())
        });
    }
    fn is_incremental(&self) -> bool { true }
    fn values(&self) -> &IndicatorValues { &self.values }
    fn colors(&self) -> Vec<ecolor::Color32> { vec![ecolor::Color32::YELLOW] }
    fn set_colors(&mut self, _: Vec<ecolor::Color32>) {}
    fn set_visible(&mut self, _: bool) {}
    fn clone_box(&self) -> Box<dyn Indicator> { Box::new(self.clone()) }
}
let mut bars: Vec<_> = [1.0, 2.0, 4.0].into_iter().enumerate().map(|(i, p)| {
    Bar::new(chrono::DateTime::from_timestamp(i as i64, 0).unwrap(), p, p, p, p, 1.0)
}).collect();
let mut study = MeanSquare::default();
study.calculate(&bars);
assert_eq!(study.values().value(0, 2), Some(7.0)); // (1 + 4 + 16) / 3
bars[2].close = 5.0;
study.update(&bars, 2);
let mut fresh = MeanSquare::default();
fresh.calculate(&bars);
assert_eq!(format!("{:?}", study.values()), format!("{:?}", fresh.values()));
bars.push(Bar::new(chrono::DateTime::from_timestamp(3, 0).unwrap(), 6.0, 6.0, 6.0, 6.0, 1.0));
study.update(&bars, 3);
fresh.calculate(&bars);
assert_eq!(format!("{:?}", study.values()), format!("{:?}", fresh.values()));

Implementations§

Source§

impl<S: Clone + Default> Stepper<S>

Source

pub fn state(&self) -> &S

The state after the previous run’s last bar.

Source

pub fn run( &mut self, data: &[Bar], from: usize, out: &mut IndicatorValues, step: impl FnMut(&mut S, &Bar) -> IndicatorValue, )

Recomputes out[from..], given that data[..from] is unchanged since the previous run. Resumes when from >= len - 1 (last bar changed or bars appended), restarts from bar 0 otherwise (correction deep in history, prepended page, removed bars).

Trait Implementations§

Source§

impl<S: Clone> Clone for Stepper<S>

Source§

fn clone(&self) -> Stepper<S>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S: Default> Default for Stepper<S>

Source§

fn default() -> Stepper<S>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<S> Freeze for Stepper<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for Stepper<S>
where S: RefUnwindSafe,

§

impl<S> Send for Stepper<S>
where S: Send,

§

impl<S> Sync for Stepper<S>
where S: Sync,

§

impl<S> Unpin for Stepper<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for Stepper<S>
where S: UnsafeUnpin,

§

impl<S> UnwindSafe for Stepper<S>
where S: UnwindSafe,

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> MaybeSend for T
where T: Send + Sync + ?Sized,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

§

impl<T> SerializableAny for T
where T: 'static + Any + Clone + for<'a> Send + Sync,

§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

§

fn simd_from(_simd: S, value: T) -> T

§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

§

fn simd_into(self, simd: S) -> T

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.