Documentation
¶
Overview ¶
Package indicators provides technical-analysis indicator helpers — moving averages, oscillators, bands, and volume studies — for trading bots.
The numeric kernels are a thin, hardened wrapper over the pure-Go TA-Lib port github.com/markcheno/go-talib, so outputs match the de-facto TA-Lib reference, including its initialization conventions (for example, the first true range is seeded at 0). The wrapper adds three guarantees the bare port does not:
Panic-free. Every function validates its arguments and returns a typed *InputError on misuse (a period below the indicator's minimum, or input series of differing lengths). Too-short input is not an error: it returns an all-NaN result of the input length — the normal "still warming up" state. A defensive recover converts any unexpected port panic into the same typed error, so bad input can never crash the host process.
Explicit warm-up. An indicator is undefined until enough samples have accumulated (its lookback). Those leading positions are returned as NaN, never 0: a bare 0 is indistinguishable from a legitimate zero reading of a centered oscillator. Callers test with math.IsNaN; the count of leading NaN values is the indicator's lookback.
Float confined to the signal layer. Indicators operate on float64. Convert decimal price/quantity strings to float64 once, at the boundary, and use indicator outputs only as signals — never round-trip a float back into an order price, quantity, or balance, which stay decimal strings end to end.
Batch and streaming ¶
Each indicator has a batch form (whole input slice in, full output slice out, of equal length) and a streaming form (a Stream). A Stream feeds one sample — or one OHLCV bar — at a time via Update and returns the latest value(s), NaN until warmed up. This is the natural shape for reacting to a live market feed bar by bar.
A Stream maintains a bounded window of recent samples. Windowed indicators (SMA, WMA, Bollinger Bands, Stochastic, CCI, Williams %R, Aroon, ROC, Momentum, MFI) are exact. Recursive indicators (EMA, DEMA, TEMA, RSI, ATR, NATR, ADX, MACD, Stochastic RSI) carry effectively unbounded history in the reference definition; a Stream computes them over a bounded recent window, so each result converges to the batch value after the warm-up region and stays within floating-point noise of it thereafter. On-balance volume (OBV) is cumulative, and its Stream carries the full running total. A Stream is not safe for concurrent use.
Indicators ¶
Trend and moving averages: SMA, EMA, WMA, DEMA, TEMA.
Momentum and oscillators: RSI, MACD, Stochastic, Stochastic RSI, CCI, ROC, Momentum, Williams %R.
Volatility and bands: Bollinger Bands, ATR, NATR.
Directional: ADX, Aroon.
Volume: OBV, MFI.
Multi-component indicators return a result struct: MACD (MACD, Signal, Histogram), Bollinger Bands (Upper, Middle, Lower), Stochastic and Stochastic RSI (K, D), and Aroon (Down, Up).
Index ¶
- func ADX(high, low, close []float64, period int) ([]float64, error)
- func ATR(high, low, close []float64, period int) ([]float64, error)
- func CCI(high, low, close []float64, period int) ([]float64, error)
- func DEMA(real []float64, period int) ([]float64, error)
- func EMA(real []float64, period int) ([]float64, error)
- func MFI(high, low, close, volume []float64, period int) ([]float64, error)
- func Mom(real []float64, period int) ([]float64, error)
- func NATR(high, low, close []float64, period int) ([]float64, error)
- func OBV(close, volume []float64) ([]float64, error)
- func ROC(real []float64, period int) ([]float64, error)
- func RSI(real []float64, period int) ([]float64, error)
- func SMA(real []float64, period int) ([]float64, error)
- func TEMA(real []float64, period int) ([]float64, error)
- func WMA(real []float64, period int) ([]float64, error)
- func WillR(high, low, close []float64, period int) ([]float64, error)
- type AroonResult
- type BBandsResult
- type InputError
- type MACDResult
- type StochResult
- type Stream
- func NewADX(period int) (*Stream, error)
- func NewATR(period int) (*Stream, error)
- func NewAroon(period int) (*Stream, error)
- func NewBBands(period int, nbDevUp, nbDevDn float64, maType int) (*Stream, error)
- func NewCCI(period int) (*Stream, error)
- func NewDEMA(period int) (*Stream, error)
- func NewEMA(period int) (*Stream, error)
- func NewMACD(fastPeriod, slowPeriod, signalPeriod int) (*Stream, error)
- func NewMFI(period int) (*Stream, error)
- func NewMom(period int) (*Stream, error)
- func NewNATR(period int) (*Stream, error)
- func NewOBV() *Stream
- func NewROC(period int) (*Stream, error)
- func NewRSI(period int) (*Stream, error)
- func NewSMA(period int) (*Stream, error)
- func NewStoch(fastKPeriod, slowKPeriod, slowKMAType, slowDPeriod, slowDMAType int) (*Stream, error)
- func NewStochRSI(period, fastKPeriod, fastDPeriod, fastDMAType int) (*Stream, error)
- func NewTEMA(period int) (*Stream, error)
- func NewWMA(period int) (*Stream, error)
- func NewWillR(period int) (*Stream, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func OBV ¶
OBV is on-balance volume. It is cumulative (no warm-up); the first value is the first volume.
Types ¶
type AroonResult ¶
type AroonResult struct{ Down, Up []float64 }
AroonResult holds the Aroon Down and Aroon Up lines.
type BBandsResult ¶
type BBandsResult struct{ Upper, Middle, Lower []float64 }
BBandsResult holds the upper, middle, and lower Bollinger Bands.
type InputError ¶
InputError reports misuse of an indicator: a period below its minimum, input series of differing lengths, or input the underlying kernel cannot process. Too-short input is NOT an InputError — it returns an all-NaN warm-up result.
func (*InputError) Error ¶
func (e *InputError) Error() string
type MACDResult ¶
type MACDResult struct{ MACD, Signal, Hist []float64 }
MACDResult holds the MACD line, its signal line, and the histogram.
type StochResult ¶
type StochResult struct{ K, D []float64 }
StochResult holds the %K and %D lines (Stochastic and Stochastic RSI).
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
Stream incrementally feeds samples into one indicator. Update appends the new sample (or OHLCV bar) and returns the indicator's latest value(s) — NaN until warmed up. A Stream is not safe for concurrent use.
For multi-component indicators, Update returns the components in the same order as the batch result struct: MACD -> [MACD, Signal, Hist]; BBands -> [Upper, Middle, Lower]; Stoch / StochRSI -> [K, D]; Aroon -> [Down, Up].
func NewOBV ¶
func NewOBV() *Stream
NewOBV returns a streaming on-balance volume (close, volume). It carries the full running total rather than a bounded window, matching the batch result.
func NewStochRSI ¶
NewStochRSI returns a streaming stochastic RSI ([K, D]).