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

Install

install from PyPI:

1
$ python -m pip install TA-Lib

or

Max OS

1
$ brew install ta-lib

Abstract API Quick Start

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import numpy as np
from talib.abstract import *

# 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)
}

output = SMA(input_arrays, timeperiod=25) # calculate on close prices by default
output = SMA(input_arrays, timeperiod=25, price='open') # calculate on opens
upper, middle, lower = BBANDS(input_arrays, 20, 2, 2)
slowk, slowd = STOCH(input_arrays, 5, 3, 0, 3, 0) # uses high, low, close by default
slowk, slowd = STOCH(input_arrays, 5, 3, 0, 3, 0, prices=['high', 'low', 'open'])

TA-lib function help

In this case we’re looking for a hammer pattern.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
>>> import talib
>>> help(talib.CDLHAMMER)
Help on function CDLHAMMER in module talib._ta_lib:

CDLHAMMER(...)
    CDLHAMMER(open, high, low, close)
    
    Hammer (Pattern Recognition)
    
    Inputs:
        prices: ['open', 'high', 'low', 'close']
    Outputs:
        integer (values are -100, 0 or 100)

From experimentation, I’ve found that a value of 100 represents a bullish signal, -100 a bearish signal, and 0 neutral.

example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import talib
import numpy
import pandas

sample_data = [
  ['Mon', 21, 28, 39, 47],
  ['Tue', 31, 38, 40, 51],
  ['Wed', 51, 55, 57, 63],
  ['Thu', 78, 70, 71, 61],
  ['Fri', 69, 67, 21, 14],
 ]

sample_data = pandas.DataFrame(sample_data,
                               columns=["Day","Open","High","Low","Close"])

open = sample_data['Open']
high = sample_data['High']
low = sample_data['Low']
close = sample_data['Close']

talib.CDLHAMMER(open, high, low, close)

参考