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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//! TTM Squeeze (John Carter).
use std::collections::VecDeque;
use crate::error::{Error, Result};
use crate::indicators::atr::Atr;
use crate::indicators::bollinger::BollingerBands;
use crate::indicators::sma::Sma;
use crate::ohlcv::Candle;
use crate::traits::Indicator;
/// TTM Squeeze output.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TtmSqueezeOutput {
/// `1.0` while the squeeze is *on* (Bollinger Bands sit inside the Keltner
/// Channel), `0.0` otherwise. The squeeze releases — the signal flips back
/// to `0.0` — when volatility expands and BB pierce KC.
pub squeeze: f64,
/// Detrended momentum: linear-regression endpoint of
/// `close − (midpoint(highest_high, lowest_low, period) + SMA(close, period)) / 2`.
/// Histogram-like reading that swings positive in a breakout up, negative
/// in a breakout down; trade direction on the squeeze release follows the
/// sign of `momentum`.
pub momentum: f64,
}
/// TTM Squeeze (John Carter): a Bollinger-vs-Keltner volatility squeeze paired
/// with a detrended-close momentum reading.
///
/// Carter's setup detects coiled markets (low realised volatility relative to
/// ATR) and the *direction* of the breakout when they uncoil:
///
/// ```text
/// squeeze = 1.0 if BollingerBands(period, bb_mult)
/// ⊂ KeltnerChannels-like(SMA(period), ATR(period), kc_mult)
/// else 0.0
///
/// hl_mid = (max(high, period) + min(low, period)) / 2
/// detrend = close − (hl_mid + SMA(close, period)) / 2
/// momentum = LinearRegression(detrend, period) // endpoint
/// ```
///
/// The "Keltner-like" envelope here uses an *SMA* centerline (not the EMA of
/// typical price that [`Keltner`](crate::Keltner) uses) plus an ATR offset,
/// exactly as Carter's original publication and every chart-vendor
/// implementation define it. Common parameters: `period = 20`, `bb_mult = 2.0`,
/// `kc_mult = 1.5`.
///
/// # Example
///
/// ```
/// use wickra_core::{Candle, Indicator, TtmSqueeze};
///
/// let mut indicator = TtmSqueeze::new(20, 2.0, 1.5).unwrap();
/// let mut last = None;
/// for i in 0..40 {
/// let base = 100.0 + f64::from(i);
/// let candle =
/// Candle::new(base, base + 2.0, base - 2.0, base + 1.0, 10.0, i64::from(i)).unwrap();
/// last = indicator.update(candle);
/// }
/// assert!(last.is_some());
/// ```
#[derive(Debug, Clone)]
pub struct TtmSqueeze {
period: usize,
kc_mult: f64,
bb: BollingerBands,
sma_close: Sma,
atr: Atr,
highs: VecDeque<f64>,
lows: VecDeque<f64>,
closes: VecDeque<f64>,
// Pre-computed OLS constants over `x = 0..period − 1`.
sum_x: f64,
denom: f64,
}
impl TtmSqueeze {
/// # Errors
/// Returns [`Error::PeriodZero`] if `period == 0` and
/// [`Error::NonPositiveMultiplier`] if either multiplier is not strictly
/// positive and finite. `period >= 2` is required for the linear-regression
/// momentum component.
pub fn new(period: usize, bb_mult: f64, kc_mult: f64) -> Result<Self> {
if period < 2 {
return Err(Error::InvalidPeriod {
message: "TTM squeeze needs period >= 2 for the momentum regression",
});
}
if !bb_mult.is_finite() || bb_mult <= 0.0 || !kc_mult.is_finite() || kc_mult <= 0.0 {
return Err(Error::NonPositiveMultiplier);
}
let n = period as f64;
let sum_x = n * (n - 1.0) / 2.0;
let sum_xx = (n - 1.0) * n * (2.0 * n - 1.0) / 6.0;
Ok(Self {
period,
kc_mult,
bb: BollingerBands::new(period, bb_mult)?,
sma_close: Sma::new(period)?,
atr: Atr::new(period)?,
highs: VecDeque::with_capacity(period),
lows: VecDeque::with_capacity(period),
closes: VecDeque::with_capacity(period),
sum_x,
denom: n * sum_xx - sum_x * sum_x,
})
}
/// John Carter's classic configuration: `period = 20`, `bb_mult = 2.0`,
/// `kc_mult = 1.5`.
pub fn classic() -> Self {
Self::new(20, 2.0, 1.5).expect("classic TTM Squeeze parameters are valid")
}
/// Configured `(period, bb_mult, kc_mult)`.
pub fn parameters(&self) -> (usize, f64, f64) {
(self.period, self.bb.multiplier(), self.kc_mult)
}
}
impl Indicator for TtmSqueeze {
type Input = Candle;
type Output = TtmSqueezeOutput;
fn update(&mut self, candle: Candle) -> Option<TtmSqueezeOutput> {
if self.highs.len() == self.period {
self.highs.pop_front();
self.lows.pop_front();
self.closes.pop_front();
}
self.highs.push_back(candle.high);
self.lows.push_back(candle.low);
self.closes.push_back(candle.close);
// Feed all three sub-indicators unconditionally so they warm up in
// lock-step. ATR returns its first value at bar `period` (Wilder
// seeds), the SMA and BB on bar `period` as well.
let bb = self.bb.update(candle.close);
let mid = self.sma_close.update(candle.close);
let atr = self.atr.update(candle);
let (bb, mid, atr) = (bb?, mid?, atr?);
let kc_upper = mid + self.kc_mult * atr;
let kc_lower = mid - self.kc_mult * atr;
let squeeze = f64::from(bb.upper <= kc_upper && bb.lower >= kc_lower);
// Detrended close. The reference forms it as the deviation of close
// from the average of the rolling high-low midpoint and the SMA of
// close, then runs a linear regression of that series.
let hi = self.highs.iter().copied().fold(f64::NEG_INFINITY, f64::max);
let lo = self.lows.iter().copied().fold(f64::INFINITY, f64::min);
let hl_mid = f64::midpoint(hi, lo);
// Build the detrended window over the closes currently in `closes`.
// We need all `period` closes to fit the regression, which is
// guaranteed once `bb` / `mid` are ready.
let baseline = f64::midpoint(hl_mid, mid);
let mut sum_y = 0.0;
let mut sum_xy = 0.0;
for (i, &c) in self.closes.iter().enumerate() {
let y = c - baseline;
let x = i as f64;
sum_y += y;
sum_xy += x * y;
}
let n = self.period as f64;
let slope = (n * sum_xy - self.sum_x * sum_y) / self.denom;
let intercept = (sum_y - slope * self.sum_x) / n;
let momentum = intercept + slope * (n - 1.0);
Some(TtmSqueezeOutput { squeeze, momentum })
}
fn reset(&mut self) {
self.bb.reset();
self.sma_close.reset();
self.atr.reset();
self.highs.clear();
self.lows.clear();
self.closes.clear();
}
fn warmup_period(&self) -> usize {
self.period
}
fn is_ready(&self) -> bool {
self.bb.is_ready() && self.sma_close.is_ready() && self.atr.is_ready()
}
fn name(&self) -> &'static str {
"TtmSqueeze"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::BatchExt;
use approx::assert_relative_eq;
fn c(h: f64, l: f64, cl: f64) -> Candle {
Candle::new(cl, h, l, cl, 1.0, 0).unwrap()
}
#[test]
fn rejects_invalid_period() {
assert!(TtmSqueeze::new(0, 2.0, 1.5).is_err());
assert!(TtmSqueeze::new(1, 2.0, 1.5).is_err());
}
#[test]
fn rejects_non_positive_multipliers() {
assert!(matches!(
TtmSqueeze::new(20, 0.0, 1.5),
Err(Error::NonPositiveMultiplier)
));
assert!(matches!(
TtmSqueeze::new(20, 2.0, -1.0),
Err(Error::NonPositiveMultiplier)
));
assert!(matches!(
TtmSqueeze::new(20, f64::NAN, 1.5),
Err(Error::NonPositiveMultiplier)
));
}
#[test]
fn accessors_and_metadata() {
let s = TtmSqueeze::classic();
let (p, b, k) = s.parameters();
assert_eq!(p, 20);
assert_relative_eq!(b, 2.0, epsilon = 1e-12);
assert_relative_eq!(k, 1.5, epsilon = 1e-12);
assert_eq!(s.warmup_period(), 20);
assert_eq!(s.name(), "TtmSqueeze");
}
#[test]
fn flat_market_has_zero_momentum() {
let candles: Vec<Candle> = (0..30).map(|_| c(10.0, 10.0, 10.0)).collect();
let mut s = TtmSqueeze::new(20, 2.0, 1.5).unwrap();
let last = s.batch(&candles).into_iter().flatten().last().unwrap();
assert_relative_eq!(last.momentum, 0.0, epsilon = 1e-9);
// With zero volatility both BB and KC collapse to a point, so the
// squeeze is trivially "on".
assert_relative_eq!(last.squeeze, 1.0, epsilon = 1e-12);
}
#[test]
fn batch_equals_streaming() {
let candles: Vec<Candle> = (0..40)
.map(|i| c(f64::from(i) + 2.0, f64::from(i), f64::from(i) + 1.0))
.collect();
let mut a = TtmSqueeze::new(20, 2.0, 1.5).unwrap();
let mut b = TtmSqueeze::new(20, 2.0, 1.5).unwrap();
assert_eq!(
a.batch(&candles),
candles.iter().map(|x| b.update(*x)).collect::<Vec<_>>()
);
}
#[test]
fn reset_clears_state() {
let candles: Vec<Candle> = (0..30)
.map(|i| c(f64::from(i) + 1.0, f64::from(i) - 1.0, f64::from(i)))
.collect();
let mut s = TtmSqueeze::classic();
s.batch(&candles);
assert!(s.is_ready());
s.reset();
assert!(!s.is_ready());
assert_eq!(s.update(candles[0]), None);
}
/// Squeeze fires only after `period` candles, never before.
#[test]
fn warmup_returns_none() {
let mut s = TtmSqueeze::new(20, 2.0, 1.5).unwrap();
for i in 0..19 {
let base = 100.0 + f64::from(i);
assert!(s.update(c(base + 1.0, base - 1.0, base)).is_none());
}
assert!(s.update(c(121.0, 119.0, 120.0)).is_some());
}
/// Squeeze flag is binary — `0.0` or `1.0`.
#[test]
fn squeeze_is_binary() {
let candles: Vec<Candle> = (0..60)
.map(|i| {
let m = 100.0 + (f64::from(i) * 0.4).sin() * 2.0;
c(m + 1.0, m - 1.0, m)
})
.collect();
let mut s = TtmSqueeze::new(20, 2.0, 1.5).unwrap();
for o in s.batch(&candles).into_iter().flatten() {
assert!(o.squeeze == 0.0 || o.squeeze == 1.0);
}
}
}