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
impl RSI
Sourcepub fn new(
period: usize,
overbought: Option<f64>,
oversold: Option<f64>,
) -> Self
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. IfNone, defaults to 70.0.oversold- Optional oversold threshold. IfNone, defaults to 30.0.
Sourcepub fn calculate(&mut self, price: f64) -> Option<RSIResult>
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);
}Sourcepub fn determine_condition(&self, rsi: f64) -> MarketCondition
pub fn determine_condition(&self, rsi: f64) -> MarketCondition
Determines the market condition based on the given RSI value and the configured thresholds.
- Returns
Overboughtif RSI is greater than or equal to the overbought threshold. - Returns
Oversoldif RSI is less than or equal to the oversold threshold. - Returns
Neutralotherwise.
§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);