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
impl Momentum
Sourcepub const DEFAULT_PERIOD: usize = 14usize
pub const DEFAULT_PERIOD: usize = 14usize
The default period for the Momentum indicator.
Sourcepub fn calculate(&mut self, price: f64) -> Option<MomentumResult>
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.Noneif 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§
impl Freeze for Momentum
impl RefUnwindSafe for Momentum
impl Send for Momentum
impl Sync for Momentum
impl Unpin for Momentum
impl UnwindSafe for Momentum
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