TA-Lib Function & Abstract API 2018-09-26

Function API Examples

Similar to TA-Lib, the function interface 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.

All of the following examples use the function API:

import numpy
import talib

close = numpy.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)

Abstract API Quick Start

If you're already familiar with using the function API, you should feel right at home using the abstract API. Every function takes the same input, passed as a dictionary of Numpy arrays:

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
sma = abstract.SMA
sma = abstract.Function('sma')

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

from talib.abstract import *
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'])

Advanced Usage

For more advanced use cases of TA-Lib, the Abstract API also offers much more flexibility. You can even subclass abstract.Function and override set_input_arrays to customize the type of input data Function accepts (e.g. a pandas DataFrame).

Details about every function can be accessed via the info property:

print Function('stoch').info
{
  'name': 'STOCH',
  'display_name': 'Stochastic',
  'group': 'Momentum Indicators',
  'input_names': OrderedDict([
    ('prices', ['high', 'low', 'close']),
  ]),
  'parameters': OrderedDict([
    ('fastk_period', 5),
    ('slowk_period', 3),
    ('slowk_matype', 0),
    ('slowd_period', 3),
    ('slowd_matype', 0),
  ]),
  'output_names': ['slowk', 'slowd'],
}

Or in human-readable format:

help(STOCH)
str(STOCH)

Other useful properties of Function:

Function('x').function_flags
Function('x').input_names
Function('x').input_arrays
Function('x').parameters
Function('x').lookback
Function('x').output_names
Function('x').output_flags
Function('x').outputs

Aside from calling the function directly, Functions maintain state and will remember their parameters/input_arrays after they've been set. You can set parameters and recalculate with new input data using run():

SMA.parameters = {'timeperiod': 15}
result1 = SMA.run(input_arrays1)
result2 = SMA.run(input_arrays2)

# Or set input_arrays and change the parameters:

SMA.input_arrays = input_arrays1
ma10 = SMA(timeperiod=10)
ma20 = SMA(20)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末怜奖,一起剝皮案震驚了整個(gè)濱河市崇裁,隨后出現(xiàn)的幾起案子是钥,更是在濱河造成了極大的恐慌,老刑警劉巖桦卒,帶你破解...
    沈念sama閱讀 206,378評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡元潘,警方通過查閱死者的電腦和手機(jī)人柿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,356評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門柴墩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人顷扩,你說(shuō)我怎么就攤上這事拐邪。” “怎么了隘截?”我有些...
    開封第一講書人閱讀 152,702評(píng)論 0 342
  • 文/不壞的土叔 我叫張陵扎阶,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我婶芭,道長(zhǎng)东臀,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,259評(píng)論 1 279
  • 正文 為了忘掉前任犀农,我火速辦了婚禮惰赋,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘呵哨。我一直安慰自己赁濒,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,263評(píng)論 5 371
  • 文/花漫 我一把揭開白布孟害。 她就那樣靜靜地躺著拒炎,像睡著了一般。 火紅的嫁衣襯著肌膚如雪挨务。 梳的紋絲不亂的頭發(fā)上击你,一...
    開封第一講書人閱讀 49,036評(píng)論 1 285
  • 那天玉组,我揣著相機(jī)與錄音,去河邊找鬼丁侄。 笑死惯雳,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的鸿摇。 我是一名探鬼主播石景,決...
    沈念sama閱讀 38,349評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼户辱!你這毒婦竟也來(lái)了鸵钝?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 36,979評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤庐镐,失蹤者是張志新(化名)和其女友劉穎恩商,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體必逆,經(jīng)...
    沈念sama閱讀 43,469評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡怠堪,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,938評(píng)論 2 323
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了名眉。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片粟矿。...
    茶點(diǎn)故事閱讀 38,059評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖损拢,靈堂內(nèi)的尸體忽然破棺而出陌粹,到底是詐尸還是另有隱情,我是刑警寧澤福压,帶...
    沈念sama閱讀 33,703評(píng)論 4 323
  • 正文 年R本政府宣布掏秩,位于F島的核電站,受9級(jí)特大地震影響荆姆,放射性物質(zhì)發(fā)生泄漏蒙幻。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,257評(píng)論 3 307
  • 文/蒙蒙 一胆筒、第九天 我趴在偏房一處隱蔽的房頂上張望邮破。 院中可真熱鬧,春花似錦仆救、人聲如沸抒和。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,262評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)构诚。三九已至,卻和暖如春铆惑,著一層夾襖步出監(jiān)牢的瞬間范嘱,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,485評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工员魏, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留丑蛤,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,501評(píng)論 2 354
  • 正文 我出身青樓撕阎,卻偏偏與公主長(zhǎng)得像受裹,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子虏束,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,792評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容