Struct RSI

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

A struct for calculating the Relative Strength Index (RSI) with customizable thresholds.

The RSI calculator maintains a sliding window of gains and losses based on incoming price data. Once sufficient data is collected (i.e. equal to the specified period), it returns the current RSI value along with an indication of market condition. Users can customize the overbought and oversold thresholds via the constructor.

Implementations§

Source§

impl RSI

Source

pub fn new( period: usize, overbought: Option<f64>, oversold: Option<f64>, ) -> Self

Creates a new RSI calculator with the specified period and optional thresholds.

§Arguments
  • period - The number of periods over which to calculate the RSI.
  • overbought - Optional overbought threshold. If None, defaults to 70.0.
  • oversold - Optional oversold threshold. If None, defaults to 30.0.
Source

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

Updates the RSI calculation with a new price and returns the current RSI result if available.

This method processes the new price, updates the internal sliding window of gains and losses, and calculates the RSI once enough data has been gathered.

§Arguments
  • price - The latest price data.
§Returns

An Option<RSIResult> containing:

  • value: The calculated RSI.
  • condition: The market condition determined from the RSI value.

Returns None if insufficient data has been provided.

§Examples
use indexes_rs::v1::rsi::main::RSI;
use indexes_rs::v1::sma::main::SMAError;
use indexes_rs::v1::rsi::types::RSIResult;
use indexes_rs::v1::types::TrendDirection;

let mut rsi = RSI::new(14, None, None);
let price = 44.33;
if let Some(result) = rsi.calculate(price) {
    println!("RSI: {:.2}", result.value);
}
Source

pub fn determine_condition(&self, rsi: f64) -> MarketCondition

Determines the market condition based on the given RSI value and the configured thresholds.

  • Returns Overbought if RSI is greater than or equal to the overbought threshold.
  • Returns Oversold if RSI is less than or equal to the oversold threshold.
  • Returns Neutral otherwise.
§Arguments
  • rsi - The RSI value.
§Examples
use indexes_rs::v1::rsi::main::RSI;
use indexes_rs::v1::rsi::types::MarketCondition;
let rsi = RSI::new(14, Some(80.0), Some(20.0));
assert_eq!(rsi.determine_condition(85.0), MarketCondition::Overbought);

Auto Trait Implementations§

§

impl Freeze for RSI

§

impl RefUnwindSafe for RSI

§

impl Send for RSI

§

impl Sync for RSI

§

impl Unpin for RSI

§

impl UnwindSafe for RSI

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.