Skip to content

Commit 5263446

Browse files
committed
fix: source unstable-period ids from the C header (#752)
_ta_func_unst_ids was built with enumerate() over the TA-Lib 0.4.0 names. TA-Lib C 0.6.0 inserted TA_FUNC_UNST_IMI at slot 12, so every id from there up mis-targets: set_unstable_period('RSI') sets PLUS_DM's unstable period, 'MAMA' sets KAMA's, and 'ALL' sets T3's alone. setup.py requires ta-lib >= 0.6.1, so this affects every normal install. Take the ids from lib.TA_FUNC_UNST_* instead. Cython reads the value of a `cdef extern` const from the header at build time and ignores the literal written in the .pxd, so one table is correct for every ta-lib release with no version detection. 'ADXR', 'MFI' and 'STOCHRSI' remain accepted keys but are now no-ops that emit a DeprecationWarning; TA-Lib C retired those slots. They are not aliased to the inner ADX/RSI, which would turn a long-standing no-op into a call with side effects on other functions. In _ta_lib.pxd, fix the TA_FUNC_UNST_HD_PHASOR typo, drop the stale literal values, and drop the enumerators that no longer exist. The new test is behavioural on purpose: set and get look the id up in the same table, so a wrong id round-trips perfectly. Verified against ta-lib C 0.6.4, 0.7.1 and 0.8.1.
1 parent 290dd19 commit 5263446

3 files changed

Lines changed: 184 additions & 35 deletions

File tree

‎talib/_common.pxi‎

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import warnings
2+
13
cimport _ta_lib as lib
24
from _ta_lib cimport TA_RetCode, TA_FuncUnstId
35

@@ -78,24 +80,73 @@ class MA_Type(object):
7880

7981
MA_Type = MA_Type()
8082

81-
_ta_func_unst_ids = {'NONE': -1}
82-
for i, name in enumerate([
83-
'ADX', 'ADXR', 'ATR', 'CMO', 'DX', 'EMA', 'HT_DCPERIOD',
84-
'HT_DCPHASE', 'HT_PHASOR', 'HT_SINE', 'HT_TRENDLINE',
85-
'HT_TRENDMODE', 'KAMA', 'MAMA', 'MFI', 'MINUS_DI', 'MINUS_DM',
86-
'NATR', 'PLUS_DI', 'PLUS_DM', 'RSI', 'STOCHRSI', 'T3', 'ALL'
87-
]):
88-
_ta_func_unst_ids[name] = i
83+
# Take every id straight from the C header instead of hardcoding a list.
84+
# Cython resolves each `lib.TA_FUNC_UNST_*` at build time from the ta_defs.h
85+
# it compiles against, so this one table is correct for every TA-Lib release.
86+
_ta_func_unst_ids = {
87+
# Not sourced from the header: TA_FUNC_UNST_NONE does not exist in newer
88+
# ta-lib releases, and -1 is a Python-side sentinel anyway.
89+
'NONE': -1,
90+
'ADX': lib.TA_FUNC_UNST_ADX,
91+
'ATR': lib.TA_FUNC_UNST_ATR,
92+
'CMO': lib.TA_FUNC_UNST_CMO,
93+
'DX': lib.TA_FUNC_UNST_DX,
94+
'EMA': lib.TA_FUNC_UNST_EMA,
95+
'HT_DCPERIOD': lib.TA_FUNC_UNST_HT_DCPERIOD,
96+
'HT_DCPHASE': lib.TA_FUNC_UNST_HT_DCPHASE,
97+
'HT_PHASOR': lib.TA_FUNC_UNST_HT_PHASOR,
98+
'HT_SINE': lib.TA_FUNC_UNST_HT_SINE,
99+
'HT_TRENDLINE': lib.TA_FUNC_UNST_HT_TRENDLINE,
100+
'HT_TRENDMODE': lib.TA_FUNC_UNST_HT_TRENDMODE,
101+
'KAMA': lib.TA_FUNC_UNST_KAMA,
102+
'MAMA': lib.TA_FUNC_UNST_MAMA,
103+
'MINUS_DI': lib.TA_FUNC_UNST_MINUS_DI,
104+
'MINUS_DM': lib.TA_FUNC_UNST_MINUS_DM,
105+
'NATR': lib.TA_FUNC_UNST_NATR,
106+
'PLUS_DI': lib.TA_FUNC_UNST_PLUS_DI,
107+
'PLUS_DM': lib.TA_FUNC_UNST_PLUS_DM,
108+
'RSI': lib.TA_FUNC_UNST_RSI,
109+
'T3': lib.TA_FUNC_UNST_T3,
110+
'ALL': lib.TA_FUNC_UNST_ALL,
111+
}
112+
113+
# Still accepted so existing code keeps running, but they are no longer knobs
114+
# in TA-Lib C: their enumerators are now TA_FUNC_UNST_UNUSED_*. They are not
115+
# aliased to their inner indicator on purpose -- turning a long-standing no-op
116+
# into a call that changes ADX or RSI for every other function would be a
117+
# worse surprise than doing nothing.
118+
_ta_func_unst_retired = {
119+
'ADXR': "ADXR has no unstable period of its own; it follows the inner ADX. Set 'ADX' instead.",
120+
'MFI': "MFI's unstable period was retired in TA-Lib C.",
121+
'STOCHRSI': "STOCHRSI has no unstable period of its own; it follows the inner RSI. Set 'RSI' instead.",
122+
}
123+
124+
def _ta_func_unst_id(name):
125+
if name in _ta_func_unst_retired:
126+
warnings.warn(
127+
"unstable period '%s' is deprecated and has no effect: %s" % (
128+
name, _ta_func_unst_retired[name]),
129+
DeprecationWarning, stacklevel=3)
130+
return None
131+
return _ta_func_unst_ids[name]
89132

90133
def _ta_set_unstable_period(name, period):
91134
cdef TA_RetCode ret_code
92-
cdef TA_FuncUnstId id = _ta_func_unst_ids[name]
135+
cdef TA_FuncUnstId id
136+
unst_id = _ta_func_unst_id(name)
137+
if unst_id is None:
138+
return
139+
id = unst_id
93140
ret_code = lib.TA_SetUnstablePeriod(id, period)
94141
_ta_check_success('TA_SetUnstablePeriod', ret_code)
95142

96143
def _ta_get_unstable_period(name):
97144
cdef unsigned int period
98-
cdef TA_FuncUnstId id = _ta_func_unst_ids[name]
145+
cdef TA_FuncUnstId id
146+
unst_id = _ta_func_unst_id(name)
147+
if unst_id is None:
148+
return 0
149+
id = unst_id
99150
period = lib.TA_GetUnstablePeriod(id)
100151
return period
101152

‎talib/_ta_lib.pxd‎

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,31 @@ cdef extern from "ta-lib/ta_defs.h":
3939
const TA_MAType TA_MAType_T3 = 8
4040

4141
ctypedef int TA_FuncUnstId
42-
const TA_FuncUnstId TA_FUNC_UNST_ADX = 0
43-
const TA_FuncUnstId TA_FUNC_UNST_ADXR = 1
44-
const TA_FuncUnstId TA_FUNC_UNST_ATR = 2
45-
const TA_FuncUnstId TA_FUNC_UNST_CMO = 3
46-
const TA_FuncUnstId TA_FUNC_UNST_DX = 4
47-
const TA_FuncUnstId TA_FUNC_UNST_EMA = 5
48-
const TA_FuncUnstId TA_FUNC_UNST_HT_DCPERIOD = 6
49-
const TA_FuncUnstId TA_FUNC_UNST_HT_DCPHASE = 7
50-
const TA_FuncUnstId TA_FUNC_UNST_HD_PHASOR = 8
51-
const TA_FuncUnstId TA_FUNC_UNST_HT_SINE = 9
52-
const TA_FuncUnstId TA_FUNC_UNST_HT_TRENDLINE = 10
53-
const TA_FuncUnstId TA_FUNC_UNST_HT_TRENDMODE = 11
54-
const TA_FuncUnstId TA_FUNC_UNST_KAMA = 12
55-
const TA_FuncUnstId TA_FUNC_UNST_MAMA = 13
56-
const TA_FuncUnstId TA_FUNC_UNST_MFI = 14
57-
const TA_FuncUnstId TA_FUNC_UNST_MINUS_DI = 15
58-
const TA_FuncUnstId TA_FUNC_UNST_MINUS_DM = 16
59-
const TA_FuncUnstId TA_FUNC_UNST_NATR = 17
60-
const TA_FuncUnstId TA_FUNC_UNST_PLUS_DI = 18
61-
const TA_FuncUnstId TA_FUNC_UNST_PLUS_DM = 19
62-
const TA_FuncUnstId TA_FUNC_UNST_RSI = 20
63-
const TA_FuncUnstId TA_FUNC_UNST_STOCHRSI = 21
64-
const TA_FuncUnstId TA_FUNC_UNST_T3 = 21
65-
const TA_FuncUnstId TA_FUNC_UNST_ALL = 22
66-
const TA_FuncUnstId TA_FUNC_UNST_NONE = -1
42+
# No values here on purpose. Cython takes the value of a `cdef extern`
43+
# const from the C header at build time and ignores anything written on
44+
# the right-hand side, so a literal would be decoration that goes stale
45+
# without ever failing a build.
46+
const TA_FuncUnstId TA_FUNC_UNST_ADX
47+
const TA_FuncUnstId TA_FUNC_UNST_ATR
48+
const TA_FuncUnstId TA_FUNC_UNST_CMO
49+
const TA_FuncUnstId TA_FUNC_UNST_DX
50+
const TA_FuncUnstId TA_FUNC_UNST_EMA
51+
const TA_FuncUnstId TA_FUNC_UNST_HT_DCPERIOD
52+
const TA_FuncUnstId TA_FUNC_UNST_HT_DCPHASE
53+
const TA_FuncUnstId TA_FUNC_UNST_HT_PHASOR
54+
const TA_FuncUnstId TA_FUNC_UNST_HT_SINE
55+
const TA_FuncUnstId TA_FUNC_UNST_HT_TRENDLINE
56+
const TA_FuncUnstId TA_FUNC_UNST_HT_TRENDMODE
57+
const TA_FuncUnstId TA_FUNC_UNST_KAMA
58+
const TA_FuncUnstId TA_FUNC_UNST_MAMA
59+
const TA_FuncUnstId TA_FUNC_UNST_MINUS_DI
60+
const TA_FuncUnstId TA_FUNC_UNST_MINUS_DM
61+
const TA_FuncUnstId TA_FUNC_UNST_NATR
62+
const TA_FuncUnstId TA_FUNC_UNST_PLUS_DI
63+
const TA_FuncUnstId TA_FUNC_UNST_PLUS_DM
64+
const TA_FuncUnstId TA_FUNC_UNST_RSI
65+
const TA_FuncUnstId TA_FUNC_UNST_T3
66+
const TA_FuncUnstId TA_FUNC_UNST_ALL
6767

6868
ctypedef int TA_RangeType
6969
const TA_RangeType TA_RangeType_RealBody = 0

‎tests/test_func.py‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,104 @@ def test_unstable_period():
6363
talib.set_unstable_period('EMA', 0)
6464

6565

66+
# One entry per real unstable-period id in TA-Lib C. Each id is named after
67+
# the function it controls, so every case simply calls that same function.
68+
def _unstable_period_cases():
69+
n = 400
70+
rs = np.random.RandomState(1)
71+
close = np.cumsum(rs.randn(n)) + 100.0
72+
high = close + rs.rand(n) + 0.5
73+
low = close - rs.rand(n) - 0.5
74+
return {
75+
'ADX': lambda: func.ADX(high, low, close),
76+
'ATR': lambda: func.ATR(high, low, close),
77+
'CMO': lambda: func.CMO(close),
78+
'DX': lambda: func.DX(high, low, close),
79+
'EMA': lambda: func.EMA(close),
80+
'HT_DCPERIOD': lambda: func.HT_DCPERIOD(close),
81+
'HT_DCPHASE': lambda: func.HT_DCPHASE(close),
82+
'HT_PHASOR': lambda: func.HT_PHASOR(close)[0],
83+
'HT_SINE': lambda: func.HT_SINE(close)[0],
84+
'HT_TRENDLINE': lambda: func.HT_TRENDLINE(close),
85+
'HT_TRENDMODE': lambda: func.HT_TRENDMODE(close),
86+
'KAMA': lambda: func.KAMA(close),
87+
'MAMA': lambda: func.MAMA(close)[0],
88+
'MINUS_DI': lambda: func.MINUS_DI(high, low, close),
89+
'MINUS_DM': lambda: func.MINUS_DM(high, low),
90+
'NATR': lambda: func.NATR(high, low, close),
91+
'PLUS_DI': lambda: func.PLUS_DI(high, low, close),
92+
'PLUS_DM': lambda: func.PLUS_DM(high, low),
93+
'RSI': lambda: func.RSI(close),
94+
'T3': lambda: func.T3(close),
95+
}
96+
97+
98+
UNSTABLE_PERIOD_CASES = _unstable_period_cases()
99+
100+
101+
def _leading_unset(result):
102+
# TA-Lib writes nothing before its lookback: real outputs stay NaN there,
103+
# the one integer output (HT_TRENDMODE) stays 0.
104+
valid = ~np.isnan(result) if result.dtype.kind == 'f' else result != 0
105+
assert valid.any(), 'no valid output to measure'
106+
return int(np.argmax(valid))
107+
108+
109+
# NOTE: this has to be a behavioural test. set_unstable_period() and
110+
# get_unstable_period() look the id up in the same table, so a wrong id
111+
# round-trips perfectly -- which is how ta-lib-python shipped a table that
112+
# pointed 'RSI' at PLUS_DM for two years (issue #752). Only checking that the
113+
# setting moves THAT function's own output can catch it. Please do not
114+
# "simplify" this into a get/set assertion.
115+
@pytest.mark.parametrize('name', sorted(UNSTABLE_PERIOD_CASES))
116+
def test_unstable_period_moves_its_own_function(name):
117+
call = UNSTABLE_PERIOD_CASES[name]
118+
talib.set_unstable_period(name, 0)
119+
unshifted = call()
120+
baseline = _leading_unset(unshifted)
121+
assert baseline > 0, 'nothing to shift'
122+
try:
123+
talib.set_unstable_period(name, 5)
124+
shifted = call()
125+
assert _leading_unset(shifted) == baseline + 5
126+
# an unstable period only discards warm-up bars, so whatever both runs
127+
# do emit has to be identical -- this pins the shift to a pure delay
128+
assert_array_equal(shifted[baseline + 5:], unshifted[baseline + 5:])
129+
finally:
130+
talib.set_unstable_period(name, 0)
131+
assert _leading_unset(call()) == baseline
132+
133+
134+
def test_unstable_period_all():
135+
talib.set_unstable_period('ALL', 0)
136+
baseline = {name: _leading_unset(call())
137+
for name, call in UNSTABLE_PERIOD_CASES.items()}
138+
try:
139+
talib.set_unstable_period('ALL', 3)
140+
for name, call in UNSTABLE_PERIOD_CASES.items():
141+
assert _leading_unset(call()) == baseline[name] + 3, name
142+
finally:
143+
talib.set_unstable_period('ALL', 0)
144+
for name, call in UNSTABLE_PERIOD_CASES.items():
145+
assert _leading_unset(call()) == baseline[name], name
146+
147+
148+
# These three are accepted for backwards compatibility only: TA-Lib C retired
149+
# their unstable-period slots. They must stay no-ops -- aliasing them to the
150+
# inner ADX/RSI would give them side effects on every other function.
151+
@pytest.mark.parametrize('name', ['ADXR', 'MFI', 'STOCHRSI'])
152+
def test_unstable_period_retired_is_a_warning_and_a_noop(name):
153+
talib.set_unstable_period('ALL', 0)
154+
before = {n: _leading_unset(call())
155+
for n, call in UNSTABLE_PERIOD_CASES.items()}
156+
with pytest.deprecated_call():
157+
talib.set_unstable_period(name, 5)
158+
with pytest.deprecated_call():
159+
assert talib.get_unstable_period(name) == 0
160+
for n, call in UNSTABLE_PERIOD_CASES.items():
161+
assert _leading_unset(call()) == before[n], n
162+
163+
66164
def test_compatibility():
67165
a = np.arange(10, dtype=float)
68166
talib.set_compatibility(0)

0 commit comments

Comments
 (0)