Skip to main content

Module primitives

Module primitives 

Source
Expand description

§Primitives: the small measuring tools

Everything here is a plain rolling calculation over a price series. Each function returns one value per input bar, and every value at bar t uses only bars <= t (no peeking). Warm-up bars are NaN.

If you have never met these before, the one-line versions are:

ToolOne line
ATR“How far does price usually travel in one bar?”
Standard deviation“How spread out have closes been?”
Linear regression slope“If I drew the best straight line through the last N closes, how steep is it?”
RSI“Of the last N bars’ movement, how much was up?” (0-100)
MACD histogram“Is the fast trend pulling away from the slow trend, and is that speeding up?”

Re-exports§

pub use crate::indicators::ema;
pub use crate::indicators::rolling_std;
pub use crate::indicators::sma;
pub use crate::indicators::OHLCV;

Functions§

atr
Average true range as a simple average (matches ThinkScript’s Keltner channel with averageType = SIMPLE).
diff
x[t] - x[t-k], NaN for the first k bars.
highest
Rolling maximum / minimum over length bars.
linreg_value
Slope-adjusted endpoint of a least-squares line through the last length values (ThinkScript Inertia, a.k.a. LinearRegCurve).
lowest
macd_hist
MACD histogram (12, 26, 9).
ratio
Divide element-wise, NaN where the denominator is not positive.
rsi
Wilder RSI.
squash
A gentle squashing function: tanh(x / scale), so a feature measured in any units ends up in (-1, 1) and one wild bar cannot dominate a blend.
true_range
True range: the biggest of (high-low), |high-prev close|, |low-prev close|.