1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//! Equivolume — the price box height and its volume-scaled width.
use crate::error::{Error, Result};
use crate::indicators::sma::Sma;
use crate::ohlcv::Candle;
use crate::traits::Indicator;
/// Output of [`Equivolume`]: the box's price height and its volume-relative width.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct EquivolumeOutput {
/// Box height — the bar's price range `high − low`.
pub height: f64,
/// Box width — volume relative to its `period` average (`1.0` = average).
pub width: f64,
}
/// Equivolume — Richard Arms' charting style rendered as numbers: each bar is a
/// "box" whose **height** is its price range and whose **width** is its volume
/// relative to the recent average.
///
/// ```text
/// height = high − low
/// width = volume / SMA(volume, period) (1.0 = average volume)
/// ```
///
/// Equivolume discards time and substitutes volume for the horizontal axis: a tall
/// narrow box is an easy move (big range on light volume), while a short wide box
/// is churn (small range on heavy volume) that often marks support/resistance.
/// Reporting the two dimensions lets you reconstruct that shape programmatically:
/// the height/width relationship is Arms' "ease of movement" read. The width is
/// normalised by the volume SMA so it self-scales across instruments.
///
/// The first value lands after `period` inputs (to seed the volume average). Each
/// `update` is O(1).
///
/// # Example
///
/// ```
/// use wickra_core::{Candle, Indicator, Equivolume};
///
/// let mut indicator = Equivolume::new(14).unwrap();
/// let mut last = None;
/// for i in 0..40 {
/// let base = 100.0 + f64::from(i);
/// let c = Candle::new(base, base + 2.0, base - 2.0, base, 1_000.0 + f64::from(i), 0).unwrap();
/// last = indicator.update(c);
/// }
/// assert!(last.is_some());
/// ```
#[derive(Debug, Clone)]
pub struct Equivolume {
period: usize,
vol_sma: Sma,
last: Option<EquivolumeOutput>,
}
impl Equivolume {
/// Construct an Equivolume with the given volume-averaging `period`.
///
/// # Errors
///
/// Returns [`Error::PeriodZero`] if `period == 0`.
pub fn new(period: usize) -> Result<Self> {
if period == 0 {
return Err(Error::PeriodZero);
}
Ok(Self {
period,
vol_sma: Sma::new(period)?,
last: None,
})
}
/// Configured volume-averaging period.
pub const fn period(&self) -> usize {
self.period
}
/// Current value if available.
pub const fn value(&self) -> Option<EquivolumeOutput> {
self.last
}
}
impl Indicator for Equivolume {
type Input = Candle;
type Output = EquivolumeOutput;
fn update(&mut self, candle: Candle) -> Option<EquivolumeOutput> {
let avg_vol = self.vol_sma.update(candle.volume)?;
let height = candle.high - candle.low;
let width = if avg_vol > 0.0 {
candle.volume / avg_vol
} else {
0.0
};
let out = EquivolumeOutput { height, width };
self.last = Some(out);
Some(out)
}
fn reset(&mut self) {
self.vol_sma.reset();
self.last = None;
}
fn warmup_period(&self) -> usize {
self.period
}
fn is_ready(&self) -> bool {
self.last.is_some()
}
fn name(&self) -> &'static str {
"Equivolume"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::BatchExt;
use approx::assert_relative_eq;
fn c(high: f64, low: f64, volume: f64) -> Candle {
Candle::new_unchecked(low, high, low, f64::midpoint(high, low), volume, 0)
}
#[test]
fn rejects_zero_period() {
assert!(matches!(Equivolume::new(0), Err(Error::PeriodZero)));
}
#[test]
fn accessors_and_metadata() {
let e = Equivolume::new(14).unwrap();
assert_eq!(e.period(), 14);
assert_eq!(e.warmup_period(), 14);
assert_eq!(e.name(), "Equivolume");
assert!(!e.is_ready());
assert_eq!(e.value(), None);
}
#[test]
fn first_emission_at_warmup_period() {
let mut e = Equivolume::new(3).unwrap();
let candles: Vec<Candle> = (0..6).map(|_| c(102.0, 98.0, 1_000.0)).collect();
let out = e.batch(&candles);
for v in out.iter().take(2) {
assert!(v.is_none());
}
assert!(out[2].is_some());
}
#[test]
fn height_is_range() {
let mut e = Equivolume::new(2).unwrap();
let out = e
.batch(&[c(105.0, 100.0, 1_000.0), c(105.0, 100.0, 1_000.0)])
.into_iter()
.flatten()
.last()
.unwrap();
assert_relative_eq!(out.height, 5.0, epsilon = 1e-9);
}
#[test]
fn average_volume_width_is_one() {
let mut e = Equivolume::new(3).unwrap();
let out = e
.batch(&[c(102.0, 98.0, 1_000.0); 6])
.into_iter()
.flatten()
.last()
.unwrap();
assert_relative_eq!(out.width, 1.0, epsilon = 1e-9);
}
#[test]
fn heavy_bar_is_wide() {
let mut e = Equivolume::new(3).unwrap();
let candles = [
c(102.0, 98.0, 1_000.0),
c(102.0, 98.0, 1_000.0),
c(102.0, 98.0, 4_000.0),
];
let out = e.batch(&candles).into_iter().flatten().last().unwrap();
assert!(
out.width > 1.0,
"a heavy bar should be wider than average, got {}",
out.width
);
}
#[test]
fn reset_clears_state() {
let mut e = Equivolume::new(3).unwrap();
e.batch(&[c(102.0, 98.0, 1_000.0); 6]);
assert!(e.is_ready());
e.reset();
assert!(!e.is_ready());
assert_eq!(e.value(), None);
assert_eq!(e.update(c(102.0, 98.0, 1_000.0)), None);
}
#[test]
fn zero_volume_gives_zero_width() {
let mut e = Equivolume::new(2).unwrap();
let out = e
.batch(&[c(11.0, 9.0, 0.0), c(12.0, 10.0, 0.0), c(13.0, 11.0, 0.0)])
.into_iter()
.flatten()
.last()
.unwrap();
assert_eq!(out.width, 0.0);
}
#[test]
fn batch_equals_streaming() {
let candles: Vec<Candle> = (0..80)
.map(|i| {
c(
110.0 + (f64::from(i) * 0.25).sin() * 5.0,
90.0,
1_000.0 + f64::from(i),
)
})
.collect();
let batch = Equivolume::new(14).unwrap().batch(&candles);
let mut b = Equivolume::new(14).unwrap();
let streamed: Vec<_> = candles.iter().map(|x| b.update(*x)).collect();
assert_eq!(batch, streamed);
}
}