use crate::bar_indicators::volatility::atr::Atr;
use crate::bar_indicators::average::MovingAverageType;
use crate::bar_indicators::indicator_value::IndicatorValue;
#[derive(Clone)]
pub struct ChoppinessIndex {
period: usize,
high_prices: Vec<f64>,
low_prices: Vec<f64>,
atr_values: Vec<f64>,
choppiness_values: Vec<f64>,
atr: Atr,
choppiness_value: f64,
bars_count: usize,
is_ready: bool,
}
impl ChoppinessIndex {
pub fn new() -> Self {
Self::with_period(14)
}
pub fn with_period(period: usize) -> Self {
assert!(period > 0, "Period must be greater than 0");
Self {
period,
high_prices: Vec::with_capacity(period),
low_prices: Vec::with_capacity(period),
atr_values: Vec::with_capacity(period),
choppiness_values: Vec::with_capacity(period),
atr: Atr::new(1, MovingAverageType::RMA), choppiness_value: 50.0,
bars_count: 0,
is_ready: false,
}
}
pub fn update_bar(&mut self, open: f64, high: f64, low: f64, close: f64, volume: f64) -> f64 {
self.bars_count += 1;
if self.high_prices.len() >= 512 {
self.high_prices.remove(0);
}
if self.low_prices.len() >= 512 {
self.low_prices.remove(0);
}
self.high_prices.push(high);
self.low_prices.push(low);
let atr_value = self.atr.update_bar(open, high, low, close, volume);
if self.atr_values.len() >= 512 {
self.atr_values.remove(0);
}
self.atr_values.push(atr_value);
if self.high_prices.len() >= self.period && self.low_prices.len() >= self.period && self.atr_values.len() >= self.period {
let start_idx = self.high_prices.len() - self.period;
let highest_high = self.high_prices[start_idx..].iter()
.fold(f64::NEG_INFINITY, |a, &b| a.max(b));
let lowest_low = self.low_prices[start_idx..].iter()
.fold(f64::INFINITY, |a, &b| a.min(b));
let atr_start_idx = self.atr_values.len() - self.period;
let atr_sum: f64 = self.atr_values[atr_start_idx..].iter().sum();
let range = highest_high - lowest_low;
if range > 1e-12 && atr_sum > 1e-12 {
let ratio = atr_sum / range;
let log_ratio = ratio.log10();
let log_period = (self.period as f64).log10();
if log_period.abs() > 1e-12 {
self.choppiness_value = 100.0 * log_ratio / log_period;
self.choppiness_value = self.choppiness_value.clamp(0.0, 100.0);
}
}
}
if self.choppiness_values.len() >= 512 {
self.choppiness_values.remove(0);
}
self.choppiness_values.push(self.choppiness_value);
if self.bars_count >= self.period + 5 {
self.is_ready = true;
}
self.choppiness_value
}
pub fn value(&self) -> IndicatorValue {
IndicatorValue::Single(self.choppiness_value)
}
pub fn is_ready(&self) -> bool {
self.is_ready
}
pub fn period(&self) -> usize {
self.period
}
pub fn reset(&mut self) {
self.high_prices.clear();
self.low_prices.clear();
self.atr_values.clear();
self.choppiness_values.clear();
self.atr.reset();
self.choppiness_value = 50.0;
self.bars_count = 0;
self.is_ready = false;
}
pub fn market_condition(&self) -> &'static str {
match self.choppiness_value {
v if v > 61.8 => "Choppy/Sideways", v if v > 50.0 => "Moderately Choppy", v if v > 38.2 => "Moderately Trending", _ => "Trending" }
}
pub fn trading_signal(&self) -> i8 {
if !self.is_ready() {
return 0;
}
match self.choppiness_value {
v if v < 38.2 => 1, v if v > 61.8 => -1, _ => 0 }
}
pub fn advanced_signal(&self) -> i8 {
if !self.is_ready() || self.choppiness_values.len() < 3 {
return 0;
}
let len = self.choppiness_values.len();
let current = self.choppiness_value;
let prev_1 = if len >= 2 { self.choppiness_values[len - 2] } else { 50.0 };
let prev_2 = if len >= 3 { self.choppiness_values[len - 3] } else { 50.0 };
if prev_2 > 50.0 && prev_1 > 40.0 && current < 38.2 {
return 1;
}
if prev_2 < 50.0 && prev_1 < 60.0 && current > 61.8 {
return -1;
}
0
}
pub fn trend_strength(&self) -> f64 {
if !self.is_ready() {
return 0.0;
}
100.0 - self.choppiness_value
}
pub fn sideways_strength(&self) -> f64 {
if !self.is_ready() {
return 0.0;
}
self.choppiness_value
}
pub fn normalized_value(&self) -> f64 {
if !self.is_ready() {
return 0.5;
}
self.choppiness_value / 100.0
}
pub fn trend_direction(&self, lookback: usize) -> i8 {
if !self.is_ready() || self.choppiness_values.len() < lookback + 1 {
return 0;
}
let current = self.choppiness_value;
let past = self.choppiness_values[self.choppiness_values.len() - lookback - 1];
if current > past {
1 } else if current < past {
-1 } else {
0 }
}
pub fn rate_of_change(&self, periods: usize) -> f64 {
if !self.is_ready() || self.choppiness_values.len() < periods + 1 {
return 0.0;
}
let current = self.choppiness_value;
let past = self.choppiness_values[self.choppiness_values.len() - periods - 1];
if past.abs() > 1e-12 {
(current - past) / past * 100.0
} else {
0.0
}
}
pub fn volatility(&self, periods: usize) -> f64 {
if !self.is_ready() || self.choppiness_values.len() < periods {
return 0.0;
}
let start_idx = self.choppiness_values.len() - periods;
let slice = &self.choppiness_values[start_idx..];
let mean = slice.iter().sum::<f64>() / slice.len() as f64;
let variance = slice.iter()
.map(|&x| (x - mean).powi(2))
.sum::<f64>() / slice.len() as f64;
variance.sqrt()
}
pub fn extremes(&self, periods: usize) -> (f64, f64) {
if !self.is_ready() || self.choppiness_values.len() < periods {
return (0.0, 100.0);
}
let start_idx = self.choppiness_values.len() - periods;
let slice = &self.choppiness_values[start_idx..];
let max_val = slice.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));
let min_val = slice.iter().fold(f64::INFINITY, |a, &b| a.min(b));
(min_val, max_val)
}
pub fn average_value(&self, periods: usize) -> f64 {
if !self.is_ready() || self.choppiness_values.len() < periods {
return 50.0;
}
let start_idx = self.choppiness_values.len() - periods;
let slice = &self.choppiness_values[start_idx..];
slice.iter().sum::<f64>() / slice.len() as f64
}
pub fn market_regime(&self) -> &'static str {
match self.choppiness_value {
v if v < 25.0 => "Strong Trend - Use Trend Following",
v if v < 38.2 => "Trend - Use Trend Following",
v if v < 50.0 => "Weak Trend - Use Hybrid Strategies",
v if v < 61.8 => "Weak Chop - Use Hybrid Strategies",
v if v < 75.0 => "Chop - Use Mean Reversion",
_ => "Strong Chop - Use Mean Reversion"
}
}
pub fn info(&self) -> String {
let trend_str = self.trend_strength();
let sideways_str = self.sideways_strength();
let trend_dir = match self.trend_direction(5) {
1 => "More Choppy",
-1 => "More Trending",
_ => "Stable"
};
format!(
"Choppiness Index: {:.1}, Condition: {}, Trend Strength: {:.1}, Sideways Strength: {:.1}, Direction: {}",
self.choppiness_value,
self.market_condition(),
trend_str,
sideways_str,
trend_dir
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_choppiness_index_creation() {
let ci = ChoppinessIndex::new();
assert!(!ci.is_ready());
assert_eq!(ci.value().main(), 50.0);
}
#[test]
fn test_choppiness_index_warmup() {
let mut ci = ChoppinessIndex::with_period(14);
for i in 0..25 {
let price = 100.0 + (i as f64 * 0.1).sin() * 5.0;
ci.update_bar(price, price + 1.0, price - 1.0, price, 1000.0);
}
assert!(ci.is_ready());
}
#[test]
fn test_choppiness_index_range() {
let mut ci = ChoppinessIndex::with_period(14);
for i in 0..30 {
let price = 100.0 + i as f64;
let value = ci.update_bar(price, price + 2.0, price - 2.0, price, 1000.0);
assert!(value >= 0.0 && value <= 100.0, "Choppiness should be in [0, 100]");
}
}
#[test]
fn test_choppiness_index_market_condition() {
let mut ci = ChoppinessIndex::with_period(14);
for i in 0..30 {
let price = 100.0 + i as f64;
ci.update_bar(price, price + 1.0, price - 1.0, price, 1000.0);
}
let condition = ci.market_condition();
assert!(!condition.is_empty());
}
#[test]
fn test_choppiness_index_reset() {
let mut ci = ChoppinessIndex::with_period(14);
for i in 0..25 {
ci.update_bar(100.0 + i as f64, 101.0, 99.0, 100.0 + i as f64, 1000.0);
}
ci.reset();
assert!(!ci.is_ready());
assert_eq!(ci.value().main(), 50.0);
}
}