Trait yata::core::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

§

type Params = u8

§

type Input = dyn OHLCV

§

type Output = f64

source§

impl Method for CCI

§

type Params = u8

§

type Input = f64

§

type Output = <CCI as Method>::Input

source§

impl Method for Conv

§

type Params = Vec<f64>

§

type Input = f64

§

type Output = <Conv as Method>::Input

source§

impl Method for Cross

§

type Params = ()

§

type Input = (f64, f64)

§

type Output = Action

source§

impl Method for CrossAbove

§

type Params = ()

§

type Input = (f64, f64)

§

type Output = Action

source§

impl Method for CrossUnder

§

type Params = ()

§

type Input = (f64, f64)

§

type Output = Action

source§

impl Method for DEMA

§

type Params = u8

§

type Input = f64

§

type Output = <DEMA as Method>::Input

source§

impl Method for DMA

§

type Params = u8

§

type Input = f64

§

type Output = <DMA as Method>::Input

source§

impl Method for Derivative

source§

impl Method for EMA

§

type Params = u8

§

type Input = f64

§

type Output = <EMA as Method>::Input

source§

impl Method for HMA

§

type Params = u8

§

type Input = f64

§

type Output = <HMA as Method>::Input

source§

impl Method for HeikinAshi

§

type Params = ()

§

type Input = dyn OHLCV

§

type Output = Candle

source§

impl Method for Highest

§

type Params = u8

§

type Input = f64

§

type Output = <Highest as Method>::Input

source§

impl Method for HighestIndex

§

type Params = u8

§

type Input = f64

§

type Output = u8

source§

impl Method for HighestLowestDelta

source§

impl Method for Integral

§

type Params = u8

§

type Input = f64

§

type Output = <Integral as Method>::Input

source§

impl Method for LinReg

§

type Params = u8

§

type Input = f64

§

type Output = <LinReg as Method>::Input

source§

impl Method for LinearVolatility

source§

impl Method for LowerReversalSignal

§

type Params = (u8, u8)

§

type Input = f64

§

type Output = Action

source§

impl Method for Lowest

§

type Params = u8

§

type Input = f64

§

type Output = <Lowest as Method>::Input

source§

impl Method for LowestIndex

§

type Params = u8

§

type Input = f64

§

type Output = u8

source§

impl Method for MeanAbsDev

source§

impl Method for MedianAbsDev

source§

impl Method for Momentum

§

type Params = u8

§

type Input = f64

§

type Output = <Momentum as Method>::Input

source§

impl Method for RMA

§

type Params = u8

§

type Input = f64

§

type Output = <RMA as Method>::Input

source§

impl Method for RateOfChange

source§

impl Method for Renko

§

type Params = (f64, Source)

§

type Input = dyn OHLCV

§

type Output = RenkoOutput

source§

impl Method for ReversalSignal

§

type Params = (u8, u8)

§

type Input = f64

§

type Output = Action

source§

impl Method for SMA

§

type Params = u8

§

type Input = f64

§

type Output = <SMA as Method>::Input

source§

impl Method for SMM

§

type Params = u8

§

type Input = f64

§

type Output = <SMM as Method>::Input

source§

impl Method for SWMA

§

type Params = u8

§

type Input = f64

§

type Output = <SWMA as Method>::Input

source§

impl Method for StDev

§

type Params = u8

§

type Input = f64

§

type Output = <StDev as Method>::Input

source§

impl Method for TEMA

§

type Params = u8

§

type Input = f64

§

type Output = <TEMA as Method>::Input

source§

impl Method for TMA

§

type Params = u8

§

type Input = f64

§

type Output = <TMA as Method>::Input

source§

impl Method for TR

§

type Params = ()

§

type Input = dyn OHLCV

§

type Output = f64

source§

impl Method for TRIMA

§

type Params = u8

§

type Input = f64

§

type Output = <TRIMA as Method>::Input

source§

impl Method for TSI

§

type Params = (u8, u8)

§

type Input = f64

§

type Output = <TSI as Method>::Input

source§

impl Method for UpperReversalSignal

§

type Params = (u8, u8)

§

type Input = f64

§

type Output = Action

source§

impl Method for VWMA

§

type Params = u8

§

type Input = (f64, f64)

§

type Output = f64

source§

impl Method for Vidya

§

type Params = u8

§

type Input = f64

§

type Output = <Vidya as Method>::Input

source§

impl Method for WMA

§

type Params = u8

§

type Input = f64

§

type Output = <WMA as Method>::Input

source§

impl Method for WSMA

§

type Params = u8

§

type Input = f64

§

type Output = <WSMA as Method>::Input

source§

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

§

type Params = <T as Method>::Params

§

type Input = <T as Method>::Input

§

type Output = <T as Method>::Output

source§

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

§

type Params = <T as Method>::Params

§

type Input = <T as Method>::Input

§

type Output = <T as Method>::Output

source§

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

§

type Params = usize

§

type Input = T

§

type Output = Option<T>

source§

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

§

type Params = u8

§

type Input = T

§

type Output = T