抽象API快速开始

如果您已经熟悉使用函数API,那么您就应该在精通使用抽象API。 每个函数有相同的输入,作为一个字典通过NumPy数组:

  1. import numpy as np
  2. #请注意,所有的ndarrays必须是相同的长度!
  3. inputs = {
  4. 'open': np.random.random(100),
  5. 'high': np.random.random(100),
  6. 'low': np.random.random(100),
  7. 'close': np.random.random(100),
  8. 'volume': np.random.random(100)
  9. }

函数可以直接导入,也可以用名称实例化:

  1. from talib import abstract
  2. sma = abstract.SMA
  3. sma = abstract.Function('sma')

调用函数基本上与函数API相同:

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

高级用法

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).
对于更高级的TA库用例,抽象API也提供了更大的灵活性。 你甚至可以子类abstract.Function和覆盖set_input_arrays自定义类型的输入数据的函数接受 (e.g. a pandas DataFrame).

Details about every function can be accessed via the info property:
有关每个功能的详细信息可以通过信息属性访问:

  1. print Function('stoch').info
  2. {
  3. 'name': 'STOCH',
  4. 'display_name': 'Stochastic',
  5. 'group': 'Momentum Indicators',
  6. 'input_names': OrderedDict([
  7. ('prices', ['high', 'low', 'close']),
  8. ]),
  9. 'parameters': OrderedDict([
  10. ('fastk_period', 5),
  11. ('slowk_period', 3),
  12. ('slowk_matype', 0),
  13. ('slowd_period', 3),
  14. ('slowd_matype', 0),
  15. ]),
  16. 'output_names': ['slowk', 'slowd'],
  17. }

或者是可读的格式:

  1. help(STOCH)
  2. str(STOCH)

其他有用属性 Function:

  1. Function('x').function_flags
  2. Function('x').input_names
  3. Function('x').input_arrays
  4. Function('x').parameters
  5. Function('x').lookback
  6. Function('x').output_names
  7. Function('x').output_flags
  8. 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():
除了直接调用函数,函数还可以保持状态,已经记住他们的 参数/数组 你可以设置参数,重新计算使用run()新输入数据

  1. SMA.parameters = {'timeperiod': 15}
  2. result1 = SMA.run(input_arrays1)
  3. result2 = SMA.run(input_arrays2)
  4. # Or set input_arrays and change the parameters:
  5. SMA.input_arrays = input_arrays1
  6. ma10 = SMA(timeperiod=10)
  7. ma20 = SMA(20)

欲了解更多详情,请看 code.