Technical indicators module for tzutrader
Pure Nim implementations of all technical indicators. Replaces pybottrader's C++ indicators with native Nim code.
Features:
- Moving averages (SMA, EMA, WMA)
- Momentum indicators (RSI, Stochastic, ROC)
- Trend indicators (MACD, ADX)
- Volatility indicators (ATR, Bollinger Bands, Standard Deviation)
- Volume indicators (OBV)
- Both batch and streaming modes
- Memory-efficient rolling windows
- NaN handling for partial data
Types
ATR = ref object of IndicatorBase
- Streaming Average True Range
EMA = ref object of IndicatorBase
- Streaming Exponential Moving Average
IndicatorBase = ref object of RootObj period*: int values*: Deque[float64] initialized*: bool
- Base type for streaming indicators
MACD = ref object of IndicatorBase
- Streaming MACD indicator
OBV = ref object of IndicatorBase
- Streaming On-Balance Volume
RSI = ref object of IndicatorBase
- Streaming Relative Strength Index
SMA = ref object of IndicatorBase
- Streaming Simple Moving Average
Procs
proc addValue(ind: IndicatorBase; value: float64) {....raises: [], tags: [], forbids: [].}
- Add value to indicator's rolling window
proc atr(high, low, close: seq[float64]; period: int = 14): seq[float64] {. ...raises: [], tags: [], forbids: [].}
-
Calculate Average True Range (ATR)
Args: high: High prices low: Low prices close: Close prices period: Number of periods (default 14)
Returns: Sequence of ATR values
proc bollinger(data: seq[float64]; period: int = 20; stdDev: float64 = 2.0): tuple[ upper, middle, lower: seq[float64]] {....raises: [], tags: [], forbids: [].}
-
Calculate Bollinger Bands
Args: data: Price data period: Number of periods (default 20) stdDev: Number of standard deviations (default 2.0)
Returns: Tuple of (upper band, middle band, lower band)
proc ema(data: seq[float64]; period: int): seq[float64] {....raises: [], tags: [], forbids: [].}
-
Calculate Exponential Moving Average (EMA)
Args: data: Price data period: Number of periods for average
Returns: Sequence of EMA values
proc isFull(ind: IndicatorBase): bool {....raises: [], tags: [], forbids: [].}
- Check if indicator has enough data
proc isNaN(x: float64): bool {.inline, ...raises: [], tags: [], forbids: [].}
- Check if value is NaN
proc macd(data: seq[float64]; fastPeriod: int = 12; slowPeriod: int = 26; signalPeriod: int = 9): tuple[macd, signal, histogram: seq[float64]] {. ...raises: [], tags: [], forbids: [].}
-
Calculate MACD (Moving Average Convergence Divergence)
Args: data: Price data fastPeriod: Fast EMA period (default 12) slowPeriod: Slow EMA period (default 26) signalPeriod: Signal line EMA period (default 9)
Returns: Tuple of (MACD line, signal line, histogram)
proc nanToZero(x: float64): float64 {.inline, ...raises: [], tags: [], forbids: [].}
- Convert NaN to zero
proc obv(close, volume: seq[float64]): seq[float64] {....raises: [], tags: [], forbids: [].}
-
Calculate On-Balance Volume (OBV)
Args: close: Close prices volume: Volume data
Returns: Sequence of OBV values
proc roc(data: seq[float64]; period: int = 12): seq[float64] {....raises: [], tags: [], forbids: [].}
-
Calculate Rate of Change (ROC)
Args: data: Price data period: Number of periods
Returns: Sequence of ROC values (percentage)
proc roi(initial, final: float64): float64 {....raises: [], tags: [], forbids: [].}
-
Calculate Return on Investment (ROI)
Args: initial: Initial value final: Final value
Returns: ROI as percentage
proc rsi(data: seq[float64]; period: int = 14): seq[float64] {....raises: [], tags: [], forbids: [].}
-
Calculate Relative Strength Index (RSI)
Args: data: Price data period: Number of periods (default 14)
Returns: Sequence of RSI values (0-100)
proc sma(data: seq[float64]; period: int): seq[float64] {....raises: [], tags: [], forbids: [].}
-
Calculate Simple Moving Average (SMA)
Args: data: Price data period: Number of periods for average
Returns: Sequence of SMA values (NaN for insufficient data)
proc stddev(data: seq[float64]; period: int): seq[float64] {....raises: [], tags: [], forbids: [].}
-
Calculate rolling standard deviation
Args: data: Data series period: Number of periods
Returns: Sequence of standard deviation values
proc update(atr: ATR; high, low, close: float64): float64 {....raises: [], tags: [], forbids: [].}
- Update ATR with new OHLC data
proc update(ema: EMA; value: float64): float64 {....raises: [], tags: [], forbids: [].}
- Update EMA with new value and return current EMA
proc update(macd: MACD; price: float64): tuple[macd, signal, histogram: float64] {. ...raises: [], tags: [], forbids: [].}
- Update MACD with new price
proc update(obv: OBV; close, volume: float64): float64 {....raises: [], tags: [], forbids: [].}
- Update OBV with new close/volume data
proc wma(data: seq[float64]; period: int): seq[float64] {....raises: [], tags: [], forbids: [].}
-
Calculate Weighted Moving Average (WMA)
Args: data: Price data period: Number of periods for average
Returns: Sequence of WMA values