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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
//! Trend Indicators
//!
//! Indicators where the direction may signify opportunities. The slope and trajectory of the
//! indicator are more important than the magnitude of the resulting value.
//! In some cases, the strength of the trend can be derived.
use std::iter;
use itertools::{izip, multiunzip};
use crate::momentum::_swing;
use crate::smooth;
use crate::volatility::_true_range;
/// Shinohara intensity ratio
///
/// Measures trend intensity by plotting Strong Ratio (Strength) and Weak Ratio (Popularity) lines.
/// The Strong Ratio is (high - prev close) / (prev close - low) and the Weak Ratio
/// is (high - close) / (close - low).
///
/// # Usage
///
/// When Strong Ratio is above the Weak, it suggests uptrend with greater intensity the greater,
/// the delta.
///
/// NOTE: Implementation differs from source where weak ratio uses close rather than open.
///
/// # Source
///
/// https://www.sevendata.co.jp/shihyou/technical/shinohara.html
///
/// # Examples
///
/// ```
/// use traquer::trend;
///
/// trend::shinohara(
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// 6).collect::<Vec<(f64,f64)>>();
///
/// ```
pub fn shinohara<'a>(
high: &'a [f64],
low: &'a [f64],
close: &'a [f64],
window: usize,
) -> impl Iterator<Item = (f64, f64)> + 'a {
let high_win = high
.windows(window)
.map(|w| w.iter().sum())
.collect::<Vec<f64>>();
let low_win = low
.windows(window)
.map(|w| w.iter().sum())
.collect::<Vec<f64>>();
let close_win = close
.windows(window)
.map(|w| w.iter().sum())
.collect::<Vec<f64>>();
// yahoo uses close rather than open for weak ratio described above
let weak_ratio = izip!(&high_win, &low_win, &close_win)
.map(|(h, l, c)| 100.0 * (h - c) / (c - l))
.collect::<Vec<f64>>();
let strong_ratio = izip!(
&high_win[1..],
&low_win[1..],
&close_win[..close_win.len() - 1]
)
.map(|(h, l, c)| 100.0 * (h - c) / (c - l))
.collect::<Vec<f64>>();
iter::repeat(f64::NAN)
.take(1)
.chain(strong_ratio)
.zip(weak_ratio)
}
/// Average directional index
///
/// Measures strength a trend, not the direction, by directional movement by comparing
/// the difference between two consecutive lows with the difference between their
/// respective highs.
///
/// # Usage
///
/// When +DMI is above -DMI, it suggests an uptrend. A higher ADX value suggest strength of trend
///
/// # Source
///
/// https://www.investopedia.com/terms/a/adx.asp
///
/// # Examples
///
/// ```
/// use traquer::trend;
///
/// trend::adx(
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// 3, 6).collect::<Vec<(f64,f64,f64)>>();
///
/// ```
pub fn adx<'a>(
high: &'a [f64],
low: &'a [f64],
close: &'a [f64],
window: usize,
smoothing: usize,
) -> impl Iterator<Item = (f64, f64, f64)> + 'a {
let (dm_pos, dm_neg, tr): (Vec<_>, Vec<_>, Vec<_>) = multiunzip(
izip!(
&high[..high.len() - 1],
&high[1..],
&low[..low.len() - 1],
&low[1..],
&close[..close.len() - 1],
)
.map(|(prevh, h, prevl, l, prevc)| {
let dm_pos = if h - prevh > prevl - l {
f64::max(0.0, h - prevh)
} else {
0.0
};
let dm_neg = if prevl - l > h - prevh {
f64::max(0.0, prevl - l)
} else {
0.0
};
let tr = (h - l).max(f64::abs(h - prevc)).max(f64::abs(l - prevc));
(dm_pos, dm_neg, tr)
}),
);
let atr = smooth::wilder(&tr, window).collect::<Vec<f64>>();
let di_pos = izip!(smooth::wilder(&dm_pos, window), &atr)
.map(|(di, tr)| di / tr * 100.0)
.collect::<Vec<f64>>();
let di_neg = izip!(smooth::wilder(&dm_neg, window), &atr)
.map(|(di, tr)| di / tr * 100.0)
.collect::<Vec<f64>>();
let dx = izip!(&di_pos, &di_neg)
.map(|(pos, neg)| f64::abs(pos - neg) / (pos + neg) * 100.0)
.collect::<Vec<f64>>();
izip!(
di_pos,
di_neg,
iter::repeat(f64::NAN)
.take(smoothing - 1)
.chain(smooth::wilder(&dx, smoothing).collect::<Vec<f64>>()),
)
}
/// Vortex
///
/// Calculates two lines: VI+ and VI-. The greater the distance between the low of a price bar and
/// the subsequent bar's high, the greater the positive Vortex movement (VM+).
///
/// # Usage
///
/// When VI+ crosses above VI-, it suggests a uptrend.
///
/// # Source
///
/// https://en.wikipedia.org/wiki/Vortex_indicator
/// https://www.investopedia.com/terms/v/vortex-indicator-vi.asp
///
/// # Examples
///
/// ```
/// use traquer::trend;
///
/// trend::vortex(
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// 6).collect::<Vec<(f64,f64)>>();
///
/// ```
pub fn vortex<'a>(
high: &'a [f64],
low: &'a [f64],
close: &'a [f64],
window: usize,
) -> impl Iterator<Item = (f64, f64)> + 'a {
izip!(
&high[..high.len() - 1],
&high[1..],
&low[..low.len() - 1],
&low[1..],
&close[..close.len() - 1],
)
.map(|(prevh, h, prevl, l, prevc)| {
let vm_pos = (h - prevl).abs();
let vm_neg = (l - prevh).abs();
let tr = (h - l).max(f64::abs(h - prevc)).max(f64::abs(l - prevc));
(vm_pos, vm_neg, tr)
})
.collect::<Vec<(f64, f64, f64)>>()
.windows(window)
.map(|w| {
let (vm_pos, vm_neg, tr) = w
.iter()
.copied()
.reduce(|(acc_pos, acc_neg, acc_tr), (pos, neg, tr)| {
(acc_pos + pos, acc_neg + neg, acc_tr + tr)
})
.unwrap();
(vm_pos / tr, vm_neg / tr)
})
.collect::<Vec<(f64, f64)>>()
.into_iter()
}
/// Accumulative Swing Index
///
/// Cumulative sum of Swing Index
///
/// # Usage
///
/// An increasing value suggests an uptrend.
///
/// # Source
///
/// https://www.investopedia.com/terms/a/asi.asp
/// https://quantstrategy.io/blog/accumulative-swing-index-how-to-trade/
///
/// # Examples
///
/// ```
/// use traquer::trend;
///
/// trend::asi(
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// 0.5).collect::<Vec<f64>>();
///
/// ```
pub fn asi<'a>(
open: &'a [f64],
high: &'a [f64],
low: &'a [f64],
close: &'a [f64],
limit: f64,
) -> impl Iterator<Item = f64> + 'a {
_swing(open, high, low, close, limit).scan(0.0, |acc, x| {
*acc += x;
Some(*acc)
})
}
/// Ulcer Index
///
/// Measures downside risk in terms of both the depth and duration of price declines.
/// The index increases in value as the price moves farther away from a recent high
/// and falls as the price rises to new highs.
///
/// # Usage
///
/// Value decreases during uptrends.
///
/// # Source
///
/// https://en.wikipedia.org/wiki/Ulcer_index
///
/// # Examples
///
/// ```
/// use traquer::trend;
///
/// trend::ulcer(
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// 6).collect::<Vec<f64>>();
///
/// ```
pub fn ulcer(data: &[f64], window: usize) -> impl Iterator<Item = f64> + '_ {
let highest = data
.windows(window)
.map(|w| w.iter().fold(f64::NAN, |state, &x| state.max(x)));
smooth::sma(
&highest
.zip(data.iter().skip(window - 1))
.map(|(high, c)| (100.0 * (c - high) / high).powi(2))
.collect::<Vec<f64>>(),
window,
)
.map(|x| x.sqrt())
.collect::<Vec<f64>>()
.into_iter()
}
/// Supertrend
///
/// Acts as a dynamic level of support or resistance.
///
/// # Usage
///
/// A value above close line suggests downtrend.
///
/// # Source
///
/// https://www.tradingview.com/support/solutions/43000634738-supertrend/
/// https://www.investopedia.com/supertrend-indicator-7976167
///
/// # Examples
///
/// ```
/// use traquer::trend;
///
/// trend::supertrend(
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// 6, 3.0).collect::<Vec<f64>>();
///
/// ```
pub fn supertrend<'a>(
high: &'a [f64],
low: &'a [f64],
close: &'a [f64],
window: usize,
multiplier: f64,
) -> impl Iterator<Item = f64> + 'a {
// TODO: needs a test for when it actually flips to use upper band line
let tr = _true_range(high, low, close).collect::<Vec<f64>>();
let atr = smooth::wilder(&tr, window);
izip!(&high[window..], &low[window..], &close[window..], atr)
.scan(
(f64::NAN, f64::NAN, f64::MIN_POSITIVE, 1),
|state, (h, l, c, tr)| {
let (prevlower, prevupper, prevc, prevdir) = state;
let mut lower = (h + l) / 2.0 - multiplier * tr;
let mut upper = (h + l) / 2.0 + multiplier * tr;
if prevc > prevlower && *prevlower > lower {
lower = *prevlower;
}
if prevc < prevupper && *prevupper < upper {
upper = *prevupper;
}
let dir = if c > prevupper {
1
} else if c < prevlower {
-1
} else {
*prevdir
};
*state = (lower, upper, *c, dir);
if dir > 0 {
Some(lower)
} else {
Some(upper)
}
},
)
.collect::<Vec<f64>>()
.into_iter()
}
/// Random Walk Index
///
/// Compares a security's price movements to random movements to determine if it's in a trend.
///
/// # Usage
///
/// When RWI High is above RWI Low, it suggests an uptrend and strength increases as it goes
/// above 1.
///
/// NOTE: Window includes current price where other libraries use window strictly as lookback.
/// You may need to add 1 to window for comparable behaviour.
///
/// # Source
///
/// https://www.technicalindicators.net/indicators-technical-analysis/168-rwi-random-walk-index
/// https://www.investopedia.com/terms/r/random-walk-index.asp
///
/// # Examples
///
/// ```
/// use traquer::trend;
///
/// trend::rwi(
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// 6).collect::<Vec<(f64, f64)>>();
///
/// ```
pub fn rwi<'a>(
high: &'a [f64],
low: &'a [f64],
close: &'a [f64],
window: usize,
) -> impl Iterator<Item = (f64, f64)> + 'a {
// looks back n number of periods *including* current. other libs may not include current.
izip!(
high[1..].windows(window),
low[1..].windows(window),
_true_range(high, low, close)
.collect::<Vec<f64>>()
.windows(window),
)
.map(|(h, l, tr)| {
let mut rwi_high: f64 = 0.0;
let mut rwi_low: f64 = 0.0;
let mut tr_sum = 0.0;
for i in 2..=window {
tr_sum += tr[window - i];
let denom = (tr_sum / (i - 1) as f64) * ((i - 1) as f64).sqrt();
rwi_high = rwi_high.max((h[window - 1] - l[window - i]) / denom);
rwi_low = rwi_low.max((h[window - i] - l[window - 1]) / denom);
}
(rwi_high, rwi_low)
})
.collect::<Vec<(f64, f64)>>()
.into_iter()
}
/// Parabolic Stop and Reverse (SAR)
///
/// Calculating the stop for each upcoming period. When the stop is hit you close the
/// current trade and initiate a new trade in the opposite direction.
///
/// # Usage
///
/// A value above close line suggests downtrend.
///
/// # Source
///
/// https://www.investopedia.com/terms/p/parabolicindicator.asp
///
/// # Examples
///
/// ```
/// use traquer::trend;
///
/// trend::psar(
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// None, None).collect::<Vec<f64>>();
///
/// ```
pub fn psar<'a>(
high: &'a [f64],
low: &'a [f64],
af: Option<f64>,
af_max: Option<f64>,
) -> impl Iterator<Item = f64> + 'a {
let af_inc = af.unwrap_or(0.02);
let mut af = af_inc;
let af_max = af_max.unwrap_or(0.2);
let mut fall = true;
let mut sar = high[0];
let mut ep = low[0];
let mut flip: bool;
let mut result = Vec::with_capacity(high.len() - 1);
for i in 0..high.len() - 1 {
sar = sar + af * (ep - sar);
if fall {
flip = high[i + 1] > sar;
if low[i + 1] < ep {
ep = low[i + 1];
af = f64::min(af + af_inc, af_max);
}
sar = f64::max(f64::max(high[std::cmp::max(i, 1) - 1], high[i + 1]), sar)
} else {
flip = low[i + 1] < sar;
if high[i + 1] > ep {
ep = high[i + 1];
af = f64::min(af + af_inc, af_max);
}
sar = f64::min(f64::min(low[std::cmp::max(i, 1) - 1], low[i + 1]), sar)
}
if flip {
sar = ep;
af = af_inc;
fall = !fall;
ep = if fall { low[i + 1] } else { high[i + 1] };
}
result.push(sar);
}
result.into_iter()
}
/// Detrended Price Oscillator
///
/// Measure the difference between a security's price and its trend and attempts to identify
/// cyclicality. It behaves closer to a momentum indicator
///
/// # Usage
///
/// Suggests uptrend when above zero or when value reaches prior low of oscillator.
///
/// # Examples
///
/// ```
/// use traquer::{trend,smooth};
///
/// trend::dpo(
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// 6, Some(smooth::MaMode::SMA)).collect::<Vec<f64>>();
///
/// ```
pub fn dpo(
data: &[f64],
window: usize,
mamode: Option<smooth::MaMode>,
) -> impl Iterator<Item = f64> + '_ {
let ma = smooth::ma(data, window, mamode.unwrap_or(smooth::MaMode::SMA));
let lag = window / 2 + 1;
data[window - lag - 1..].iter().zip(ma).map(|(x, y)| x - y)
}
/// Aroon
///
/// Measures the trend strength and direction of an asset based on the time between highs and
/// lows. Consists of two lines that show the time since a recent high or low.
///
/// # Usage
///
/// When up line is above the down, it suggests an uptrend.
///
/// # Source
///
/// https://www.tradingview.com/support/solutions/43000501801-aroon/
///
/// # Examples
///
/// ```
/// use traquer::trend;
///
/// trend::aroon(
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// &vec![1.0,2.0,3.0,4.0,5.0,6.0,4.0,5.0],
/// 3).collect::<Vec<(f64, f64)>>();
///
/// ```
pub fn aroon<'a>(
high: &'a [f64],
low: &'a [f64],
window: usize,
) -> impl Iterator<Item = (f64, f64)> + 'a {
high.windows(window + 1)
.zip(low.windows(window + 1))
.map(move |(h, l)| {
let (hh, _) =
h.iter().enumerate().fold(
(0, h[0]),
|state, (idx, x)| {
if x >= &state.1 {
(idx, *x)
} else {
state
}
},
);
let (ll, _) =
l.iter().enumerate().fold(
(0, l[0]),
|state, (idx, x)| {
if x <= &state.1 {
(idx, *x)
} else {
state
}
},
);
(
hh as f64 / window as f64 * 100.0,
ll as f64 / window as f64 * 100.0,
)
})
}