Skip to content

Commit 75659b4

Browse files
committed
feat: support TA-Lib C 0.8.1
TA-Lib C 0.8.1 is now the minimum required version. - Bind the 40 functions added since 0.7.1: 161 -> 201. - MA_Type gains HMA, DISABLED, DEFAULT, ZLEMA and RMA; set_unstable_period() gains RMA, HA and RVI. - generate_func.py / generate_stream.py: skip any declaration mentioning a TA_<FUNC>_Stream handle. The verb list it replaces missed _Value and _Clone, which 0.8.1 declares for every function. - generate_func.py / generate_stream.py: a moving-average parameter not spelled exactly 'matype' -- KDJ's slowk_matype -- defaulted to SMA rather than to the function's own default, so func.KDJ and abstract.KDJ disagreed. - abstract: ignore flag bits added after this wrapper was written instead of raising KeyError, and keep the -100/0/100 wording for candlesticks only (SUPERTREND's integer output is a trend direction). - Building against an older ta-lib now fails at compile time; the import guard explains a stale extension. - Tests: func and abstract must agree on all 201, and the stubs must parse and cover every function. talib/_ta_lib.c is left untouched; it is refreshed at release time. Claude-Session: https://claude.ai/code/session_01R8JMZAcnp5snoMjFd1HyMU
1 parent 9899d25 commit 75659b4

19 files changed

Lines changed: 4335 additions & 219 deletions

‎.github/workflows/wheels.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
workflow_dispatch:
88

99
env:
10-
TALIB_C_VER: 0.7.1
10+
TALIB_C_VER: 0.8.1
1111
PIP_NO_VERIFY: 0
1212
PIP_VERBOSE: 1
1313
CIBW_BEFORE_BUILD: pip install -U setuptools Cython wheel meson-python ninja && pip install -U numpy

‎CHANGELOG‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1+
0.8.0
2+
=====
3+
4+
- [NEW]: Support TA-Lib C 0.8.1, which is now the minimum required version.
5+
6+
- [NEW]: The 40 functions TA-Lib C added since 0.7.1: AC, ADR, AO, CMF, CMOU,
7+
COPPOCK, CUMSUM, CVI, DONCHIAN, DPO, EFI, ER, ERI, FOSC, FRACTAL, HA, HMA,
8+
KC, KDJ, MARKETFI, MASSI, NVI, PERCENTILE, PERCENTRANK, PVI, PVO, PVT,
9+
QSTICK, RMA, RVI, RVOL, SMI, SUPERTREND, TSI, VHF, VORTEX, VWAP, VWMA, WAD,
10+
ZLEMA.
11+
12+
- [NEW]: New moving averages: ``MA_Type.HMA``, ``MA_Type.DISABLED``,
13+
``MA_Type.DEFAULT``, ``MA_Type.ZLEMA`` and ``MA_Type.RMA``.
14+
15+
- [NEW]: ``set_unstable_period()`` and ``get_unstable_period()`` accept
16+
``'RMA'``, ``'HA'`` and ``'RVI'``.
17+
18+
- [FIX]: ``abstract`` raised ``KeyError`` on function and output flags added
19+
after this wrapper was written; unknown flag bits are now ignored.
20+
21+
- [FIX]: A moving-average parameter not spelled exactly ``matype`` -- ``KDJ``'s
22+
``slowk_matype`` -- defaulted to SMA rather than to the function's own
23+
documented default.
24+
25+
- [FIX]: An ``integer`` output is documented as the candlestick -100/0/100
26+
convention only for candlestick functions; ``SUPERTREND``'s is a trend
27+
direction.
28+
29+
- [CHANGE]: ``APO`` and ``PPO`` now default ``matype`` to EMA, and ``BBANDS``
30+
defaults ``timeperiod`` to 20, following TA-Lib C 0.8.1.
31+
132
0.7.1
233
=====
334

‎talib/__init__.py‎

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,30 @@ def wrapper(*args, **kwds):
106106
_wrapper = lambda x: x
107107

108108

109-
from ._ta_lib import (
110-
_ta_initialize, _ta_shutdown, MA_Type, __ta_version__,
111-
_ta_set_unstable_period as set_unstable_period,
112-
_ta_get_unstable_period as get_unstable_period,
113-
_ta_set_compatibility as set_compatibility,
114-
_ta_get_compatibility as get_compatibility,
115-
__TA_FUNCTION_NAMES__
116-
)
109+
# The TA-Lib C library this wrapper is built against
110+
TA_LIB_C_REQUIRED = '0.8.1'
111+
112+
try:
113+
from ._ta_lib import (
114+
_ta_initialize, _ta_shutdown, MA_Type, __ta_version__,
115+
_ta_set_unstable_period as set_unstable_period,
116+
_ta_get_unstable_period as get_unstable_period,
117+
_ta_set_compatibility as set_compatibility,
118+
_ta_get_compatibility as get_compatibility,
119+
__TA_FUNCTION_NAMES__
120+
)
121+
except ImportError as error:
122+
# Loading the extension resolves its symbols against whatever TA-Lib C is
123+
# installed. Linking never catches a too-old library -- a shared object may
124+
# keep undefined symbols -- so a missing function shows up here instead, as
125+
# "undefined symbol: TA_CMF_Lookback" or the macOS/Windows equivalent.
126+
raise ImportError(
127+
'%s\n\n'
128+
'talib could not load its extension module. This build requires the '
129+
'TA-Lib C library %s or later; an older one is missing functions this '
130+
'wrapper calls. See https://ta-lib.org/install/'
131+
% (error, TA_LIB_C_REQUIRED)
132+
) from error
117133

118134
# import all the func and stream functions
119135
from ._ta_lib import *
@@ -154,6 +170,7 @@ def wrapper(*args, **kwds):
154170
],
155171
'Math Operators': [
156172
'ADD',
173+
'CUMSUM',
157174
'DIV',
158175
'MAX',
159176
'MAXINDEX',
@@ -183,16 +200,26 @@ def wrapper(*args, **kwds):
183200
'TANH',
184201
],
185202
'Momentum Indicators': [
203+
'AC',
186204
'ADX',
187205
'ADXR',
206+
'AO',
188207
'APO',
189208
'AROON',
190209
'AROONOSC',
191210
'BOP',
192211
'CCI',
193212
'CMO',
213+
'CMOU',
214+
'COPPOCK',
215+
'DPO',
194216
'DX',
217+
'ER',
218+
'ERI',
219+
'FOSC',
220+
'FRACTAL',
195221
'IMI',
222+
'KDJ',
196223
'MACD',
197224
'MACDEXT',
198225
'MACDFIX',
@@ -203,37 +230,50 @@ def wrapper(*args, **kwds):
203230
'PLUS_DI',
204231
'PLUS_DM',
205232
'PPO',
233+
'QSTICK',
206234
'ROC',
207235
'ROCP',
208236
'ROCR',
209237
'ROCR100',
210238
'RSI',
239+
'SMI',
211240
'STOCH',
212241
'STOCHF',
213242
'STOCHRSI',
214243
'TRIX',
244+
'TSI',
215245
'ULTOSC',
246+
'VHF',
247+
'VORTEX',
248+
'WAD',
216249
'WILLR',
217250
],
218251
'Overlap Studies': [
219252
'ACCBANDS',
220253
'BBANDS',
221254
'DEMA',
255+
'DONCHIAN',
222256
'EMA',
257+
'HMA',
223258
'HT_TRENDLINE',
224259
'KAMA',
260+
'KC',
225261
'MA',
226262
'MAMA',
227263
'MAVP',
228264
'MIDPOINT',
229265
'MIDPRICE',
266+
'RMA',
230267
'SAR',
231268
'SAREXT',
232269
'SMA',
270+
'SUPERTREND',
233271
'T3',
234272
'TEMA',
235273
'TRIMA',
274+
'VWMA',
236275
'WMA',
276+
'ZLEMA',
237277
],
238278
'Pattern Recognition': [
239279
'CDL2CROWS',
@@ -301,6 +341,7 @@ def wrapper(*args, **kwds):
301341
'Price Transform': [
302342
'AVGDEV',
303343
'AVGPRICE',
344+
'HA',
304345
'MEDPRICE',
305346
'TYPPRICE',
306347
'WCLPRICE',
@@ -312,19 +353,34 @@ def wrapper(*args, **kwds):
312353
'LINEARREG_ANGLE',
313354
'LINEARREG_INTERCEPT',
314355
'LINEARREG_SLOPE',
356+
'PERCENTILE',
357+
'PERCENTRANK',
315358
'STDDEV',
316359
'TSF',
317360
'VAR',
318361
],
319362
'Volatility Indicators': [
363+
'ADR',
320364
'ATR',
365+
'CVI',
366+
'MASSI',
321367
'NATR',
368+
'RVI',
322369
'TRANGE',
323370
],
324371
'Volume Indicators': [
325372
'AD',
326373
'ADOSC',
327-
'OBV'
374+
'CMF',
375+
'EFI',
376+
'MARKETFI',
377+
'NVI',
378+
'OBV',
379+
'PVI',
380+
'PVO',
381+
'PVT',
382+
'RVOL',
383+
'VWAP',
328384
],
329385
}
330386

