indicators

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func ADX

func ADX(high, low, close []float64, period int) ([]float64, error)

ADX is the average directional movement index.

func ATR

func ATR(high, low, close []float64, period int) ([]float64, error)

ATR is the average true range.

func CCI

func CCI(high, low, close []float64, period int) ([]float64, error)

CCI is the commodity channel index.

func DEMA

func DEMA(real []float64, period int) ([]float64, error)

DEMA is the double exponential moving average.

func EMA

func EMA(real []float64, period int) ([]float64, error)

EMA is the exponential moving average.

func MFI

func MFI(high, low, close, volume []float64, period int) ([]float64, error)

MFI is the money flow index.

func Mom

func Mom(real []float64, period int) ([]float64, error)

Mom is the momentum: real[i] - real[i-period].

func NATR

func NATR(high, low, close []float64, period int) ([]float64, error)

NATR is the normalized average true range (ATR as a percentage of price).

func OBV

func OBV(close, volume []float64) ([]float64, error)

OBV is on-balance volume. It is cumulative (no warm-up); the first value is the first volume.

func ROC

func ROC(real []float64, period int) ([]float64, error)

ROC is the rate of change (percentage) over period bars.

func RSI

func RSI(real []float64, period int) ([]float64, error)

RSI is the relative strength index.

func SMA

func SMA(real []float64, period int) ([]float64, error)

SMA is the simple moving average.

func TEMA

func TEMA(real []float64, period int) ([]float64, error)

TEMA is the triple exponential moving average.

func WMA

func WMA(real []float64, period int) ([]float64, error)

WMA is the weighted moving average.

func WillR

func WillR(high, low, close []float64, period int) ([]float64, error)

WillR is Williams %R.

Types

type AroonResult

type AroonResult struct{ Down, Up []float64 }

AroonResult holds the Aroon Down and Aroon Up lines.

func Aroon

func Aroon(high, low []float64, period int) (AroonResult, error)

Aroon is the Aroon indicator (Down, Up).

type BBandsResult

type BBandsResult struct{ Upper, Middle, Lower []float64 }

BBandsResult holds the upper, middle, and lower Bollinger Bands.

func BBands

func BBands(real []float64, period int, nbDevUp, nbDevDn float64, maType int) (BBandsResult, error)

BBands is the Bollinger Bands (upper, middle, lower). nbDevUp/nbDevDn are the standard-deviation multipliers; maType selects the middle-band average.

type InputError

type InputError struct {
	Indicator string
	Reason    string
}

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.

func MACD

func MACD(real []float64, fastPeriod, slowPeriod, signalPeriod int) (MACDResult, error)

MACD is the moving-average convergence/divergence (line, signal, histogram). fastPeriod must be less than slowPeriod.

type StochResult

type StochResult struct{ K, D []float64 }

StochResult holds the %K and %D lines (Stochastic and Stochastic RSI).

func Stoch

func Stoch(high, low, close []float64, fastKPeriod, slowKPeriod, slowKMAType, slowDPeriod, slowDMAType int) (StochResult, error)

Stoch is the (slow) stochastic oscillator (%K, %D). maType selects the smoothing average (0=SMA, 1=EMA, ...; see TA-Lib MaType).

func StochRSI

func StochRSI(real []float64, period, fastKPeriod, fastDPeriod, fastDMAType int) (StochResult, error)

StochRSI is the stochastic RSI (%K, %D). maType selects the %D smoothing.

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 NewADX

func NewADX(period int) (*Stream, error)

NewADX returns a streaming average directional index (high, low, close).

func NewATR

func NewATR(period int) (*Stream, error)

NewATR returns a streaming average true range (high, low, close).

func NewAroon

func NewAroon(period int) (*Stream, error)

NewAroon returns a streaming Aroon ([Down, Up]) over (high, low).

func NewBBands

func NewBBands(period int, nbDevUp, nbDevDn float64, maType int) (*Stream, error)

NewBBands returns a streaming Bollinger Bands ([Upper, Middle, Lower]).

func NewCCI

func NewCCI(period int) (*Stream, error)

NewCCI returns a streaming commodity channel index (high, low, close).

func NewDEMA

func NewDEMA(period int) (*Stream, error)

NewDEMA returns a streaming double exponential moving average.

func NewEMA

func NewEMA(period int) (*Stream, error)

NewEMA returns a streaming exponential moving average.

func NewMACD

func NewMACD(fastPeriod, slowPeriod, signalPeriod int) (*Stream, error)

NewMACD returns a streaming MACD ([MACD, Signal, Hist]).

func NewMFI

func NewMFI(period int) (*Stream, error)

NewMFI returns a streaming money flow index (high, low, close, volume).

func NewMom

func NewMom(period int) (*Stream, error)

NewMom returns a streaming momentum.

func NewNATR

func NewNATR(period int) (*Stream, error)

NewNATR returns a streaming normalized average true range (high, low, close).

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 NewROC

func NewROC(period int) (*Stream, error)

NewROC returns a streaming rate of change.

func NewRSI

func NewRSI(period int) (*Stream, error)

NewRSI returns a streaming relative strength index.

func NewSMA

func NewSMA(period int) (*Stream, error)

NewSMA returns a streaming simple moving average.

func NewStoch

func NewStoch(fastKPeriod, slowKPeriod, slowKMAType, slowDPeriod, slowDMAType int) (*Stream, error)

NewStoch returns a streaming stochastic oscillator ([K, D]).

func NewStochRSI

func NewStochRSI(period, fastKPeriod, fastDPeriod, fastDMAType int) (*Stream, error)

NewStochRSI returns a streaming stochastic RSI ([K, D]).

func NewTEMA

func NewTEMA(period int) (*Stream, error)

NewTEMA returns a streaming triple exponential moving average.

func NewWMA

func NewWMA(period int) (*Stream, error)

NewWMA returns a streaming weighted moving average.

func NewWillR

func NewWillR(period int) (*Stream, error)

NewWillR returns a streaming Williams %R (high, low, close).

func (*Stream) Arity

func (s *Stream) Arity() int

Arity reports how many values Update expects per call (1 for single-series indicators, more for OHLCV indicators).

func (*Stream) Name

func (s *Stream) Name() string

Name reports the indicator name.

func (*Stream) Update

func (s *Stream) Update(vals ...float64) ([]float64, error)

Update appends one sample per input series and returns the latest value(s). The number of values must equal Arity.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL