Method

Trait Method 

Source
pub trait Method {
    type Params;
    type Input: ?Sized;
    type Output;

    // Required methods
    fn new(
        parameters: Self::Params,
        initial_value: &Self::Input,
    ) -> Result<Self, Error>
       where Self: Sized;
    fn next(&mut self, value: &Self::Input) -> Self::Output;

    // Provided methods
    fn with_history(
        parameters: Self::Params,
        initial_value: &Self::Input,
    ) -> Result<WithHistory<Self, Self::Output>, Error>
       where Self: Sized,
             Self::Output: Debug + Clone { ... }
    fn with_last_value(
        parameters: Self::Params,
        initial_value: &Self::Input,
    ) -> Result<WithLastValue<Self, Self::Output>, Error>
       where Self: Sized,
             Self::Output: Debug + Clone { ... }
    fn name(&self) -> &str { ... }
    fn memsize(&self) -> (usize, usize)
       where Self: Sized { ... }
    fn over<S>(&mut self, inputs: S) -> Vec<Self::Output>
       where S: Sequence<Self::Input>,
             Self::Input: Sized,
             Self: Sized { ... }
    fn apply<T, S>(&mut self, sequence: &mut S)
       where S: Sequence<T> + AsMut<[T]> + ?Sized,
             Self: Method<Input = T, Output = T> + Sized { ... }
    fn new_over<S>(
        parameters: Self::Params,
        inputs: S,
    ) -> Result<Vec<Self::Output>, Error>
       where S: Sequence<Self::Input>,
             Self::Input: Sized,
             Self: Sized { ... }
    fn new_apply<T, S>(
        parameters: Self::Params,
        sequence: &mut S,
    ) -> Result<(), Error>
       where S: Sequence<T> + AsMut<[T]>,
             Self: Method<Input = T, Output = T> + Sized { ... }
    fn into_fn<'a>(
        self,
    ) -> Box<dyn FnMut(&'a <Self as Method>::Input) -> <Self as Method>::Output>
       where Self: Sized + 'static,
             Self::Input: 'static { ... }
    fn new_fn(
        params: Self::Params,
        initial_value: &Self::Input,
    ) -> Result<Box<dyn FnMut(&'_ <Self as Method>::Input) -> <Self as Method>::Output>, Error>
       where Self: Sized + 'static { ... }
}
Expand description

Trait for creating methods for timeseries

§Regular methods usage

§Iterate over vector’s values

use yata::methods::SMA;
use yata::prelude::*;

let s: Vec<_> = vec![1.,2.,3.,4.,5.,6.,7.,8.,9.,10.];
let mut ma = SMA::new(2, &s[0]).unwrap();

s.iter().enumerate().for_each(|(index, value)| {
    assert_eq!(ma.next(value), (*value + s[index.saturating_sub(1)])/2.);
});

§Get a whole new vector over the input vector

You can call method over any Sequence:

use yata::methods::SMA;
use yata::prelude::*;

let s: Vec<_> = vec![1.,2.,3.,4.,5.,6.,7.,8.,9.,10.];
let mut ma = SMA::new(2, &s[0]).unwrap();

let result = ma.over(s);
assert_eq!(result.as_slice(), &[1., 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5]);

Or you can provide Method to Sequence:

use yata::methods::SMA;
use yata::prelude::*;

let s: Vec<_> = vec![1.,2.,3.,4.,5.,6.,7.,8.,9.,10.];
let mut ma = SMA::new(2, &s[0]).unwrap();

let result = s.call(&mut ma);
assert_eq!(result.as_slice(), &[1., 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5]);

Or you can even change Sequence values in-place:

use yata::methods::SMA;
use yata::prelude::*;

let mut s: Vec<_> = vec![1.,2.,3.,4.,5.,6.,7.,8.,9.,10.];
let mut ma = SMA::new(2, &s[0]).unwrap();

s.apply(&mut ma);
assert_eq!(s.as_slice(), &[1., 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5]);

§Be advised

There is no reset method on the trait. If you need reset a state of the Method instance, you should just create a new one.

Required Associated Types§

Source

type Params

Method parameters

Source

type Input: ?Sized

Input value type

Source

type Output

Output value type

Required Methods§

Source

fn new( parameters: Self::Params, initial_value: &Self::Input, ) -> Result<Self, Error>
where Self: Sized,

Static method for creating an instance of the method with given parameters and initial value (simply first input value)

Source

fn next(&mut self, value: &Self::Input) -> Self::Output

Generates next output value based on the given input value

Provided Methods§

Source

fn with_history( parameters: Self::Params, initial_value: &Self::Input, ) -> Result<WithHistory<Self, Self::Output>, Error>
where Self: Sized, Self::Output: Debug + Clone,

Creates an instance of the method with given parameters and initial value, wrapped by historical data holder

Source

fn with_last_value( parameters: Self::Params, initial_value: &Self::Input, ) -> Result<WithLastValue<Self, Self::Output>, Error>
where Self: Sized, Self::Output: Debug + Clone,

Creates an instance of the method with given parameters and initial value, wrapped by last produced value holder

Source

fn name(&self) -> &str

Returns a name of the method

Source

fn memsize(&self) -> (usize, usize)
where Self: Sized,

👎Deprecated

Returns memory size of the method (size, align)

Source

fn over<S>(&mut self, inputs: S) -> Vec<Self::Output>
where S: Sequence<Self::Input>, Self::Input: Sized, Self: Sized,

Iterates the Method over the given inputs slice and returns Vec of output values.

§Guarantees

The length of an output Vec is always equal to the length of an inputs slice.

use yata::methods::SMA;
use yata::prelude::*;

let s: Vec<_> = vec![1.,2.,3.,4.,5.,6.,7.,8.,9.,10.];
let mut ma = SMA::new(5, &s[0]).unwrap();

let result = ma.over(&s);
assert_eq!(result.len(), s.len());
use yata::methods::SMA;
use yata::prelude::*;

let s: Vec<_> = vec![1.,2.,3.,4.,5.,6.,7.,8.,9.,10.];
let mut ma = SMA::new(100, &s[0]).unwrap();

let result = ma.over(&s);
assert_eq!(result.len(), s.len());
Source

fn apply<T, S>(&mut self, sequence: &mut S)
where S: Sequence<T> + AsMut<[T]> + ?Sized, Self: Method<Input = T, Output = T> + Sized,

Applies method to the sequence in-place.

Source

fn new_over<S>( parameters: Self::Params, inputs: S, ) -> Result<Vec<Self::Output>, Error>
where S: Sequence<Self::Input>, Self::Input: Sized, Self: Sized,

Creates new Method instance and iterates it over the given inputs slice and returns Vec of output values.

§Guarantees

The length of an output Vec is always equal to the length of an inputs slice.

Source

fn new_apply<T, S>( parameters: Self::Params, sequence: &mut S, ) -> Result<(), Error>
where S: Sequence<T> + AsMut<[T]>, Self: Method<Input = T, Output = T> + Sized,

Creates new Method instance and applies it to the sequence.

Source

fn into_fn<'a>( self, ) -> Box<dyn FnMut(&'a <Self as Method>::Input) -> <Self as Method>::Output>
where Self: Sized + 'static, Self::Input: 'static,

