Skip to content

TA-Lib/ta-lib-python

Repository files navigation

TA-Lib

Tests

This is a Python wrapper for TA-LIB based on Cython instead of SWIG. From the homepage:

TA-Lib is widely used by trading software developers requiring to perform technical analysis of financial market data.

  • Includes 150+ indicators such as ADX, MACD, RSI, Stochastic, Bollinger Bands, etc.
  • Candlestick pattern recognition
  • Open-source API for C/C++, Java, Perl, Python and 100% Managed .NET

The original Python bindings included with TA-Lib use SWIG which unfortunately are difficult to install and aren't as efficient as they could be. Therefore this project uses Cython and Numpy to efficiently and cleanly bind to TA-Lib -- producing results 2-4 times faster than the SWIG interface.

In addition, this project also supports the use of the Polars and Pandas libraries.

Installation

You can install from PyPI:

$ python -m pip install TA-Lib

Or checkout the sources and run setup.py yourself:

$ python setup.py install

It also appears possible to install via Conda Forge:

$ conda install -c conda-forge ta-lib

Dependencies

To use TA-Lib for python, you need to have the TA-Lib already installed. You should probably follow their installation directions for your platform, but some suggestions are included below for reference.

Some Conda Forge users have reported success installing the underlying TA-Lib C library using the libta-lib package:

$ conda install -c conda-forge libta-lib

Mac OS X

You can simply install using Homebrew:

$ brew install ta-lib

If you are using Apple Silicon, such as the M1 processors, and building mixed architecture Homebrew projects, you might want to make sure it's being built for your architecture:

$ arch -arm64 brew install ta-lib

And perhaps you can set these before installing with pip:

$ export TA_INCLUDE_PATH="$(brew --prefix ta-lib)/include"
$ export TA_LIBRARY_PATH="$(brew --prefix ta-lib)/lib"

You might also find this helpful, particularly if you have tried several different installations without success:

$ your-arm64-python -m pip install --no-cache-dir ta-lib
Windows

Download ta-lib-0.4.0-msvc.zip and unzip to C:\ta-lib.

This is a 32-bit binary release. If you want to use 64-bit Python, you will need to build a 64-bit version of the library. Some unofficial instructions for building on 64-bit Windows 10 or Windows 11, here for reference:

  1. Download and Unzip ta-lib-0.4.0-msvc.zip
  2. Move the Unzipped Folder ta-lib to C:\
  3. Download and Install Visual Studio Community (2015 or later)
    • Remember to Select [Visual C++] Feature
  4. Build TA-Lib Library
    • From Windows Start Menu, Start [VS2015 x64 Native Tools Command Prompt]
    • Move to C:\ta-lib\c\make\cdr\win32\msvc
    • Build the Library nmake

You might also try these unofficial windows binary wheels for both 32-bit and 64-bit:

https://github.com/cgohlke/talib-build/

Linux

Download ta-lib-0.4.0-src.tar.gz and:

$ tar -xzf ta-lib-0.4.0-src.tar.gz
$ cd ta-lib/
$ ./configure --prefix=/usr
$ make
$ sudo make install

If you build TA-Lib using make -jX it will fail but that's OK! Simply rerun make -jX followed by [sudo] make install.

Note: if your directory path includes spaces, the installation will probably fail with No such file or directory errors.

Troubleshooting

If you get a warning that looks like this:

setup.py:79: UserWarning: Cannot find ta-lib library, installation may fail.
warnings.warn('Cannot find ta-lib library, installation may fail.')

This typically means setup.py can't find the underlying TA-Lib library, a dependency which needs to be installed.


If you installed the underlying TA-Lib library with a custom prefix (e.g., with ./configure --prefix=$PREFIX), then when you go to install this python wrapper you can specify additional search paths to find the library and include files for the underlying TA-Lib library using the TA_LIBRARY_PATH and TA_INCLUDE_PATH environment variables:

$ export TA_LIBRARY_PATH=$PREFIX/lib
$ export TA_INCLUDE_PATH=$PREFIX/include
$ python setup.py install # or pip install ta-lib

Sometimes installation will produce build errors like this:

talib/_ta_lib.c:601:10: fatal error: ta-lib/ta_defs.h: No such file or directory
  601 | #include "ta-lib/ta_defs.h"
      |          ^~~~~~~~~~~~~~~~~~
compilation terminated.

or:

common.obj : error LNK2001: unresolved external symbol TA_SetUnstablePeriod
common.obj : error LNK2001: unresolved external symbol TA_Shutdown
common.obj : error LNK2001: unresolved external symbol TA_Initialize
common.obj : error LNK2001: unresolved external symbol TA_GetUnstablePeriod
common.obj : error LNK2001: unresolved external symbol TA_GetVersionString

This typically means that it can't find the underlying TA-Lib library, a dependency which needs to be installed. On Windows, this could be caused by installing the 32-bit binary distribution of the underlying TA-Lib library, but trying to use it with 64-bit Python.


Sometimes installation will fail with errors like this:

talib/common.c:8:22: fatal error: pyconfig.h: No such file or directory
 #include "pyconfig.h"
                      ^
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

This typically means that you need the Python headers, and should run something like:

$ sudo apt-get install python3-dev

Sometimes building the underlying TA-Lib library has errors running make that look like this:

../libtool: line 1717: cd: .libs/libta_lib.lax/libta_abstract.a: No such file or directory
make[2]: *** [libta_lib.la] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all-recursive] Error 1