‎talib/_abstract.pxi‎

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'''
22
This file Copyright (c) 2013 Brian A Cappello <briancappello at gmail>
33
'''
4-
import math
54
import threading
65
try:
76
from collections import OrderedDict
@@ -592,30 +591,23 @@ def __get_flags(int flag, dict flags_lookup_dict):
592591
This function returns the flags from flag found in the provided
593592
flags_lookup_dict.
594593
"""
595-
value_range = flags_lookup_dict.keys()
596-
if not isinstance(value_range, list):
597-
value_range = list(value_range)
598-
min_int = int(math.log(min(value_range), 2))
599-
max_int = int(math.log(max(value_range), 2))
600-
601-
# if the flag we got is out-of-range, it just means no extra info provided
602-
if flag < 1 or flag > 2**max_int:
594+
# A bit with no description means the installed ta-lib is newer than this
595+
# build knows about; skip it rather than raising.
596+
if flag < 1:
603597
return None
604-
605-
# In this loop, i is essentially the bit-position, which represents an
606-
# input from flags_lookup_dict. We loop through as many flags_lookup_dict
607-
# bit-positions as we need to check, bitwise-ANDing each with flag for a hit.
608-
ret = []
609-
for i in xrange(min_int, max_int+1):
610-
if 2**i & flag:
611-
ret.append(flags_lookup_dict[2**i])
612-
return ret
598+
return [description
599+
for bit, description in sorted(flags_lookup_dict.items())
600+
if bit & flag]
613601

614602
TA_FUNC_FLAGS = {
603+
1: 'A period of 1 performs no smoothing',
615604
16777216: 'Output scale same as input',
605+
33554432: 'Function has a streaming API',
616606
67108864: 'Output is over volume',
617607
134217728: 'Function has an unstable period',
618-
268435456: 'Output is a candlestick'
608+
268435456: 'Output is a candlestick',
609+
536870912: 'Output is path-dependent',
610+
1073741824: 'Output can be NaN or infinite',
619611
}
620612

621613
# when flag is 0, the function (should) work on any reasonable input ndarray
@@ -642,7 +634,8 @@ TA_OUTPUT_FLAGS = {
642634
512: 'Output can be negative',
643635
1024: 'Output can be zero',
644636
2048: 'Values represent an upper limit',
645-
4096: 'Values represent a lower limit'
637+
4096: 'Values represent a lower limit',
638+
8192: 'Output is optional (nullable)',
646639
}
647640

648641
def _ta_getFuncInfo(char *function_name):
@@ -754,13 +747,14 @@ def _get_defaults_and_docs(func_info):
754747
docs.append(' %s: %s' % (param, params[param]))
755748
func_args.append('[%s=%s]' % (param, params[param]))
756749
defaults[param] = params[param]
757-
if param == 'matype':
750+
if param.endswith('matype'):
758751
docs[-1] = ' '.join([docs[-1], '(%s)' % MA_Type[params[param]]])
759752

760753
outputs = func_info['output_names']
754+
candlestick = 'Output is a candlestick' in (func_info['function_flags'] or [])
761755
docs.append('Outputs:')
762756
for output in outputs:
763-
if output == 'integer':
757+
if output == 'integer' and candlestick:
764758
output = 'integer (values are -100, 0 or 100)'
765759
docs.append(' %s' % output)
766760

‎talib/_common.pxi‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ cpdef _ta_check_success(str function_name, TA_RetCode ret_code):
4040
description = 'Bad Object (TA_BAD_OBJECT)'
4141
elif ret_code == 16:
4242
description = 'Not Supported (TA_NOT_SUPPORTED)'
43+
elif ret_code == 17:
44+
description = 'Insufficient History (TA_INSUFFICIENT_HISTORY)'
4345
elif ret_code == 5000:
4446
description = 'Internal Error (TA_INTERNAL_ERROR)'
4547
elif ret_code == 65535:
@@ -60,7 +62,8 @@ def _ta_shutdown():
6062
_ta_check_success('TA_Shutdown', ret_code)
6163

6264
class MA_Type(object):
63-
SMA, EMA, WMA, DEMA, TEMA, TRIMA, KAMA, MAMA, T3 = range(9)
65+
SMA, EMA, WMA, DEMA, TEMA, TRIMA, KAMA, MAMA, T3, HMA, DISABLED, DEFAULT, \
66+
ZLEMA, RMA = range(14)
6467

6568
def __init__(self):
6669
self._lookup = {
@@ -73,6 +76,11 @@ class MA_Type(object):
7376
MA_Type.KAMA: 'Kaufman Adaptive Moving Average',
7477
MA_Type.MAMA: 'MESA Adaptive Moving Average',
7578
MA_Type.T3: 'Triple Generalized Double Exponential Moving Average',
79+
MA_Type.HMA: 'Hull Moving Average',
80+
MA_Type.DISABLED: 'No Moving Average (identity)',
81+
MA_Type.DEFAULT: "The Function's Own Default Moving Average",
82+
MA_Type.ZLEMA: 'Zero-Lag Exponential Moving Average',
83+
MA_Type.RMA: "Wilder's Smoothed Moving Average",
7684
}
7785

7886
def __getitem__(self, type_):
@@ -107,6 +115,9 @@ _ta_func_unst_ids = {
107115
'PLUS_DM': lib.TA_FUNC_UNST_PLUS_DM,
108116
'RSI': lib.TA_FUNC_UNST_RSI,
109117
'T3': lib.TA_FUNC_UNST_T3,
118+
'RMA': lib.TA_FUNC_UNST_RMA,
119+
'HA': lib.TA_FUNC_UNST_HA,
120+
'RVI': lib.TA_FUNC_UNST_RVI,
110121
'ALL': lib.TA_FUNC_UNST_ALL,
111122
}
112123

0 commit comments

Comments
 (0)