Creates a function from the Method instance

Source

fn new_fn( params: Self::Params, initial_value: &Self::Input, ) -> Result<Box<dyn FnMut(&'_ <Self as Method>::Input) -> <Self as Method>::Output>, Error>
where Self: Sized + 'static,

Creates new function based on the method

Implementors§

Source§

impl Method for MAInstance

Source§

impl Method for ADI

Source§

impl Method for CCI

Source§

impl Method for Conv

Source§

impl Method for Cross

Source§

impl Method for CrossAbove

Source§

impl Method for CrossUnder

Source§

impl Method for DEMA

Source§

impl Method for DMA

Source§

impl Method for Derivative

Source§

impl Method for EMA

Source§

impl Method for HMA

Source§

impl Method for HeikinAshi

Source§

impl Method for Highest

Source§

impl Method for HighestIndex

Source§

impl Method for HighestLowestDelta

Source§

impl Method for Integral

Source§

impl Method for LinReg

Source§

impl Method for LinearVolatility

Source§

impl Method for LowerReversalSignal

Source§

impl Method for Lowest

Source§

impl Method for LowestIndex

Source§

impl Method for MeanAbsDev

Source§

impl Method for MedianAbsDev

Source§

impl Method for Momentum

Source§

impl Method for RMA

Source§

impl Method for RateOfChange

Source§

impl Method for Renko

Source§

impl Method for ReversalSignal

Source§

impl Method for SMA

Source§

impl Method for SMM

Source§

impl Method for SWMA

Source§

impl Method for StDev

Source§

impl Method for TEMA

Source§

impl Method for TMA

Source§

impl Method for TR

Source§

impl Method for TRIMA

Source§

impl Method for TSI

Source§

impl Method for UpperReversalSignal

Source§

impl Method for VWMA

Source§

impl Method for Vidya

Source§

impl Method for WMA

Source§

impl Method for WSMA

Source§

impl<T> Method for WithHistory<T, T::Output>
where T: Method, T::Output: Debug + Clone,

Source§

type Params = <T as Method>::Params

Source§

type Input = <T as Method>::Input

Source§

type Output = <T as Method>::Output

Source§

impl<T> Method for WithLastValue<T, T::Output>
where T: Method, T::Output: Debug + Clone,

Source§

type Params = <T as Method>::Params

Source§

type Input = <T as Method>::Input

Source§

type Output = <T as Method>::Output

Source§

impl<T> Method for CollapseTimeframe<T>
where T: OHLCV + Clone + Add<Output = T>,

Source§

impl<T> Method for Past<T>
where T: Clone + Debug,