This might mean that the directory path to the underlying TA-Lib library has spaces in the directory names. Try putting it in a path that does not have any spaces and trying again.


Sometimes you might get this error running setup.py:

/usr/include/limits.h:26:10: fatal error: bits/libc-header-start.h: No such file or directory
#include <bits/libc-header-start.h>
         ^~~~~~~~~~~~~~~~~~~~~~~~~~

This is likely an issue with trying to compile for 32-bit platform but without the appropriate headers. You might find some success looking at the first answer to this question.


If you get an error on macOS like this:

code signature in <141BC883-189B-322C-AE90-CBF6B5206F67>
'python3.9/site-packages/talib/_ta_lib.cpython-39-darwin.so' not valid for
use in process: Trying to load an unsigned library)

You might look at this question and use xcrun codesign to fix it.


If you wonder why STOCHRSI gives you different results than you expect, probably you want STOCH applied to RSI, which is a little different than the STOCHRSI which is STOCHF applied to 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)

If the build appears to hang, you might be running on a VM with not enough memory -- try 1 GB or 2 GB.


If you get "permission denied" errors such as this, you might need to give your user access to the location where the underlying TA-Lib C library is installed -- or install it to a user-accessible location.

talib/_ta_lib.c:747:28: fatal error: /usr/include/ta-lib/ta_defs.h: Permission denied
 #include "ta-lib/ta-defs.h"
                            ^
compilation terminated
error: command 'gcc' failed with exit status 1

If you're having trouble compiling the underlying TA-Lib C library on ARM64, you might need to configure it with an explicit build type before running make and make install, for example:

$ ./configure --build=aarch64-unknown-linux-gnu

This is caused by old config.guess file, so another way to solve this is to copy a newer version of config.guess into the underlying TA-Lib C library sources:

$ cp /usr/share/automake-1.16/config.guess /path/to/extracted/ta-lib/config.guess

And then re-run configure:

$ ./configure

If you're having trouble using PyInstaller and get an error that looks like this:

...site-packages\PyInstaller\loader\pyimod03_importers.py", line 493, in exec_module
    exec(bytecode, module.__dict__)
  File "talib\__init__.py", line 72, in <module>
ModuleNotFoundError: No module named 'talib.stream'

Then, perhaps you can use the --hidden-import argument to fix this:

$ pyinstaller --hidden-import talib.stream "replaceToYourFileName.py"

Function API

Similar to TA-Lib, the Function API provides a lightweight wrapper of the exposed TA-Lib indicators.

Each function returns an output array and have default values for their parameters, unless specified as keyword arguments. Typically, these functions will have an initial "lookback" period (a required number of observations before an output is generated) set to NaN.

For convenience, the Function API supports both numpy.ndarray and pandas.Series and polars.Series inputs.

All of the following examples use the Function API:

import numpy as np
import talib

close = np.random.random(100)

Calculate a simple moving average of the close prices:

output = talib.SMA(close)

Calculating bollinger bands, with triple exponential moving average:

from talib import MA_Type

upper, middle, lower = talib.BBANDS(close, matype=MA_Type.T3)

Calculating momentum of the close prices, with a time period of 5:

output = talib.MOM(close, timeperiod=5)
NaN's

The underlying TA-Lib C library handles NaN's in a sometimes surprising manner by typically propagating NaN's to the end of the output, for example:

>>> 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])

You can compare that to a Pandas rolling mean, where their approach is to output NaN until enough "lookback" values are observed to generate new outputs:

>>> c = pandas.Series([1.0, 2.0, 3.0, np.nan, 4.0, 5.0, 6.0])

>>> c.rolling(3).mean()
0    NaN
1    NaN
2    2.0
3    NaN
4    NaN
5    NaN
6    5.0
dtype: float64

Abstract API

If you're already familiar with using the function API, you should feel right at home using the Abstract API.

Every function takes a collection of named inputs, either a dict of numpy.ndarray or pandas.Series or polars.Series, or a pandas.DataFrame or polars.DataFrame. If a pandas.DataFrame or polars.DataFrame is provided, the output is returned as the same type with named output columns.

For example, inputs could be provided for the typical "OHLCV" data:

import numpy as np

# note that all ndarrays must be the same length!
inputs = {
    'open': np.random.random(100),
    'high': np.random.random(100),
    'low': np.random.random(100),
    'close': np.random.random(100),
    'volume': np.random.random(100)
}

Functions can either be imported directly or instantiated by name:

from talib import abstract

# directly
SMA = abstract.SMA

# or by name
SMA = abstract.Function('sma')

From there, calling functions is basically the same as the function API:

from talib.abstract import *

# uses close prices (default)
output = SMA(inputs, timeperiod=25)

# uses open prices
output = SMA(inputs, timeperiod=25, price='open')

# uses close prices (default)
upper, middle, lower = BBANDS(inputs, 20, 2.0, 2.0)

# uses high, low, close (default)
slowk, slowd = STOCH(inputs, 5, 3, 0, 3, 0) # uses high, low, close by default

# uses high, low, open instead
slowk, slowd = STOCH(inputs, 5, 3, 0, 3, 0, prices=['high', 'low', 'open'])

Streaming API

An experimental Streaming API was added that allows users to compute the latest value of an indicator. This can be faster than using the Function API, for example in an application that receives streaming data, and wants to know just the most recent updated indicator value.

import talib
from talib import stream

close = np.random.random(100)

# the Function API
output = talib.SMA(close)

# the Streaming API