Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 50 additions & 50 deletions talib/_abstract.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ cimport _ta_lib as lib
np.import_array() # Initialize the NumPy C API

# lookup for TALIB input parameters which don't define expected price series inputs
__INPUT_PRICE_SERIES_DEFAULTS = {'price': 'close',
'price0': 'high',
'price1': 'low',
'periods': 'periods', # only used by MAVP; not a price series!
}
_INPUT_PRICE_SERIES_DEFAULTS = {'price': 'close',
'price0': 'high',
'price1': 'low',
'periods': 'periods', # only used by MAVP; not a price series!
}

__INPUT_ARRAYS_TYPES = [dict]
__ARRAY_TYPES = [np.ndarray]
_INPUT_ARRAYS_TYPES = [dict]
_ARRAY_TYPES = [np.ndarray]

# allow use of pandas.DataFrame for input arrays
try:
import pandas
__INPUT_ARRAYS_TYPES.append(pandas.DataFrame)
__ARRAY_TYPES.append(pandas.Series)
__PANDAS_DATAFRAME = pandas.DataFrame
__PANDAS_SERIES = pandas.Series
_INPUT_ARRAYS_TYPES.append(pandas.DataFrame)
_ARRAY_TYPES.append(pandas.Series)
_PANDAS_DATAFRAME = pandas.DataFrame
_PANDAS_SERIES = pandas.Series
except ImportError as import_error:
try:
if not isinstance(import_error, ModuleNotFoundError) or import_error.name != 'pandas':
Expand All @@ -42,16 +42,16 @@ except ImportError as import_error:
except NameError:
pass

__PANDAS_DATAFRAME = None
__PANDAS_SERIES = None
_PANDAS_DATAFRAME = None
_PANDAS_SERIES = None

# allow use of polars.DataFrame for input arrays
try:
import polars
__INPUT_ARRAYS_TYPES.append(polars.DataFrame)
__ARRAY_TYPES.append(polars.Series)
__POLARS_DATAFRAME = polars.DataFrame
__POLARS_SERIES = polars.Series
_INPUT_ARRAYS_TYPES.append(polars.DataFrame)
_ARRAY_TYPES.append(polars.Series)
_POLARS_DATAFRAME = polars.DataFrame
_POLARS_SERIES = polars.Series
except ImportError as import_error:
try:
if not isinstance(import_error, ModuleNotFoundError) or import_error.name != 'polars':
Expand All @@ -61,11 +61,11 @@ except ImportError as import_error:
except NameError:
pass

__POLARS_DATAFRAME = None
__POLARS_SERIES = None
_POLARS_DATAFRAME = None
_POLARS_SERIES = None

__INPUT_ARRAYS_TYPES = tuple(__INPUT_ARRAYS_TYPES)
__ARRAY_TYPES = tuple(__ARRAY_TYPES)
_INPUT_ARRAYS_TYPES = tuple(_INPUT_ARRAYS_TYPES)
_ARRAY_TYPES = tuple(_ARRAY_TYPES)


if sys.version >= '3':
Expand Down Expand Up @@ -147,7 +147,7 @@ class Function(object):
info = _ta_getInputParameterInfo(self.__name, i)
input_name = info['name']
if info['price_series'] is None:
info['price_series'] = __INPUT_PRICE_SERIES_DEFAULTS[input_name]
info['price_series'] = _INPUT_PRICE_SERIES_DEFAULTS[input_name]
local.input_names[input_name] = info
local.info['input_names'] = self.input_names

Expand Down Expand Up @@ -217,8 +217,8 @@ class Function(object):
Returns a copy of the dict of input arrays in use.
"""
local = self.__local
if __POLARS_DATAFRAME is not None \
and isinstance(local.input_arrays, __POLARS_DATAFRAME):
if _POLARS_DATAFRAME is not None \
and isinstance(local.input_arrays, _POLARS_DATAFRAME):
return local.input_arrays.clone()
else:
return local.input_arrays.copy()
Expand Down Expand Up @@ -249,11 +249,11 @@ class Function(object):
return False
"""
local = self.__local
if isinstance(input_arrays, __INPUT_ARRAYS_TYPES):
if isinstance(input_arrays, _INPUT_ARRAYS_TYPES):
missing_keys = []
for key in self.__input_price_series_names():
if __POLARS_DATAFRAME is not None \
and isinstance(input_arrays, __POLARS_DATAFRAME):
if _POLARS_DATAFRAME is not None \
and isinstance(input_arrays, _POLARS_DATAFRAME):
missing = key not in input_arrays.columns
else:
missing = key not in input_arrays
Expand Down Expand Up @@ -376,22 +376,22 @@ class Function(object):
ret = local.outputs.values()
if not isinstance(ret, list):
ret = list(ret)
if __PANDAS_DATAFRAME is not None and \
isinstance(local.input_arrays, __PANDAS_DATAFRAME):
if _PANDAS_DATAFRAME is not None and \
isinstance(local.input_arrays, _PANDAS_DATAFRAME):
index = local.input_arrays.index
if len(ret) == 1:
return __PANDAS_SERIES(ret[0], index=index)
return _PANDAS_SERIES(ret[0], index=index)
else:
return __PANDAS_DATAFRAME(numpy.column_stack(ret),
index=index,
columns=self.output_names)
elif __POLARS_DATAFRAME is not None and \
isinstance(local.input_arrays, __POLARS_DATAFRAME):
return _PANDAS_DATAFRAME(numpy.column_stack(ret),
index=index,
columns=self.output_names)
elif _POLARS_DATAFRAME is not None and \
isinstance(local.input_arrays, _POLARS_DATAFRAME):
if len(ret) == 1:
return __POLARS_SERIES(ret[0])
return _POLARS_SERIES(ret[0])
else:
return __POLARS_DATAFRAME(numpy.column_stack(ret),
schema=self.output_names)
return _POLARS_DATAFRAME(numpy.column_stack(ret),
schema=self.output_names)
else:
return ret[0] if len(ret) == 1 else ret

