Implementation:TA Lib Ta lib python Generate Stream
| 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 streaming (single-value) indicator functions by parsing C header signatures.
Description
generate_stream.py is a code generator script in the `tools/` directory that reads the TA-Lib C library header file (`ta_func.h`) and produces Cython extension code for streaming variants of all indicator functions. It generates the `_stream.pxi` include file that provides `stream_*` prefixed functions returning scalar values instead of arrays.
The key difference from `generate_func.py` is that streaming functions:
- Call the C function with `startIdx = endIdx = length - 1` (only computes the last value)
- Return scalar `double` or `int` values instead of numpy arrays
- Initialize output variables to `NaN` or `0` rather than allocating output arrays
- Extract raw data pointers from input arrays for direct C function calls
This makes streaming functions suitable for real-time, incremental computation where only the most recent indicator value is needed.
Usage
Run this script during the build process to regenerate the Cython streaming function wrappers. It is a companion to `generate_func.py` and is not imported at runtime.
Code Reference
Source Location
- Repository: TA_Lib_Ta_lib_python
- File: tools/generate_stream.py
- Lines: 1-293
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_stream.py > talib/_stream.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 with one `stream_*` Cython `def` per indicator function, returning scalar values |
Usage Examples
Regenerating Stream Wrappers
# From the repository root, with TA-Lib C library installed:
# python tools/generate_stream.py > talib/_stream.pxi
# With custom TA-Lib include path:
# TA_INCLUDE_PATH=/custom/path python tools/generate_stream.py > talib/_stream.pxi
# The output is then compiled by Cython during `python setup.py build_ext`
Generated Output Structure (Example)
# Each generated streaming function follows this pattern:
@wraparound(False)
@boundscheck(False)
def stream_SMA(np.ndarray real not None, int timeperiod=30):
""" SMA(real[, timeperiod=?])"""
cdef:
np.npy_intp length
TA_RetCode retCode
double* real_data
double outreal
real = check_array(real)
real_data = <double*>real.data
length = real.shape[0]
outreal = NaN
retCode = lib.TA_SMA(<int>(length) - 1, <int>(length) - 1,
real_data, timeperiod,
&outbegidx, &outnbelement, &outreal)
_ta_check_success("TA_SMA", retCode)
return outreal