Introduction to TA-Lib and its use in the backtrader framework

1. Introduction to TA-Lib

  • TA-Lib  - a technical analysis lib widely used in trading software development, including more than 200 technical indicators, such as MACD, RSI, etc.
  • ta-lib for python  -- ta-lib packaged in python. python encapsulated ta-lib, efficiently implemented using Cython and Numpy, 2-4 times faster than the original version using the SWIG interface

2. Installation

The mac installation method is as follows:

$ brew install ta-lib
$ pip install TA-Lib

If you do not use brew to install ta-lib, the following error will be reported

func.c:256:28: fatal error: ta-lib/ta_libc.h: No such file or directory
compilation terminated.
If you get build errors like this, it typically means that it can't find the underlying TA-Lib library and needs to be installed:

windows, linux reference: https://mrjbq7.github.io/ta-lib/install.html

import talibIf no error is reported when using python , the installation is successful.

3. Use of commonly used indicators

  1. SMA: Simple Moving Average talib.SMA() requires the data to be in numpy.ndarray format talib.abstract.SMA() requires the data to be in dictionary format of Numpy array
import numpy as np
import talib

close = np.random.random(100)
output = talib.SMA(close) # 默认是SMA30
output = talib.SMA(close, timeperiod=5) # SMA5

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)
}
from talib.abstract import *
output = SMA(input_arrays, timeperiod=25) # 默认对close价格计算
output = SMA(input_arrays, timeperiod=25, price='open') # 对open价格计算

2. EMA: Exponential Moving Average

talib.EMA(close)
talib.abstract.EMA(inputs)

3. MACD: Moving Average Convergence/Divergence (Moving Average Convergence / Divergence) Through the signals of three types of data (a combination of moving averages), the strength and direction of the trend can be judged and the turning point of the trend can be determined.

talib.MACD(close)
talib.abstract.MACD(inputs)

4. List of supported indicators

  • Moving average indicators, such as EMA, SMA, WMA, etc.
  • Momentum indicators such as MACD, MOM, RSI, etc.
  • Trading volume indicators, such as AD, OBV, etc.
  • Variable indicators, such as ATR, NATR, etc.
  • Price transformation, such as AVGPRICE, MEDPRICE, etc.
  • Cycle indicators, such as HT_DCPERIOD, HT_SINE, etc.
  • Pattern recognition, such as CDL2CROWS, CDLHAMMER, etc.
  • Statistical functions, such as VAR, STDDEV, LINEARREG, etc.
  • Mathematical transformations, such as ACOS, ASIN, CEIL, COS, EXP, LN, SQRT, etc.
  • Mathematical operations, such as ADD, DIV, MAX, MULT, SUM, etc.

The supported functions and function list are as follows:

import talib
talib.get_functions()
print(len(talib.get_functions()))
talib.get_function_groups()
print(len(talib.get_function_groups()))

4. Call the TA-Lib library in backtrader

Follow the WeChat public account to view and communicate. WeChat public account: Zhuge Talk 

Introduction to TA-Lib and its use in the backtrader framework

Reference for more articles: Quantitative Station

Guess you like

Origin blog.csdn.net/richardzhutalk/article/details/124389051