TA-Lib tutorial
1、TA-Lib Introduction
TA-Lib, The full English name is "Technical Analysis Library", is a third-party library used for financial quantification, covering more than 150 commonly used technical analysis indicators in stock and futures trading software, such as MA、MACD、RSI、KDJ、Momentum indicators, Bollinger Bands, and so on.
TA-Lib can be divided into 10 sub sections: Overlap Studies, Momentum Indicators, Volume Indicators, Cycle Indicators, Price Transform, Volatility Indicators, Pattern Recognition, Statistic Functions, Math Transform, and Math Operators.
TA-Lib official website address: https://www.ta-lib.org/. Note: The official website has not been updated for a long time.
When using Python for quantitative analysis, TA-Lib is almost a necessary step.
2、TA-Lib Setup
Install using local file Wheel (recommended)
Christoph Gohlke from the Fluorescence Dynamics Laboratory at the University of California, Irvine has provided an unofficial Python extension library at
https://github.com/cgohlke/talib-build/releases
Select and download the corresponding libraries for your computer (32-bit or 64 bit) and Python version to your local device
- TA_Lib-0.4.32-cp310-cp310-win32.whl
- TA\_Lib-0.4.32-cp310-cp310-win\_amd64.whl
- TA_Lib-0.4.32-cp311-cp311-win32.whl
- TA\_Lib-0.4.32-cp311-cp311-win\_amd64.whl
- TA\_Lib-0.4.32-cp311-cp311-win\_arm64.whl
- TA_Lib-0.4.32-cp312-cp312-win32.whl
- TA\_Lib-0.4.32-cp312-cp312-win\_amd64.whl
- TA\_Lib-0.4.32-cp312-cp312-win\_arm64.whl
- TA_Lib-0.4.32-cp313-cp313-win32.whl
- TA\_Lib-0.4.32-cp313-cp313-win\_amd64.whl
- TA\_Lib-0.4.32-cp313-cp313-win\_arm64.whl
- TA_Lib-0.4.32-cp37-cp37m-win32.whl
- TA\_Lib-0.4.32-cp37-cp37m-win\_amd64.whl
- TA_Lib-0.4.32-cp38-cp38-win32.whl
- TA\_Lib-0.4.32-cp38-cp38-win\_amd64.whl
- TA_Lib-0.4.32-cp39-cp39-win32.whl
- TA\_Lib-0.4.32-cp39-cp39-win\_amd64.whl
Use the following command again, in the format of
pip install TA_Lib-0.4.20-cp38-cp38-win_amd64.whl
Among them, TA_Lib-0.4.20-cp38-cp38-win-amd64.whl needs to be changed to the file name downloaded by oneself
The installation process is as follows
It can be successfully installed.
In Python, if you enter import talib and there are no error returns, it means you can use it.
Install online using pip
Enter the following command at the prompt
$ pip install TA-Lib
But errors may occur:
common.obj : warning LNK4197: export 'PyInit_common' specified multiple times; using first specification
Creating library build\temp.win-amd64-3.5\Release\talib\common.cp35-win_amd64.lib and
object build\temp.win-amd64-3.5\Release\talib\common.cp35-win_amd64.exp
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
build\lib.win-amd64-3.5\talib\common.cp35-win_amd64.pyd : fatal error LNK1120: 5 unresolved externals
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\amd64\\link.exe' failed with exit status 1120
This problem can be solved in the following ways.
Start ->All Programs ->Mircosoft Visual Studio 2010 -> visual studio tools -> Visual Studio x64 win64 command prompt (2010)
Under the VS x64 win64 command line, in the directory cd C:\ta-lib\c\make\cdr\win32\msvc, execute nmake
You can also consider using the following command to install
pip install https://github.com/mrjbq7/ta-lib/archive/TA_Lib-0.4.8.zip
3、TA-Lib Using
MA
Firstly, financial data needs to be obtained, using the K-line of the BTC_USDT daily chart from Binance Exchange as the data source
# -*- coding: utf-8 -*-
import talib
from udcei.base.exchange import Exchange
exchange = Exchange('binance')
kline = exchange.kline(symbol = "BTC_USDT", interval = '1day')
stamp, open, high, low, close, volume = kline
#print(close)
Print out the closing price as follows
Then input the closing price into the MA indicator of the moving average for calculation. The code for calculating the 5-day moving average is as follows:,
real = talib.MA(close, timeperiod=5)
Get a list of 5-day moving average prices
The first 4 days were classified as NAN due to insufficient data.
Based on the moving average data, further calculations can be made, such as golden crosses, death crosses, long positions, short positions, etc. This achieves quantitative analysis of the data.
Other indicators
The other indicators of TA Lib are listed below,
Cycle Indicators
[‘HT_DCPERIOD’, ‘HT_DCPHASE’, ‘HT_PHASOR’, ‘HT_SINE’, ‘HT_TRENDMODE’],
Math Operators
[‘ADD’, ‘DIV’, ‘MAX’, ‘MAXINDEX’, ‘MIN’, ‘MININDEX’, ‘MINMAX’, ‘MINMAXINDEX’, ‘MULT’, ‘SUB’, ‘SUM’],
Math Transform
[‘ACOS’, ‘ASIN’, ‘ATAN’, ‘CEIL’, ‘COS’, ‘COSH’, ‘EXP’, ‘FLOOR’, ‘LN’, ‘LOG10’, ‘SIN’, ‘SINH’, ‘SQRT’, ‘TAN’, ‘TANH’],
Momentum Indicators
[‘ADX’, ‘ADXR’, ‘APO’, ‘AROON’, ‘AROONOSC’, ‘BOP’, ‘CCI’, ‘CMO’, ‘DX’, ‘MACD’, ‘MACDEXT’, ‘MACDFIX’, ‘MFI’, ‘MINUS_DI’, ‘MINUS_DM’, ‘MOM’, ‘PLUS_DI’, ‘PLUS_DM’, ‘PPO’, ‘ROC’, ‘ROCP’, ‘ROCR’, ‘ROCR100’, ‘RSI’, ‘STOCH’, ‘STOCHF’, ‘STOCHRSI’, ‘TRIX’, ‘ULTOSC’, ‘WILLR’],
Overlap Studies
[‘BBANDS’, ‘DEMA’, ‘EMA’, ‘HT_TRENDLINE’, ‘KAMA’, ‘MA’, ‘MAMA’, ‘MAVP’, ‘MIDPOINT’, ‘MIDPRICE’, ‘SAR’, ‘SAREXT’, ‘SMA’, ‘T3’, ‘TEMA’, ‘TRIMA’, ‘WMA’],
Pattern Recognition
[‘CDL2CROWS’, ‘CDL3BLACKCROWS’, ‘CDL3INSIDE’, ‘CDL3LINESTRIKE’, ‘CDL3OUTSIDE’, ‘CDL3STARSINSOUTH’, ‘CDL3WHITESOLDIERS’, ‘CDLABANDONEDBABY’, ‘CDLADVANCEBLOCK’, ‘CDLBELTHOLD’, ‘CDLBREAKAWAY’, ‘CDLCLOSINGMARUBOZU’, ‘CDLCONCEALBABYSWALL’, ‘CDLCOUNTERATTACK’, ‘CDLDARKCLOUDCOVER’, ‘CDLDOJI’, ‘CDLDOJISTAR’, ‘CDLDRAGONFLYDOJI’, ‘CDLENGULFING’, ‘CDLEVENINGDOJISTAR’, ‘CDLEVENINGSTAR’, ‘CDLGAPSIDESIDEWHITE’, ‘CDLGRAVESTONEDOJI’, ‘CDLHAMMER’, ‘CDLHANGINGMAN’, ‘CDLHARAMI’, ‘CDLHARAMICROSS’, ‘CDLHIGHWAVE’, ‘CDLHIKKAKE’, ‘CDLHIKKAKEMOD’, ‘CDLHOMINGPIGEON’, ‘CDLIDENTICAL3CROWS’, ‘CDLINNECK’, ‘CDLINVERTEDHAMMER’, ‘CDLKICKING’, ‘CDLKICKINGBYLENGTH’, ‘CDLLADDERBOTTOM’, ‘CDLLONGLEGGEDDOJI’, ‘CDLLONGLINE’, ‘CDLMARUBOZU’, ‘CDLMATCHINGLOW’, ‘CDLMATHOLD’, ‘CDLMORNINGDOJISTAR’, ‘CDLMORNINGSTAR’, ‘CDLONNECK’, ‘CDLPIERCING’, ‘CDLRICKSHAWMAN’, ‘CDLRISEFALL3METHODS’, ‘CDLSEPARATINGLINES’, ‘CDLSHOOTINGSTAR’, ‘CDLSHORTLINE’, ‘CDLSPINNINGTOP’, ‘CDLSTALLEDPATTERN’, ‘CDLSTICKSANDWICH’, ‘CDLTAKURI’, ‘CDLTASUKIGAP’, ‘CDLTHRUSTING’, ‘CDLTRISTAR’, ‘CDLUNIQUE3RIVER’, ‘CDLUPSIDEGAP2CROWS’, ‘CDLXSIDEGAP3METHODS’],
Price Transform
[‘AVGPRICE’, ‘MEDPRICE’, ‘TYPPRICE’, ‘WCLPRICE’],
Statistic Functions
[‘BETA’, ‘CORREL’, ‘LINEARREG’, ‘LINEARREG_ANGLE’, ‘LINEARREG_INTERCEPT’, ‘LINEARREG_SLOPE’, ‘STDDEV’, ‘TSF’, ‘VAR’],
Volatility Indicators
[‘ATR’, ‘NATR’, ‘TRANGE’],
Volume Indicators
[‘AD’, ‘ADOSC’, ‘OBV’]