Struct Momentum

Source
pub struct Momentum { /* private fields */ }
Expand description

A Momentum indicator that calculates the change between the current price and the price from a given number of periods ago.

Implementations§

Source§

impl Momentum

Source

pub const DEFAULT_PERIOD: usize = 14usize

The default period for the Momentum indicator.

Source

pub fn new(period: usize) -> Self

Creates a new Momentum indicator with the specified period.

§Arguments
  • period - The number of periods over which to calculate momentum.
§Example
use indexes_rs::v1::momentum::main::Momentum;

let momentum = Momentum::new(14);
Source

pub fn calculate(&mut self, price: f64) -> Option<MomentumResult>

Calculates the current momentum and momentum ratio.

The momentum is computed as the difference between the current price and the price from period periods ago. The momentum ratio is computed as (current_price / past_price) * 100.0.

§Arguments
  • price - The latest price.
§Returns
  • Some(MomentumResult) if there are enough data points and the past price is not zero.
  • None if there aren’t enough values or if the past price is zero (to avoid division by zero).
§Example
use indexes_rs::v1::momentum::main::Momentum;
use indexes_rs::v1::momentum::types::MomentumResult;

let mut momentum = Momentum::new(3);
momentum.calculate(100.0); // first value, not enough data
momentum.calculate(102.0);
if let Some(result) = momentum.calculate(104.0) {
    // Past price is 100.0, so momentum = 104.0 - 100.0 = 4.0
    // and momentum ratio = (104.0 / 100.0) * 100 = 104.0%
    assert_eq!(result.value, 4.0);
    assert_eq!(result.ratio, 104.0);
}

Auto Trait Implementations§

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
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, 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.