mirror of
https://github.com/wassname/pandas-ta.git
synced 2026-08-07 11:25:53 +08:00
linear_decay indicator added
This commit is contained in:
@@ -163,7 +163,7 @@ Use parameter: cumulative=**True** for cumulative results.
|
||||
|:--------:|
|
||||
|  |
|
||||
|
||||
## _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**
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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.
|
||||
"""
|
||||
@@ -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",
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user