README.md
1# TA-Lib
2
3[![](https://api.travis-ci.org/mrjbq7/ta-lib.svg)](https://travis-ci.org/mrjbq7/ta-lib)
4
5This is a Python wrapper for [TA-LIB](http://ta-lib.org) based on Cython
6instead of SWIG. From the homepage:
7
8> TA-Lib is widely used by trading software developers requiring to perform
9> technical analysis of financial market data.
10
11> * Includes 150+ indicators such as ADX, MACD, RSI, Stochastic, Bollinger
12> Bands, etc.
13> * Candlestick pattern recognition
14> * Open-source API for C/C++, Java, Perl, Python and 100% Managed .NET
15
16The original Python bindings included with TA-Lib use
17[SWIG](http://swig.org) which unfortunately are difficult to install and
18aren't as efficient as they could be. Therefore this project uses Cython and
19Numpy to efficiently and cleanly bind to TA-Lib -- producing results 2-4
20times faster than the SWIG interface.
21
22## Installation
23
24You can install from PyPI:
25
26```
27$ pip install TA-Lib
28```
29
30Or checkout the sources and run ``setup.py`` yourself:
31
32```
33$ python setup.py install
34```
35
36### Troubleshooting
37
38Sometimes installation will produce build errors like this:
39
40```
41func.c:256:28: fatal error: ta-lib/ta_libc.h: No such file or directory
42compilation terminated.
43```
44
45or:
46
47```
48common.obj : error LNK2001: unresolved external symbol TA_SetUnstablePeriod
49common.obj : error LNK2001: unresolved external symbol TA_Shutdown
50common.obj : error LNK2001: unresolved external symbol TA_Initialize
51common.obj : error LNK2001: unresolved external symbol TA_GetUnstablePeriod
52common.obj : error LNK2001: unresolved external symbol TA_GetVersionString
53```
54
55This typically means that it can't find the underlying ``TA-Lib`` library, a
56dependency which needs to be installed. On Windows, this could be caused by
57installing the 32-bit binary distribution of the underlying ``TA-Lib`` library,
58but trying to use it with 64-bit Python.
59
60Sometimes installation will fail with errors like this:
61
62```
63talib/common.c:8:22: fatal error: pyconfig.h: No such file or directory
64 #include "pyconfig.h"
65 ^
66compilation terminated.
67error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
68```
69
70This typically means that you need the Python headers, and should run
71something like:
72
73```
74$ sudo apt-get install python3-dev
75```
76
77### Dependencies
78
79To use TA-Lib for python, you need to have the
80[TA-Lib](http://ta-lib.org/hdr_dw.html) already installed. You should
81probably follow their installation directions for your platform, but some
82suggestions are included below for reference.
83
84##### Mac OS X
85
86```
87$ brew install ta-lib
88```
89
90##### Windows
91
92Download [ta-lib-0.4.0-msvc.zip](http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-msvc.zip)
93and unzip to ``C:\ta-lib``
94
95> This is a 32-bit release. If you want to use 64-bit Python, you will need
96> to build a 64-bit version of the library.
97
98> Some unofficial (and unsupported) instructions for building on 64-bit Windows 10, here for reference:
99
100> 1. Download and Unzip ``ta-lib-0.4.0-msvc.zip``
101
102> 2. Move the Unzipped Folder ``ta-lib`` to ``C:\``
103
104> 3. Download and Install Visual Studio Community 2015
105
106> * Remember to Select ``[Visual C++]`` Feature
107
108> 4. Build TA-Lib Library
109
110> * From Windows Start Menu, Start ``[VS2015 x64 Native Tools Command Prompt]``
111
112> * Move to ``C:\ta-lib\c\make\cdr\win32\msvc``
113
114> * Build the Library ``nmake``
115
116##### Linux
117
118Download [ta-lib-0.4.0-src.tar.gz](http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz) and:
119```
120$ untar and cd
121$ ./configure --prefix=/usr
122$ make
123$ sudo make install
124```
125
126> If you build ``TA-Lib`` using ``make -jX`` it will fail but that's OK!
127> Simply rerun ``make -jX`` followed by ``[sudo] make install``.
128
129## Function API
130
131Similar to TA-Lib, the Function API provides a lightweight wrapper of the
132exposed TA-Lib indicators.
133
134Each function returns an output array and have default values for their
135parameters, unless specified as keyword arguments. Typically, these functions
136will have an initial "lookback" period (a required number of observations
137before an output is generated) set to ``NaN``.
138
139For convenience, the Function API supports both ``numpy.ndarray`` and
140``pandas.Series`` inputs.
141
142All of the following examples use the Function API:
143
144```python
145import numpy
146import talib
147
148close = numpy.random.random(100)
149```
150
151Calculate a simple moving average of the close prices:
152
153```python
154output = talib.SMA(close)
155```
156
157Calculating bollinger bands, with triple exponential moving average:
158
159```python
160from talib import MA_Type
161
162upper, middle, lower = talib.BBANDS(close, matype=MA_Type.T3)
163```
164
165Calculating momentum of the close prices, with a time period of 5:
166
167```python
168output = talib.MOM(close, timeperiod=5)
169```
170
171## Abstract API
172
173If you're already familiar with using the function API, you should feel right
174at home using the Abstract API.
175
176Every function takes a collection of named inputs, either a ``dict`` of
177``numpy.ndarray`` or ``pandas.Series``, or a ``pandas.DataFrame``. If a
178``pandas.DataFrame`` is provided, the output is returned as a
179``pandas.DataFrame`` with named output columns.
180
181For example, inputs could be provided for the typical "OHLCV" data:
182
183```python
184import numpy as np
185
186# note that all ndarrays must be the same length!
187inputs = {
188 'open': np.random.random(100),
189 'high': np.random.random(100),
190 'low': np.random.random(100),
191 'close': np.random.random(100),
192 'volume': np.random.random(100)
193}
194```
195
196Functions can either be imported directly or instantiated by name:
197
198```python
199from talib import abstract
200
201# directly
202sma = abstract.SMA
203
204# or by name
205sma = abstract.Function('sma')
206```
207
208From there, calling functions is basically the same as the function API:
209
210```python
211from talib.abstract import *
212
213# uses close prices (default)
214output = SMA(inputs, timeperiod=25)
215
216# uses open prices
217output = SMA(inputs, timeperiod=25, price='open')
218
219# uses close prices (default)
220upper, middle, lower = BBANDS(inputs, 20, 2, 2)
221
222# uses high, low, close (default)
223slowk, slowd = STOCH(inputs, 5, 3, 0, 3, 0) # uses high, low, close by default
224
225# uses high, low, open instead
226slowk, slowd = STOCH(inputs, 5, 3, 0, 3, 0, prices=['high', 'low', 'open'])
227```
228
229## Supported Indicators and Functions
230
231We can show all the TA functions supported by TA-Lib, either as a ``list`` or
232as a ``dict`` sorted by group (e.g. "Overlap Studies", "Momentum Indicators",
233etc):
234
235```python
236import talib
237
238# list of functions
239print talib.get_functions()
240
241# dict of functions by group
242print talib.get_function_groups()
243```
244
245### Indicator Groups
246
247* Overlap Studies
248* Momentum Indicators
249* Volume Indicators
250* Volatility Indicators
251* Price Transform
252* Cycle Indicators
253* Pattern Recognition
254
255##### Overlap Studies
256```
257BBANDS Bollinger Bands
258DEMA Double Exponential Moving Average
259EMA Exponential Moving Average
260HT_TRENDLINE Hilbert Transform - Instantaneous Trendline
261KAMA Kaufman Adaptive Moving Average
262MA Moving average
263MAMA MESA Adaptive Moving Average
264MAVP Moving average with variable period
265MIDPOINT MidPoint over period
266MIDPRICE Midpoint Price over period
267SAR Parabolic SAR
268SAREXT Parabolic SAR - Extended
269SMA Simple Moving Average
270T3 Triple Exponential Moving Average (T3)
271TEMA Triple Exponential Moving Average
272TRIMA Triangular Moving Average
273WMA Weighted Moving Average
274```
275
276##### Momentum Indicators
277```
278ADX Average Directional Movement Index
279ADXR Average Directional Movement Index Rating
280APO Absolute Price Oscillator
281AROON Aroon
282AROONOSC Aroon Oscillator
283BOP Balance Of Power
284CCI Commodity Channel Index
285CMO Chande Momentum Oscillator
286DX Directional Movement Index
287MACD Moving Average Convergence/Divergence
288MACDEXT MACD with controllable MA type
289MACDFIX Moving Average Convergence/Divergence Fix 12/26
290MFI Money Flow Index
291MINUS_DI Minus Directional Indicator
292MINUS_DM Minus Directional Movement
293MOM Momentum
294PLUS_DI Plus Directional Indicator
295PLUS_DM Plus Directional Movement
296PPO Percentage Price Oscillator
297ROC Rate of change : ((price/prevPrice)-1)*100
298ROCP Rate of change Percentage: (price-prevPrice)/prevPrice
299ROCR Rate of change ratio: (price/prevPrice)
300ROCR100 Rate of change ratio 100 scale: (price/prevPrice)*100
301RSI Relative Strength Index
302STOCH Stochastic
303STOCHF Stochastic Fast
304STOCHRSI Stochastic Relative Strength Index
305TRIX 1-day Rate-Of-Change (ROC) of a Triple Smooth EMA
306ULTOSC Ultimate Oscillator
307WILLR Williams' %R
308```
309
310##### Volume Indicators
311```
312AD Chaikin A/D Line
313ADOSC Chaikin A/D Oscillator
314OBV On Balance Volume
315```
316
317##### Cycle Indicators
318```
319HT_DCPERIOD Hilbert Transform - Dominant Cycle Period
320HT_DCPHASE Hilbert Transform - Dominant Cycle Phase
321HT_PHASOR Hilbert Transform - Phasor Components
322HT_SINE Hilbert Transform - SineWave
323HT_TRENDMODE Hilbert Transform - Trend vs Cycle Mode
324```
325
326##### Price Transform
327```
328AVGPRICE Average Price
329MEDPRICE Median Price
330TYPPRICE Typical Price
331WCLPRICE Weighted Close Price
332```
333
334##### Volatility Indicators
335```
336ATR Average True Range
337NATR Normalized Average True Range
338TRANGE True Range
339```
340
341##### Pattern Recognition
342```
343CDL2CROWS Two Crows
344CDL3BLACKCROWS Three Black Crows
345CDL3INSIDE Three Inside Up/Down
346CDL3LINESTRIKE Three-Line Strike
347CDL3OUTSIDE Three Outside Up/Down
348CDL3STARSINSOUTH Three Stars In The South
349CDL3WHITESOLDIERS Three Advancing White Soldiers
350CDLABANDONEDBABY Abandoned Baby
351CDLADVANCEBLOCK Advance Block
352CDLBELTHOLD Belt-hold
353CDLBREAKAWAY Breakaway
354CDLCLOSINGMARUBOZU Closing Marubozu
355CDLCONCEALBABYSWALL Concealing Baby Swallow
356CDLCOUNTERATTACK Counterattack
357CDLDARKCLOUDCOVER Dark Cloud Cover
358CDLDOJI Doji
359CDLDOJISTAR Doji Star
360CDLDRAGONFLYDOJI Dragonfly Doji
361CDLENGULFING Engulfing Pattern
362CDLEVENINGDOJISTAR Evening Doji Star
363CDLEVENINGSTAR Evening Star
364CDLGAPSIDESIDEWHITE Up/Down-gap side-by-side white lines
365CDLGRAVESTONEDOJI Gravestone Doji
366CDLHAMMER Hammer
367CDLHANGINGMAN Hanging Man
368CDLHARAMI Harami Pattern
369CDLHARAMICROSS Harami Cross Pattern
370CDLHIGHWAVE High-Wave Candle
371CDLHIKKAKE Hikkake Pattern
372CDLHIKKAKEMOD Modified Hikkake Pattern
373CDLHOMINGPIGEON Homing Pigeon
374CDLIDENTICAL3CROWS Identical Three Crows
375CDLINNECK In-Neck Pattern
376CDLINVERTEDHAMMER Inverted Hammer
377CDLKICKING Kicking
378CDLKICKINGBYLENGTH Kicking - bull/bear determined by the longer marubozu
379CDLLADDERBOTTOM Ladder Bottom
380CDLLONGLEGGEDDOJI Long Legged Doji
381CDLLONGLINE Long Line Candle
382CDLMARUBOZU Marubozu
383CDLMATCHINGLOW Matching Low
384CDLMATHOLD Mat Hold
385CDLMORNINGDOJISTAR Morning Doji Star
386CDLMORNINGSTAR Morning Star
387CDLONNECK On-Neck Pattern
388CDLPIERCING Piercing Pattern
389CDLRICKSHAWMAN Rickshaw Man
390CDLRISEFALL3METHODS Rising/Falling Three Methods
391CDLSEPARATINGLINES Separating Lines
392CDLSHOOTINGSTAR Shooting Star
393CDLSHORTLINE Short Line Candle
394CDLSPINNINGTOP Spinning Top
395CDLSTALLEDPATTERN Stalled Pattern
396CDLSTICKSANDWICH Stick Sandwich
397CDLTAKURI Takuri (Dragonfly Doji with very long lower shadow)
398CDLTASUKIGAP Tasuki Gap
399CDLTHRUSTING Thrusting Pattern
400CDLTRISTAR Tristar Pattern
401CDLUNIQUE3RIVER Unique 3 River
402CDLUPSIDEGAP2CROWS Upside Gap Two Crows
403CDLXSIDEGAP3METHODS Upside/Downside Gap Three Methods
404```
405