TA-Lib Python Wrapper

repository·master·Indexed 11 days ago

https://github.com/ta-lib/ta-lib-python

A high-performance Python wrapper for the TA-Lib C library, providing over 150 technical analysis indicators and candlestick pattern recognition for financial market data. It supports numpy, pandas, and polars, and offers three distinct APIs: a Function API for direct calls, an Abstract API for named inputs and DataFrames, and an experimental Streaming API for real-time data. Version 0.7.1 requires the underlying TA-Lib C library to be installed.

Tokens
12.8K
Snippets
74
Records
84
Agent score
95%

What's inside TA-Lib

  1. Overview of TA-Lib Python

    master

    TA-Lib is a Python wrapper for the TA-Lib C library, implemented using Cython and NumPy instead of SWIG. It is designed for high-performance technical analysis of financial market data.

    Key features include:

    • Over 150 technical indicators (e.g., ADX, MACD, RSI, Stochastic, Bollinger Bands).
    • Candlestick pattern recognition.
    • High efficiency: Results are typically 2-4 times faster than the original SWIG-based interface.
    • Support for numpy, pandas, and polars libraries.
  2. Get started with TA-Lib Python documentation

    master

    The TA-Lib Python documentation is organized into several key areas to help you integrate technical analysis indicators into your workflow:

    • Setup: Follow the Installation and Troubleshooting guide to get the library running.
    • API Usage: Choose between the Function API for direct indicator calls or the Abstract API for a different interface style.
    • Indicator Reference: A complete list of all available functions is provided in All Functions, categorized by indicator type (e.g., Momentum, Volatility, Pattern Recognition).
  3. Explore all supported TA-Lib indicators and functions

    master

    TA-Lib provides a comprehensive suite of technical analysis functions categorized into several functional groups. You can use these to perform overlap studies, momentum analysis, volume analysis, volatility measurement, pattern recognition, and various mathematical transformations on financial time series data.

    Supported functional groups include:

    • Overlap Studies: Moving averages (SMA, EMA, WMA, etc.) and Bollinger Bands.
    • Momentum Indicators: RSI, MACD, Stochastic, ADX, and Rate of Change (ROC).
    • Volume Indicators: OBV, Chaikin A/D, and ADOSC.
    • Volatility Indicators: ATR, NATR, and TRANGE.
    • Cycle Indicators: Hilbert Transform components (DCPERIOD, SINE, etc.).
    • Pattern Recognition: Candlestick pattern detection (e.g., Doji, Hammer, Engulfing).
    • Price Transform: Calculations like Average, Median, and Typical Price.
    • Statistic Functions: Beta, Correlation, Linear Regression, and Standard Deviation.
    • Math Transform: Vector trigonometric and arithmetic functions (SIN, COS, EXP, etc.).
    • Math Operators: Vector arithmetic (ADD, SUB, MULT, DIV) and extremum functions (MAX, MIN, SUM).
  4. Use Price Transform Functions in TA-Lib

    master

    Price Transform Functions are used to calculate new price points based on existing price data (Open, High, Low, Close). These functions transform raw price inputs into single values representing a specific price metric.

    # Example of using price transform functions
    real = AVGPRICE(open, high, low, close)
    real = MEDPRICE(high, low)
  5. Understand the difference between STOCHRSI and STOCH

    master

    Users often expect STOCHRSI to behave like Stochastic applied to RSI, but STOCHRSI is actually STOCHF (Fast Stochastic) applied to RSI. If you want the standard Stochastic applied to RSI, use STOCH instead.

    import talib
    import numpy as np
    c = np.random.randn(100)
    
    # Standard library function (Fast Stochastic applied to RSI)
    k, d = talib.STOCHRSI(c)
    
    # Equivalent to STOCHRSI
    rsi = talib.RSI(c)
    k, d = talib.STOCHF(rsi, rsi, rsi)
    
    # What you likely actually want (Stochastic applied to RSI)
    rsi = talib.RSI(c)
    k, d = talib.STOCH(rsi, rsi, rsi)
    import talib
    import numpy as np
    c = np.random.randn(100)
    
    # this is the library function
    k, d = talib.STOCHRSI(c)
    
    # this produces the same result, calling STOCHF
    rsi = talib.RSI(c)
    k, d = talib.STOCHF(rsi, rsi, rsi)
    
    # you might want this instead, calling STOCH
    rsi = talib.RSI(c)
    k, d = talib.STOCH(rsi, rsi, rsi)
  6. Manage unstable periods in TA-Lib functions

    master

    Functions documented as having an unstable period start with an unstable-period setting of 0. To track these periods, you must explicitly set them using set_unstable_period(). Once set, get_unstable_period() will return a non-zero value.

    # Example pattern for handling unstable periods
    talib.set_unstable_period(0) # Set explicitly
    period = talib.get_unstable_period()
  7. How to inspect TA-Lib function metadata via the Abstract API

    master

    Every function in the Abstract API has an .info property that provides a dictionary containing metadata about the indicator. This is useful for discovering required input names, available parameters, and expected output names.

    Metadata includes:

    • name: The technical name of the indicator.
    • display_name: The human-readable name.
    • group: The category of the indicator (e.g., 'Momentum Indicators').
    • input_names: An OrderedDict of the required input keys and their default values.
    • parameters: An OrderedDict of the function's parameters and their default values.
    • output_names: A list of the names of the returned arrays.
    from talib import abstract
    
    # Access metadata for the Stochastic function
    print(abstract.Function('stoch').info)
  8. Handle NaN propagation in TA-Lib

    master

    The underlying TA-Lib C library handles NaN values by typically propagating them to the end of the output array. This behavior differs from libraries like Pandas, which might output NaN only until the lookback period is satisfied.

    Example of TA-Lib behavior: If an input array contains a NaN, the subsequent values in the output may also become NaN depending on the indicator's calculation logic.

    >>> c = np.array([1.0, 2.0, 3.0, np.nan, 4.0, 5.0, 6.0])
    >>> talib.SMA(c, 3)
    array([nan, nan,  2., nan, nan, nan, nan])
  9. Extend the Abstract API by subclassing abstract.Function

    master
    For advanced use cases, such as using pandas DataFrames instead of NumPy arrays, you can subclass abstract.Function and override the set_input_arrays method to customize how the function accepts and processes input data.
  10. Version Compatibility Matrix

    master

    Because the upstream TA-Lib C library changed its library name from -lta_lib to -lta-lib in version 0.6.1, this project maintains three distinct feature branches to ensure compatibility with specific versions of the C library and NumPy:

    ta-lib-python versionTA-Lib C library versionNumPy version
    0.4.x0.4.x1
    0.5.x0.4.x2
    0.6.x0.6.x2