diff --git a/README.md b/README.md index fc7126b..b35b91f 100644 --- a/README.md +++ b/README.md @@ -163,7 +163,7 @@ Use parameter: cumulative=**True** for cumulative results. |:--------:| | ![Example Z Score](/images/SPY_ZScore.png) | -## _Trend_ (10) +## _Trend_ (11) * _Average Directional Movement Index_: **adx** * _Archer Moving Averages Trends_: **amat** @@ -171,6 +171,7 @@ Use parameter: cumulative=**True** for cumulative results. * _Decreasing_: **decreasing** * _Detrended Price Oscillator_: **dpo** * _Increasing_: **increasing** +* _Linear Decay_: **linear_decay** * _Long Run_: **long_run** * _Q Stick_: **qstick** * _Short Run_: **short_run** diff --git a/pandas_ta/__init__.py b/pandas_ta/__init__.py index 6e4d6fa..44ee8af 100644 --- a/pandas_ta/__init__.py +++ b/pandas_ta/__init__.py @@ -83,6 +83,7 @@ from .trend.aroon import aroon from .trend.decreasing import decreasing from .trend.dpo import dpo from .trend.increasing import increasing +from .trend.linear_decay import linear_decay from .trend.long_run import long_run from .trend.qstick import qstick from .trend.short_run import short_run diff --git a/pandas_ta/core.py b/pandas_ta/core.py index 4038ea4..abcaef6 100644 --- a/pandas_ta/core.py +++ b/pandas_ta/core.py @@ -660,6 +660,13 @@ class AnalysisIndicators(BasePandasObject): self._append(result, **kwargs) return result + def linear_decay(self, close=None, length=None, offset=None, **kwargs): + close = self._get_column(close, 'close') + from .trend.linear_decay import linear_decay + result = linear_decay(close=close, length=length, offset=offset, **kwargs) + self._append(result, **kwargs) + return result + def long_run(self, fast=None, slow=None, length=None, offset=None, **kwargs): if fast is None and slow is None: return self._df else: diff --git a/pandas_ta/trend/linear_decay.py b/pandas_ta/trend/linear_decay.py new file mode 100644 index 0000000..56d4600 --- /dev/null +++ b/pandas_ta/trend/linear_decay.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- +from pandas import DataFrame +from ..utils import get_offset, verify_series + +def linear_decay(close, length=None, offset=None, **kwargs): + """Indicator: Linear Decay""" + # Validate Arguments + close = verify_series(close) + length = int(length) if length and length > 0 else 5 + offset = get_offset(offset) + + # Calculate Result + diff = close.shift(1) - (1 / length) + diff[0] = close[0] + tdf = DataFrame({'close': close, 'diff': diff, '0': 0}) + ld = tdf.max(axis=1) + + # Offset + if offset != 0: + ld = ld.shift(offset) + + # Handle fills + if 'fillna' in kwargs: + ld.fillna(kwargs['fillna'], inplace=True) + if 'fill_method' in kwargs: + ld.fillna(method=kwargs['fill_method'], inplace=True) + + # Name and Categorize it + ld.name = f"LDECAY_{length}" + ld.category = 'trend' + + return ld + + + +linear_decay.__doc__ = \ +"""Linear Decay + +Adds a linear decay moving forward from prior signals like crosses. + +Sources: + https://tulipindicators.org/decay + +Calculation: + Default Inputs: + length=5 + max(close, close[-1] - (1 / length), 0) + +Args: + close (pd.Series): Series of 'close's + length (int): It's period. Default: 1 + offset (int): How many periods to offset the result. Default: 0 + +Kwargs: + fillna (value, optional): pd.DataFrame.fillna(value) + fill_method (value, optional): Type of fill method + +Returns: + pd.Series: New feature generated. +""" \ No newline at end of file diff --git a/setup.py b/setup.py index 17eb786..50164dc 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ long_description = "An easy to use Python 3 Pandas Extension of Technical Analys setup( name = "pandas_ta", packages = ["pandas_ta"], - version = "0.1.25b", + version = "0.1.26b", description=long_description, long_description=long_description, author = "Kevin Johnson", diff --git a/tests/test_indicator_trend.py b/tests/test_indicator_trend.py index eae31eb..664f053 100644 --- a/tests/test_indicator_trend.py +++ b/tests/test_indicator_trend.py @@ -91,6 +91,11 @@ class TestTrend(TestCase): self.assertIsInstance(result, Series) self.assertEqual(result.name, 'INC_1') + def test_linear_decay(self): + result = pandas_ta.linear_decay(self.close) + self.assertIsInstance(result, Series) + self.assertEqual(result.name, 'LDECAY_5') + def test_long_run(self): result = pandas_ta.long_run(self.close, self.open) self.assertIsInstance(result, Series) diff --git a/tests/test_indicator_trend_ext.py b/tests/test_indicator_trend_ext.py index e6a732c..a8a9734 100644 --- a/tests/test_indicator_trend_ext.py +++ b/tests/test_indicator_trend_ext.py @@ -53,6 +53,11 @@ class TestTrendExtension(TestCase): self.assertIsInstance(self.data, DataFrame) self.assertEqual(self.data.columns[-1], 'INC_1') + def test_linear_decay_ext(self): + self.data.ta.linear_decay(append=True) + self.assertIsInstance(self.data, DataFrame) + self.assertEqual(self.data.columns[-1], 'LDECAY_5') + def test_long_run_ext(self): # Nothing passed, return self self.assertEqual(self.data.ta.long_run(append=True).shape, self.data.shape)