TaLib官網:http://ta-lib.org/
Python wrapper for TA-Lib:http://mrjbq7.github.io/ta-lib/doc_index.html
一、TaLib簡介
TaLib是一個Python金融指數處理庫。包含了很多技術分析里的常用參數指標,例如MA、SMA、WMA、MACD、ATR等。
二、TaLib安裝
注:TaLib是一個pyhon庫,故在安裝TaLib前需要安裝Python。
1. pip安裝
(官方安裝教程文檔:https://mrjbq7.github.io/ta-lib/install.html )
- 在命令行下輸入命令(在線安裝):
pip install Ta-Lib
- (注意:使用pip安裝可能會出現這個問題:
error: Microsoft Visual C++ 14.0 is required. Get it with “Microsoft Visual C++ Build Tools”: https://visualstudio.microsoft.com/downloads/
此時就需要手動安裝。這個問題在安裝其他python第三方包時也可能出現,解決方法與此相同)
2. 手動安裝
A. 找到並下載自己需要的對應版本的whl格式文件。
(如:若本機安裝是32位的python3.6,則選TA_Lib‑0.4.17‑cp36‑cp36m‑win32.whl下載;
若本機安裝的是64位的python2.7,則選擇TA_Lib‑0.4.17‑cp27‑cp27m‑win_amd64.whl,其他同理)
下載地址在:
https://www.lfd.uci.edu/~gohlke/pythonlibs/
B. 在命令行中(命令行的打開方式在文章最后的Tips里),進入此whl文件所在的目錄,執行命令:
pip install 下載的whl文件名
如:pip install TA_Lib-0.4.17-cp37-cp37m-win_amd64.whl
C. 或不進入所在目錄,直接執行命令:
pip install 文件所在的絕對路徑+文件名
如:pip install D:\my_packages\ TA_Lib-0.4.17-cp37-cp37m-win_amd64.whl
3. 檢測安裝成功
在包安裝成功后,命令行界面會有“ Successfully installed TA-Lib ”的提示。
也可通過在命令行輸入命令“ pip install ta-lib ”進行檢測,如果出現“ Requirement already satisfied: ta-lib in …”(”…“為安裝此文件的目錄),即表示安裝成功。
三、TaLib函數介紹
1. 幾個指標及其用法演示
注意:talib函數的輸入數據需要是Numpy的ndarray 類型,如果數據不是這個類型就需要提前進行類型轉換。(“Every function takes the same input, passed as a dictionary of Numpy arrays”)。
在使用tqsdk時,K線及tick序列可以使用to_dataframe()函數將數據序列轉化為pandas.DataFrame,即可直接取用其close字段作為talib函數的輸入數據了(例如:df_klines=klines.to_dataframe())。
- SMA (簡單移動平均線)(參數1:收盤價序列,參數2:時間周期(均線的計算長度 即 幾日均線))
real = SMA(close, timeperiod=30)
例:雙均線策略中使用SMA的實例:
https://github.com/shinnytech/tqsdk-python/blob/master/tqsdk/demo/doublema.py
- ATR(平均真實波幅)
(參數1:最高價序列,參數2:最低價序列,參數3:收盤價序列,參數4:時間周期)
real = ATR(high, low, close, timeperiod=14)
例:海龜策略中使用ATR的實例:
https://github.com/shinnytech/tqsdk-python/blob/master/tqsdk/demo/turtle.py
- MACD(異同移動平均線)
macd, macdsignal, macdhist = MACD(close, fastperiod=12, slowperiod=26, signalperiod=9)
- BBANDS (布林帶)
upperband, middleband, lowerband = BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)
2. talib中所有指標函數
A. 按照首字母排序:
官方函數列表:http://ta-lib.org/function.html
B. 按照類型分組(點擊超鏈接進入官方文檔查看函數用法):
- Overlap Studies(重疊研究類)
- Momentum Indicators(動量指標類)
- Volume Indicators(成交量指標類)
- Volatility Indicators(波動性指標類)
- Price Transform(價格指標類)
- Cycle Indicators(周期指標類)
- Pattern Recognition(形態識別類)
- Statistic Functions(統計函數類)
- Math Transform(數學變換類)
- Math Operators(數學運算符類)
C. 每組的指標函數:
(可在頁面內按 Ctrl+f 鍵搜索到指標后,點擊其所在組別的超鏈接查看函數的使用方法)
- 重疊研究
BBANDS — Bollinger Bands (布林帶)
DEMA — Double Exponential Moving Average (雙指數移動平均線)
EMA — Exponential Moving Average (指數移動平均)
HT_TRENDLINE — Hilbert Transform – Instantaneous Trendline
KAMA — Kaufman Adaptive Moving Average
MA — Moving average (移動平均線)
MAMA — MESA Adaptive Moving Average (自適應移動平均)
MAVP — Moving average with variable period (變周期移動平均)
MIDPOINT — MidPoint over period
MIDPRICE — Midpoint Price over period
SAR — Parabolic SAR
SAREXT — Parabolic SAR – Extended
SMA — Simple Moving Average (簡單移動平均線)
T3 — Triple Exponential Moving Average (T3)
TEMA — Triple Exponential Moving Average (三重指數移動平均線)
TRIMA — Triangular Moving Average
WMA — Weighted Moving Average (加權移動平均線) - 動量指標
ADX — Average Directional Movement Index (平均趨向指數)
ADXR — Average Directional Movement Index Rating
APO — Absolute Price Oscillator
AROON — Aroon
AROONOSC — Aroon Oscillator
BOP — Balance Of Power
CCI — Commodity Channel Index
CMO — Chande Momentum Oscillator
DX — Directional Movement Index
MACD — Moving Average Convergence/Divergence (平滑異同移動平均線)
MACDEXT — MACD with controllable MA type
MACDFIX — Moving Average Convergence/Divergence Fix 12/26
MFI — Money Flow Index
MINUS_DI — Minus Directional Indicator
MINUS_DM — Minus Directional Movement
MOM — Momentum
PLUS_DI — Plus Directional Indicator
PLUS_DM — Plus Directional Movement
PPO — Percentage Price Oscillator
ROC — Rate of change : ((price/prevPrice)-1)*100
ROCP — Rate of change Percentage: (price-prevPrice)/prevPrice
ROCR — Rate of change ratio: (price/prevPrice)
ROCR100 — Rate of change ratio 100 scale: (price/prevPrice)*100
RSI — Relative Strength Index (相對強弱指數)
STOCH — Stochastic
STOCHF — Stochastic Fast
STOCHRSI — Stochastic Relative Strength Index
TRIX — 1-day Rate-Of-Change (ROC) of a Triple Smooth EMA
ULTOSC — Ultimate Oscillator
WILLR — Williams’ %R
- 成交量指標
AD — Chaikin A/D Line
ADOSC — Chaikin A/D Oscillator
OBV — On Balance Volume - 波動率指標
ATR — Average True Range (平均真實波幅)
NATR — Normalized Average True Range
TRANGE — True Range (真實波幅) - 價格指標
AVGPRICE — Average Price
MEDPRICE — Median Price
TYPPRICE — Typical Price
WCLPRICE — Weighted Close Price - 周期指標
HT_DCPERIOD — Hilbert Transform – Dominant Cycle Period
HT_DCPHASE — Hilbert Transform – Dominant Cycle Phase
HT_PHASOR — Hilbert Transform – Phasor Components
HT_SINE — Hilbert Transform – SineWave
HT_TRENDMODE — Hilbert Transform – Trend vs Cycle Mode - 形態識別
CDL2CROWS — Two Crows (兩只烏鴉)
CDL3BLACKCROWS — Three Black Crows (三只烏鴉)
CDL3INSIDE — Three Inside Up/Down (三內部上漲和下跌)
CDL3LINESTRIKE — Three-Line Strike (三線打擊)
CDL3OUTSIDE — Three Outside Up/Down (三外部上漲和下跌)
CDL3STARSINSOUTH — Three Stars In The South (南方三星)
CDL3WHITESOLDIERS — Three Advancing White Soldiers (三白兵)
CDLABANDONEDBABY — Abandoned Baby (棄嬰形態)
CDLADVANCEBLOCK — Advance Block (前方受阻(大敵當前))
CDLBELTHOLD — Belt-hold (捉腰帶線)
CDLBREAKAWAY — Breakaway (脫離)
CDLCLOSINGMARUBOZU — Closing Marubozu (收盤缺影線)
CDLCONCEALBABYSWALL — Concealing Baby Swallow (藏嬰吞沒)
CDLCOUNTERATTACK — Counterattack (反擊線)
CDLDARKCLOUDCOVER — Dark Cloud Cover (烏雲壓頂)
CDLDOJI — Doji (十字)
CDLDOJISTAR — Doji Star (十字星)
CDLDRAGONFLYDOJI — Dragonfly Doji (蜻蜓十字/T形十字)
CDLENGULFING — Engulfing Pattern (吞噬模式)
CDLEVENINGDOJISTAR — Evening Doji Star (十字暮星)
CDLEVENINGSTAR — Evening Star (暮星)
CDLGAPSIDESIDEWHITE — Up/Down-gap side-by-side white lines (向上/下跳空並列陽線)
CDLGRAVESTONEDOJI — Gravestone Doji (墓碑十字/倒T十字)
CDLHAMMER — Hammer (錘頭線)
CDLHANGINGMAN — Hanging Man (上吊線)
CDLHARAMI — Harami Pattern
CDLHARAMICROSS — Harami Cross Pattern
CDLHIGHWAVE — High-Wave Candle
CDLHIKKAKE — Hikkake Pattern (陷阱)
CDLHIKKAKEMOD — Modified Hikkake Pattern
CDLHOMINGPIGEON — Homing Pigeon (家鴿)
CDLIDENTICAL3CROWS — Identical Three Crows
CDLINNECK — In-Neck Pattern
CDLINVERTEDHAMMER — Inverted Hammer
CDLKICKING — Kicking (反沖形態)
CDLKICKINGBYLENGTH — Kicking – bull/bear determined by the longer marubozu
CDLLADDERBOTTOM — Ladder Bottom
CDLLONGLEGGEDDOJI — Long Legged Doji
CDLLONGLINE — Long Line Candle
CDLMARUBOZU — Marubozu
CDLMATCHINGLOW — Matching Low (相同低價)
CDLMATHOLD — Mat Hold
CDLMORNINGDOJISTAR — Morning Doji Star (十字晨星)
CDLMORNINGSTAR — Morning Star (晨星)
CDLONNECK — On-Neck Pattern
CDLPIERCING — Piercing Pattern (刺透形態)
CDLRICKSHAWMAN — Rickshaw Man
CDLRISEFALL3METHODS — Rising/Falling Three Methods
CDLSEPARATINGLINES — Separating Lines
CDLSHOOTINGSTAR — Shooting Star
CDLSHORTLINE — Short Line Candle
CDLSPINNINGTOP — Spinning Top(紡錘)
CDLSTALLEDPATTERN — Stalled Pattern
CDLSTICKSANDWICH — Stick Sandwich
CDLTAKURI — Takuri (Dragonfly Doji with very long lower shadow)
CDLTASUKIGAP — Tasuki Gap
CDLTHRUSTING — Thrusting Pattern
CDLTRISTAR — Tristar Pattern
CDLUNIQUE3RIVER — Unique 3 River
CDLUPSIDEGAP2CROWS — Upside Gap Two Crows
CDLXSIDEGAP3METHODS — Upside/Downside Gap Three Methods - 統計函數
BETA — Beta (貝塔系數)
CORREL — Pearson’s Correlation Coefficient (r)
LINEARREG — Linear Regression
LINEARREG_ANGLE — Linear Regression Angle
LINEARREG_INTERCEPT — Linear Regression Intercept
LINEARREG_SLOPE — Linear Regression Slope
STDDEV — Standard Deviation
TSF — Time Series Forecast
VAR — Variance - 數學變換
ACOS — Vector Trigonometric ACos
ASIN — Vector Trigonometric ASin
ATAN — Vector Trigonometric ATan
CEIL — Vector Ceil
COS — Vector Trigonometric Cos
COSH — Vector Trigonometric Cosh
EXP — Vector Arithmetic Exp
FLOOR — Vector Floor
LN — Vector Log Natural
LOG10 — Vector Log10
SIN — Vector Trigonometric Sin
SINH — Vector Trigonometric Sinh
SQRT — Vector Square Root
TAN — Vector Trigonometric Tan
TANH — Vector Trigonometric Tanh - 數學運算符
ADD — Vector Arithmetic Add
DIV — Vector Arithmetic Div
MAX — Highest value over a specified period
MAXINDEX — Index of highest value over a specified period
MIN — Lowest value over a specified period
MININDEX — Index of lowest value over a specified period
MINMAX — Lowest and highest values over a specified period
MINMAXINDEX — Indexes of lowest and highest values over a specified period
MULT — Vector Arithmetic Mult
SUB — Vector Arithmetic Substraction
SUM — Summation - 轉自https://www.shinnytech.com/blog/ta-lib_installation_instruction/