Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:TA Lib Ta lib python Indicator Function API

From Leeroopedia


Knowledge Sources
Domains Technical_Analysis, Quantitative_Finance
Last Updated 2026-02-09 22:00 GMT

Overview

Concrete tool for computing technical analysis indicators via 161 direct function calls provided by the TA-Lib Python wrapper.

Description

The Function API is the primary interface to TA-Lib. Each of the 161 technical indicators is exposed as a top-level function in the talib module. These functions are Cython wrappers around the TA-Lib C library, auto-generated from C header definitions. They accept numpy arrays (or pandas/polars Series via the _wrapper decorator) and return numpy arrays with NaN-filled lookback periods.

Representative functions include:

  • talib.SMA — Simple Moving Average
  • talib.BBANDS — Bollinger Bands (returns 3 arrays)
  • talib.RSI — Relative Strength Index
  • talib.MACD — Moving Average Convergence/Divergence (returns 3 arrays)
  • talib.ATR — Average True Range (requires high, low, close)

Usage

Import this when you need to compute any standard technical analysis indicator with a direct function call. This is the simplest API — pass arrays in, get arrays out.

Code Reference

Source Location

  • Repository: ta-lib-python
  • File: talib/_func.pxi
  • Lines: L1-5324 (all 161 indicator function Cython bindings)
  • Wrapped by: talib/__init__.py:L122-126 (pandas/polars wrapper application)

Signature

# Single-input, single-output indicators
def SMA(real: np.ndarray, timeperiod: int = 30) -> np.ndarray:
    """Simple Moving Average"""

def RSI(real: np.ndarray, timeperiod: int = 14) -> np.ndarray:
    """Relative Strength Index"""

# Single-input, multi-output indicators
def BBANDS(real: np.ndarray, timeperiod: int = 5,
           nbdevup: float = 2.0, nbdevdn: float = 2.0,
           matype: int = 0) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
    """Bollinger Bands - returns (upperband, middleband, lowerband)"""

def MACD(real: np.ndarray, fastperiod: int = 12,
         slowperiod: int = 26, signalperiod: int = 9) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
    """MACD - returns (macd, macdsignal, macdhist)"""

# Multi-input indicators
def ATR(high: np.ndarray, low: np.ndarray, close: np.ndarray,
        timeperiod: int = 14) -> np.ndarray:
    """Average True Range"""

Import

import talib
# All 161 functions available as talib.SMA, talib.RSI, etc.

I/O Contract

Inputs

Name Type Required Description
real np.ndarray (float64) Yes (most indicators) Single price series (typically close prices)
high np.ndarray (float64) Yes (some indicators) High prices (for ATR, STOCH, CDL patterns, etc.)
low np.ndarray (float64) Yes (some indicators) Low prices
close np.ndarray (float64) Yes (some indicators) Close prices
volume np.ndarray (float64) Yes (volume indicators) Volume data
timeperiod int No Lookback window (default varies per function)
matype int No Moving average type (0=SMA, 1=EMA, etc. via MA_Type)

Outputs

Name Type Description
result np.ndarray (float64) Single-output indicators (SMA, RSI, ATR)
result_tuple Tuple[np.ndarray, ...] Multi-output indicators (BBANDS returns 3, MACD returns 3)

Usage Examples

Simple Moving Average

import numpy as np
import talib

close = np.random.random(100)

# Compute 20-period SMA
sma = talib.SMA(close, timeperiod=20)
# sma[0:19] are NaN (lookback period), sma[19:] contain values

Bollinger Bands (Multi-Output)

upper, middle, lower = talib.BBANDS(close, timeperiod=20, nbdevup=2, nbdevdn=2, matype=0)

RSI and MACD

rsi = talib.RSI(close, timeperiod=14)
macd, signal, hist = talib.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9)

Multi-Input: Average True Range

high = np.random.random(100) + 1
low = np.random.random(100)
close = np.random.random(100) + 0.5

atr = talib.ATR(high, low, close, timeperiod=14)

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment