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/Stoch/MACD/BB/KC/VWAP/RVOL + *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§

bollinger
Bollinger Bands
keltner
Keltner Channels
macd
MACD (Moving Average Convergence Divergence)
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.
rvol
Relative volume (RVOL)
ta
Technical analysis indicators on price / volume series.
vwap
VWAP (volume-weighted average price)

Structs§

BollingerBarOutput
One-bar Bollinger output.
BollingerParams
Bollinger parameter pack.
BollingerSeries
Middle / upper / lower / %B series.
BollingerSolution
Teaching solution + table.
BollingerState
Incremental Bollinger Bands (sample stdev on the window).
EmaState
Incremental EMA (α = 2/(period+1), seed = SMA of first period closes).
KeltnerBarOutput
KeltnerParams
Keltner parameter pack (Wilder ATR).
KeltnerSeries
KeltnerSolution
KeltnerState
Incremental Keltner (EMA mid + Wilder ATR).
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).
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.
RvolParams
RVOL lookback pack.
RvolSeries
RvolSolution
RvolState
Incremental relative volume.
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.
ValidatedBollinger
Validated Bollinger config.
ValidatedKeltner
Validated Keltner config.
ValidatedMacd
Validated MACD config for reuse across many close series.
ValidatedRvol
Validated RVOL config.
ValidatedStochastic
Params that passed period validation — safe to use in a tight loop.
ValidatedVwap
Validated VWAP config.
VwapParams
VWAP parameter pack.
VwapSeries
VwapSolution
VwapState
Incremental VWAP (cumulative or rolling). Call VwapState::reset at session open if desired.

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§

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.
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).
keltner
keltner_solution
Examples
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.
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.
rolling_max_drawdown
Running maximum drawdown magnitude observed up to each price index.
rvol
rvol_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.
total_return
Total simple return from first to last price: (end - start) / start.
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