Skip to content

Commit 31a0349

Browse files
committed
Adding TA_SetUnstablePeriod and TA_GetUnstablePeriod.
1 parent 309b42d commit 31a0349

File tree

7 files changed

+1258
-496
lines changed

7 files changed

+1258
-496
lines changed

talib/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from . import common
55
from . import abstract
66
from .common import MA_Type, __ta_version__
7+
from .common import _ta_set_unstable_period as set_unstable_period
8+
from .common import _ta_get_unstable_period as get_unstable_period
79
from .func import *
810

911
__version__ = '0.4.9'

talib/abstract.c

Lines changed: 174 additions & 174 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

talib/common.c

Lines changed: 871 additions & 145 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

talib/common.pyx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
cimport libc as lib
3-
from libc cimport TA_RetCode
3+
from libc cimport TA_RetCode, TA_FuncUnstId
44

55
__ta_version__ = lib.TA_GetVersionString()
66

@@ -35,13 +35,11 @@ def _ta_initialize():
3535
cdef TA_RetCode ret_code
3636
ret_code = lib.TA_Initialize()
3737
_ta_check_success('TA_Initialize', ret_code)
38-
return ret_code
3938

4039
def _ta_shutdown():
4140
cdef TA_RetCode ret_code
4241
ret_code = lib.TA_Shutdown()
4342
_ta_check_success('TA_Shutdown', ret_code)
44-
return ret_code
4543

4644
class MA_Type(object):
4745
SMA, EMA, WMA, DEMA, TEMA, TRIMA, KAMA, MAMA, T3 = range(9)
@@ -63,3 +61,24 @@ class MA_Type(object):
6361
return self._lookup[type_]
6462

6563
MA_Type = MA_Type()
64+
65+
_ta_func_unst_ids = {'NONE': -1}
66+
for i, name in enumerate([
67+
'ADX', 'ADXR', 'ATR', 'CMO', 'DX', 'EMA', 'HT_DCPERIOD',
68+
'HT_DCPHASE', 'HT_PHASOR', 'HT_SINE', 'HT_TRENDLINE',
69+
'HT_TRENDMODE', 'KAMA', 'MAMA', 'MFI', 'MINUS_DI', 'MINUS_DM',
70+
'NATR', 'PLUS_DI', 'PLUS_DM', 'RSI', 'STOCHRSI', 'T3', 'ALL'
71+
]):
72+
_ta_func_unst_ids[name] = i
73+
74+
def _ta_set_unstable_period(name, period):
75+
cdef TA_RetCode ret_code
76+
cdef TA_FuncUnstId id = _ta_func_unst_ids[name]
77+
ret_code = lib.TA_SetUnstablePeriod(id, period)
78+
_ta_check_success('TA_SetUnstablePeriod', ret_code)
79+
80+
def _ta_get_unstable_period(name):
81+
cdef unsigned int period
82+
cdef TA_FuncUnstId id = _ta_func_unst_ids[name]
83+
period = lib.TA_GetUnstablePeriod(id)
84+
return period

talib/func.c

Lines changed: 174 additions & 174 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

talib/libc.pxd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ cdef extern from "ta-lib/ta_defs.h":
2525

2626
ctypedef int TA_MAType
2727

28+
ctypedef int TA_FuncUnstId
29+
2830
cdef extern from "ta-lib/ta_common.h":
2931
ctypedef int TA_Integer
3032
ctypedef double TA_Real
@@ -437,3 +439,7 @@ cdef extern from "ta-lib/ta_func.h":
437439
int TA_WILLR_Lookback( int optInTimePeriod )
438440
TA_RetCode TA_WMA( int startIdx, int endIdx, const double inReal[], int optInTimePeriod, int *outBegIdx, int *outNBElement, double outReal[] )
439441
int TA_WMA_Lookback( int optInTimePeriod )
442+
443+
# TALIB functions for TA_SetUnstablePeriod
444+
TA_RetCode TA_SetUnstablePeriod(TA_FuncUnstId id, unsigned int unstablePeriod)
445+
unsigned int TA_GetUnstablePeriod(TA_FuncUnstId id)

talib/test_func.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ def test_input_nans():
2929
assert_np_arrays_equal(r1, [np.nan, np.nan, np.nan, np.nan, 0, 0, 0, 0, 0, 0])
3030
assert_np_arrays_equal(r2, [np.nan, np.nan, np.nan, np.nan, 100, 100, 100, 100, 100, 100])
3131

32+
def test_unstable_period():
33+
a = np.arange(10, dtype=float)
34+
r = func.EMA(a, 3)
35+
assert_np_arrays_equal(r, [np.nan, np.nan, 1, 2, 3, 4, 5, 6, 7, 8])
36+
talib.set_unstable_period('EMA', 5)
37+
r = func.EMA(a, 3)
38+
assert_np_arrays_equal(r, [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 6, 7, 8])
39+
talib.set_unstable_period('EMA', 0)
40+
3241
def test_MIN():
3342
result = func.MIN(series, timeperiod=4)
3443
i = np.where(~np.isnan(result))[0][0]

0 commit comments

Comments
 (0)