Skip to main content

Module stocks

Module stocks 

Source
Expand description

Ordered price-path analytics (equities or any positive price series).

§Layers

LayerAPIPurpose
Scalarssimple_return, volatility, beta, …One-off metrics
Solutionprice_path_solutionSummary stats + formulas for a full path
SeriesPricePathSolution::seriesPeriod detail (return, wealth, drawdown)
TablesPricePathSeries::print_tableTerminal / copy-paste output
TAta — SMA/EMA/WMA/HMA/Stoch/MACD/BB/KC/Donchian/VWAP/RVOL/RSI/ATR/LinReg + *StateBatch series + incremental push

§Error handling (v0.1+)

Public scalars and price_path_solution return crate::FinanceResult. Empty series, non-positive prices, length mismatches, and zero volatility cases are structured crate::FinanceError values — not panics. Prefer composing with ? when prices come from users or external data.

Modules§

adx
Average Directional Index (ADX) / +DI / −DI / DX
atr
Average True Range (ATR)
bollinger
Bollinger Bands
cci
Commodity Channel Index (CCI)
donchian
Donchian channels
keltner
Keltner Channels
linear_regression
Rolling least-squares linear regression
macd
MACD (Moving Average Convergence Divergence)
mfi
Money Flow Index (MFI)
obv
On-Balance Volume (OBV)
path
Price-path analysis: solution struct, period series, and pretty tables.
returns
Simple and logarithmic returns from prices.
risk
Risk metrics: volatility, Sharpe, Sortino, max drawdown, beta, rolling drawdown, Calmar, Ulcer index, correlation, information ratio, and trade-PnL helpers.
rsi
Relative Strength Index (RSI)
rvol
Relative volume (RVOL)
sar
Parabolic SAR (Stop and Reverse)
supertrend
Supertrend
ta
Technical analysis indicators on price / volume series.
vwap
VWAP (volume-weighted average price)
willr
Williams %R

Structs§

AdxBarOutput
One-bar ADX pack (any field may still be warming up).
AdxParams
ADX / DI Wilder period.
AdxSeries
AdxSolution
AdxState
Incremental ADX / DI / DX.
AtrParams
ATR lookback pack (Wilder).
AtrSeries
AtrSolution
AtrState
Incremental Wilder ATR.
BollingerBarOutput
One-bar Bollinger output.
BollingerParams
Bollinger parameter pack.
BollingerSeries
Middle / upper / lower / %B series.
BollingerSolution
Teaching solution + table.
BollingerState
Incremental Bollinger Bands.
CciParams
CCI lookback (on typical price).
CciSeries
CciSolution
CciState
Incremental CCI. After warm-up each push is O(period) (mean absolute deviation).
DemaState
Double exponential moving average: 2·EMA − EMA(EMA).
DonchianBarOutput
DonchianParams
Donchian lookback.
DonchianSeries
DonchianSolution
DonchianState
Incremental Donchian.
EmaState
Incremental EMA (α = 2/(period+1), seed = SMA of first period closes).
HmaState
Hull moving average state.
KamaParams
Kaufman Adaptive Moving Average parameters.
KamaState
Incremental KAMA.
KeltnerBarOutput
KeltnerParams
Keltner parameter pack (Wilder ATR).
KeltnerSeries
KeltnerSolution
KeltnerState
Incremental Keltner (EMA mid + Wilder ATR).
LinRegBar
One fitted window.
LinRegParams
Rolling regression window length.
LinRegSolution
LinRegState
Incremental rolling regression on a caller-chosen series.
MacdParams
MACD parameter pack: fast < slow, all periods ≥ 1.
MacdSeries
Aligned MACD / signal / histogram series.
MacdSolution
Teaching wrapper with formula strings and a printable table.
MacdState
Incremental MACD (fast/slow/signal EMAs).
MfiParams
MfiSeries
MfiSolution
MfiState
Incremental MFI.
MomBarOutput
One-bar momentum pack.
MomParams
Lookback for MOM / ROC / ROCP.
MomSeries
MomSolution
MomState
Incremental MOM/ROC/ROCP (shared ring of last period closes + current).
NatrSeries
Normalized ATR series.
NatrState
Incremental NATR (wraps AtrState).
ObvParams
OBV has no lookback; pack is a unit for API consistency.
ObvSeries
ObvSolution
ObvState
Incremental OBV. Each push is O(1).
PricePathOptions
Options for price_path_solution.
PricePathPeriod
One step between consecutive prices.
PricePathSeries
Period series for a price path. Derefs to [PricePathPeriod].
PricePathSolution
Full analysis of an ordered price path.
RmaState
Incremental Wilder RMA (also called SMMA). α = 1/period.
RocSeries
RocpSeries
RsiParams
RSI lookback pack (Wilder).
RsiSeries
RsiSolution
RsiState
Incremental Wilder RSI.
RvolParams
RVOL lookback pack.
RvolSeries
RvolSolution
RvolState
Incremental relative volume.
SarBarOutput
SarParams
Parabolic SAR acceleration parameters.
SarSeries
SarSolution
SarState
Incremental Parabolic SAR.
SmaState
Incremental SMA. After warm-up, each SmaState::push is O(1).
StochBarOutput
One-bar stochastic output (warm-up allowed as None).
StochState
Incremental stochastic (fast/full via StochasticParams).
StochasticParams
Unvalidated (but Copy) stochastic parameter pack.
StochasticSeries
Aligned %K / %D output.
StochasticSolution
Teaching wrapper around StochasticSeries.
SupertrendBar
SupertrendParams
Supertrend pack: Wilder ATR period + band multiplier.
SupertrendSeries
SupertrendSolution
SupertrendState
Incremental Supertrend.
TemaState
Triple exponential moving average: 3·e1 − 3·e2 + e3.
ValidatedAdx
ValidatedAtr
Validated ATR config.
ValidatedBollinger
Validated Bollinger config.
ValidatedCci
ValidatedDonchian
ValidatedKama
ValidatedKeltner
Validated Keltner config.
ValidatedLinReg
Validated pack.
ValidatedMacd
Validated MACD config for reuse across many close series.
ValidatedMfi
ValidatedMom
ValidatedObv
Validated pack (always succeeds).
ValidatedRsi
Validated RSI config.
ValidatedRvol
Validated RVOL config.
ValidatedSar
ValidatedStochastic
Params that passed period validation — safe to use in a tight loop.
ValidatedSupertrend
ValidatedVwap
Validated VWAP config.
ValidatedWillr
Validated pack.
VwapParams
VWAP parameter pack.
VwapSeries
VwapSolution
VwapState
Incremental VWAP (cumulative or rolling). Call VwapState::reset at session open if desired.
WillrParams
Williams %R lookback.
WillrSeries
WillrSolution
WillrState
Incremental Williams %R.
WmaState
Incremental WMA: newest sample weight = period, oldest weight = 1.