Expand Down Expand Up @@ -425,9 +425,9 @@ class Function(object):
args = list(args)
input_arrays = {}
input_price_series_names = self.__input_price_series_names()
if args and not isinstance(args[0], __INPUT_ARRAYS_TYPES):
if args and not isinstance(args[0], _INPUT_ARRAYS_TYPES):
for i, arg in enumerate(args):
if not isinstance(arg, __ARRAY_TYPES):
if not isinstance(arg, _ARRAY_TYPES):
break

try:
Expand All @@ -438,11 +438,11 @@ class Function(object):
', '.join(input_price_series_names))
raise TypeError(msg)

if __PANDAS_DATAFRAME is not None \
and isinstance(local.input_arrays, __PANDAS_DATAFRAME):
if _PANDAS_DATAFRAME is not None \
and isinstance(local.input_arrays, _PANDAS_DATAFRAME):
no_existing_input_arrays = local.input_arrays.empty
elif __POLARS_DATAFRAME is not None \
and isinstance(local.input_arrays, __POLARS_DATAFRAME):
elif _POLARS_DATAFRAME is not None \
and isinstance(local.input_arrays, _POLARS_DATAFRAME):
no_existing_input_arrays = local.input_arrays.is_empty()
else:
no_existing_input_arrays = not bool(local.input_arrays)
Expand All @@ -451,7 +451,7 @@ class Function(object):
self.set_input_arrays(input_arrays)
args = args[len(input_arrays):]
elif len(input_arrays) or (no_existing_input_arrays and (
not len(args) or not isinstance(args[0], __INPUT_ARRAYS_TYPES))):
not len(args) or not isinstance(args[0], _INPUT_ARRAYS_TYPES))):
msg = 'Not enough price arguments: expected %d (%s)' % (
len(input_price_series_names),
', '.join(input_price_series_names))
Expand Down Expand Up @@ -481,7 +481,7 @@ class Function(object):
if isinstance(price_series, list): # TALIB-supplied input names
for name in price_series:
input_price_series_names.append(name)
else: # name came from __INPUT_PRICE_SERIES_DEFAULTS
else: # name came from _INPUT_PRICE_SERIES_DEFAULTS
input_price_series_names.append(price_series)
return input_price_series_names

Expand All @@ -493,11 +493,11 @@ class Function(object):
args = []
for price_series in input_price_series_names:
series = local.input_arrays[price_series]
if __PANDAS_SERIES is not None and \
isinstance(series, __PANDAS_SERIES):
if _PANDAS_SERIES is not None and \
isinstance(series, _PANDAS_SERIES):
series = series.values.astype(float)
elif __POLARS_SERIES is not None and \
isinstance(series, __POLARS_SERIES):
elif _POLARS_SERIES is not None and \
isinstance(series, _POLARS_SERIES):
series = series.to_numpy().astype(float)
args.append(series)
for opt_input in local.opt_inputs:
Expand Down
14 changes: 7 additions & 7 deletions talib/_stream.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ from collections import namedtuple
cimport _ta_lib as lib
from _ta_lib cimport TA_RetCode, TA_MAType, TA_BAD_PARAM
# NOTE: _ta_check_success and InsufficientHistory come from _common.pxi,
# check_array / make_*_array from _func.pxi, and __PANDAS_SERIES /
# __POLARS_SERIES from _abstract.pxi.
# check_array / make_*_array from _func.pxi, and _PANDAS_SERIES /
# _POLARS_SERIES from _abstract.pxi.

np.import_array() # Initialize the NumPy C API

Expand Down Expand Up @@ -2459,7 +2459,7 @@ cdef np.ndarray _stream_input(object values):
"""What the Function API accepts, through the same checks."""
if isinstance(values, np.ndarray):
return check_array(values)
for series in (__PANDAS_SERIES, __POLARS_SERIES):
for series in (_PANDAS_SERIES, _POLARS_SERIES):
if series is not None and isinstance(values, series):
return check_array(values.to_numpy().astype(float))
raise TypeError("input must be a numpy array or a pandas or polars Series, "
Expand All @@ -2484,15 +2484,15 @@ cdef _stream_open_failed(str function_name, TA_RetCode retCode, int historylen,

cdef _stream_like(tuple sources, object result):
pandas = [s for s in sources
if __PANDAS_SERIES is not None and isinstance(s, __PANDAS_SERIES)]
if _PANDAS_SERIES is not None and isinstance(s, _PANDAS_SERIES)]
polars = [s for s in sources
if __POLARS_SERIES is not None and isinstance(s, __POLARS_SERIES)]
if _POLARS_SERIES is not None and isinstance(s, _POLARS_SERIES)]
if pandas and polars:
raise Exception("Cannot mix polars and pandas")
if pandas:
return __PANDAS_SERIES(result, index=pandas[0].index)
return _PANDAS_SERIES(result, index=pandas[0].index)
if polars:
return __POLARS_SERIES(result)
return _POLARS_SERIES(result)
return result


Expand Down
Loading
Loading