Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:TA Lib Ta lib python Generate Func

From Leeroopedia


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

Overview

Build tool that generates Cython `.pyx` wrapper code for all TA-Lib array-based indicator functions by parsing C header signatures.

Description

generate_func.py is a code generator script in the `tools/` directory that reads the TA-Lib C library header file (`ta_func.h`) and automatically produces Cython extension code for 158+ technical indicator functions. It generates the `_func.pxi` include file that gets compiled into the `_ta_lib` C extension module.

The generator performs several transformations:

  • Parses C function signatures from `ta_func.h`
  • Filters out float variants (`TA_S_*`), lookback functions, and non-indicator functions (`TA_Set*`, `TA_Restore*`)
  • Produces Cython `def` wrappers with pythonic parameter names (stripping `in`/`optIn` prefixes)
  • Generates input array validation (type, dimensions, contiguity checks)
  • Creates output array allocation with NaN pre-fill for lookback periods
  • Extracts default parameter values and docstrings from the abstract API
  • Emits the `__TA_FUNCTION_NAMES__` list at the end of output

Usage

Run this script during the build process to regenerate the Cython function wrappers when the TA-Lib C library adds or changes indicator functions. It is not imported at runtime; it is a build-time code generation tool.

Code Reference

Source Location

Signature

# Module-level script (no class or main function)
# Key internal function:
def cleanup(name: str) -> str:
    """Convert C parameter names to pythonic names.

    Args:
        name: C parameter name (e.g., 'inReal', 'optInTimePeriod')
    Returns:
        Pythonic name (e.g., 'real', 'timeperiod')
    """

Import

# This is a build-time script, not an importable module.
# Run directly:
# python tools/generate_func.py > talib/_func.pxi

I/O Contract

Inputs

Name Type Required Description
ta_func.h C header file Yes TA-Lib C library header containing function prototypes; located via platform-specific include paths or `TA_INCLUDE_PATH` environment variable
TA_INCLUDE_PATH Environment variable No Override for default include directory search paths (colon/semicolon-separated)
abstract API Python module Yes `talib.abstract` module used to retrieve default parameter values and documentation strings

Outputs

Name Type Description
stdout Cython source code Complete `.pxi` file content including: helper C declarations (`check_array`, `check_length*`, `check_begidx*`, `make_double_array`, `make_int_array`), and one Cython `def` wrapper per indicator function
__TA_FUNCTION_NAMES__ Python list (in output) List of all generated function short names (e.g., `["SMA", "EMA", "RSI", ...]`)

Usage Examples

Regenerating Function Wrappers

# From the repository root, with TA-Lib C library installed:
# python tools/generate_func.py > talib/_func.pxi

# With custom TA-Lib include path:
# TA_INCLUDE_PATH=/custom/path python tools/generate_func.py > talib/_func.pxi

# The output is then compiled by Cython during `python setup.py build_ext`

Generated Output Structure (Example)

# Each generated function follows this pattern:
@wraparound(False)
@boundscheck(False)
def SMA(np.ndarray real not None, int timeperiod=30):
    """ SMA(real[, timeperiod=?])"""
    cdef:
        np.npy_intp length
        int begidx, endidx, lookback
        TA_RetCode retCode
        np.ndarray outreal
    real = check_array(real)
    length = real.shape[0]
    begidx = check_begidx1(length, <double*>(real.data))
    endidx = <int>length - begidx - 1
    lookback = begidx + lib.TA_SMA_Lookback(timeperiod)
    outreal = make_double_array(length, lookback)
    retCode = lib.TA_SMA(0, endidx, <double *>(real.data)+begidx, timeperiod,
                         &outbegidx, &outnbelement, <double *>(outreal.data)+lookback)
    _ta_check_success("TA_SMA", retCode)
    return outreal

Related Pages

Page Connections

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