Enums§

ReturnKind
Kind of return used for mean / vol / Sharpe on the path.
StdevKind
Which denominator to use for window standard deviation (Bollinger, etc.).
VwapMode
Cumulative session vs rolling window.
VwapPriceSource
Price input for VWAP numerator.

Functions§

adx
adx_solution
Examples
atr
atr_solution
Examples
beta
OLS beta of asset returns vs market returns (same length series).
bollinger
bollinger_solution
Teaching solution with formulas + table.
cagr
Compound annual growth rate: (end / start)^(1/years) - 1.
cagr_from_prices
CAGR from a positive price path over years years: (end/start)^(1/years) − 1.
cagr_from_prices_periods
CAGR using (prices.len()−1) / periods_per_year as the year fraction.
calmar_ratio
Calmar ratio: CAGR / |max drawdown| on a positive price series.
calmar_ratio_periods
Calmar with year fraction from sample length and periods_per_year.
cci
cci_solution
Examples
correlation
Pearson correlation of two equal-length series.
dema
DEMA of period closes.
dema_last
donchian
donchian_solution
drawdown_series
Running drawdown series (one value per price, starting at 0).
ema
EMA with span period (α = 2 / (period + 1)). Seed = SMA of the first period closes.
ema_last
Last defined EMA value, if any (via EmaState).
expectancy
Expectancy: mean trade P&L (including zeros).
hma
Hull moving average of period (must be ≥ 2).
hma_last
information_ratio
Information ratio: mean(active) / stdev(active) where active[i] = asset[i] − benchmark[i].
kama
Kaufman adaptive moving average.
kama_last
keltner
keltner_solution
Examples
linear_regression
Batch rolling OLS. series is your choice of bar field (close, high, …).
linear_regression_solution
log_return
Logarithmic return: ln(p1 / p0). Requires strictly positive prices.
log_returns
Log returns for consecutive prices: length prices.len() - 1.
macd
Free function: validate params then compute.
macd_solution
Solution with formulas + table for teaching / audit.
max_drawdown
Maximum peak-to-trough drawdown over a positive price series (most negative fraction).
mean_return
Arithmetic mean of a return series.
mfi
mfi_solution
Examples
mom
mom_solution
Examples
natr
Normalized ATR: 100 * ATR / close when both defined and close ≠ 0.
obv
obv_solution
Examples
price_path_solution
Build a PricePathSolution summarizing returns, risk, and period detail for a price path.
price_volatility
Volatility of simple returns computed from consecutive prices.
profit_factor
Profit factor: sum(positive pnl) / |sum(negative pnl)|.
rma
Wilder RMA / SMMA of period closes.
rma_last
roc
rocp
rolling_max_drawdown
Running maximum drawdown magnitude observed up to each price index.
rsi
rsi_solution
Examples
rvol
rvol_solution
Examples
sar
sar_solution
Examples
sharpe_ratio
Sharpe ratio: (mean - risk_free) / volatility over the return series.
simple_return
Simple return between two prices: (p1 - p0) / p0.
simple_returns
Simple returns for consecutive prices: length prices.len() - 1.
sma
SMA of period closes. Leading period - 1 values are None.
sma_last
Last defined SMA value, if any.
sortino_ratio
Sortino ratio: (mean - target) / downside_deviation, using returns below target only.
stochastics
Stochastic series with raw (possibly unvalidated) params — validates then computes.
stochastics_solution
Teaching solution: formulas + printable %K/%D table.
supertrend
supertrend_solution
Examples
tema
TEMA of period closes.
tema_last
total_return
Total simple return from first to last price: (end - start) / start.
true_range_series
Per-bar true range series (same length as inputs). Bar 0 uses H−L only.
ulcer_index
Ulcer index: sqrt(mean of squared percentage drawdowns) (Martin).
volatility
Sample standard deviation of a return series (population divisor n - 1).
volatility_annualized
Annualized volatility: volatility(returns) * sqrt(periods_per_year).
vwap
vwap_solution
Examples
willr
willr_solution
Examples
win_rate
Win rate over a trade P&L series: count(pnl > 0) / n (zeros count as non-wins).
wma
Weighted moving average (newest weight = period).
wma_last