Implementation:TA Lib Ta lib python Abstract Function Constructor
| Knowledge Sources | |
|---|---|
| Domains | Technical_Analysis, Software_Architecture |
| Last Updated | 2026-02-09 22:00 GMT |
Overview
Concrete tool for creating Abstract API Function objects provided by the abstract.Function factory and _Function.__init__ Cython class.
Description
The Function factory in talib/abstract.py validates the function name and delegates to _Function.__init__ in the Cython extension. The Cython class queries the TA-Lib abstract interface for function metadata (input names, parameters, output names) and stores a reference to the bound C function object.
Pre-instantiated Function objects are available as module globals (abstract.SMA, abstract.RSI, etc.), created at import time.
Usage
Use this when you need to create an indicator function object dynamically by name, or when using the object-oriented Abstract API interface.
Code Reference
Source Location
- Repository: ta-lib-python
- File: talib/abstract.py (L13-20), talib/_abstract.pxi (L88-128)
Signature
# Factory function in abstract.py
def Function(function_name: str, *args, **kwargs) -> _Function:
"""
Creates a Function object for the named TA-Lib indicator.
Args:
function_name: Indicator name (case-insensitive), e.g., 'sma', 'BBANDS'
*args: Optional input arrays and parameters
**kwargs: Optional named parameters
Returns:
_Function instance with bound C function
Raises:
Exception: If function_name is not supported by TA-Lib
"""
# Cython class in _abstract.pxi
class Function(object):
def __init__(self, function_name: str, func_object: callable, *args, **kwargs):
"""
Initialize with function name and C function binding.
Queries TA-Lib abstract interface for metadata.
"""
Import
from talib import abstract
# Or use pre-instantiated globals
from talib.abstract import Function, SMA, RSI, BBANDS
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| function_name | str | Yes | Indicator name (case-insensitive). One of 161 supported names. |
| *args | arrays/params | No | Optional input arrays and positional parameters |
| **kwargs | params | No | Optional named parameters (e.g., timeperiod=20) |
Outputs
| Name | Type | Description |
|---|---|---|
| function_object | _Function | Configured Function instance with .info, .parameters, .input_names properties |
Usage Examples
Basic Instantiation
from talib import abstract
# Create by name (case-insensitive)
sma = abstract.Function('sma')
bbands = abstract.Function('BBANDS')
# Inspect metadata
print(sma.info)
# {'name': 'SMA', 'group': 'Overlap Studies', 'input_names': ..., 'parameters': ..., 'output_names': ...}
Pre-instantiated Globals
from talib.abstract import SMA, RSI
# These are already Function instances
print(SMA.info)
print(RSI.parameters) # OrderedDict([('timeperiod', 14)])
Instantiation With Data
import numpy as np
from talib import abstract
inputs = {'close': np.random.random(100)}
result = abstract.Function('SMA', inputs, timeperiod=20)
# Creates function, sets inputs, sets params, and returns outputs