mirror of
https://github.com/wassname/pandas-ta.git
synced 2026-08-04 13:03:59 +08:00
added long_run indicator and tests
This commit is contained in:
@@ -163,13 +163,14 @@ Use parameter: cumulative=**True** for cumulative results.
|
||||
|:--------:|
|
||||
|  |
|
||||
|
||||
## _Trend_ (7)
|
||||
## _Trend_ (8)
|
||||
|
||||
* _Average Directional Movement Index_: **adx**
|
||||
* _Aroon Oscillator_: **aroon**
|
||||
* _Decreasing_: **decreasing**
|
||||
* _Detrended Price Oscillator_: **dpo**
|
||||
* _Increasing_: **increasing**
|
||||
* _Long Run_: **long_run**
|
||||
* _Q Stick_: **qstick**
|
||||
* _Vortex_: **vortex**
|
||||
|
||||
|
||||
+10
-3
@@ -608,6 +608,15 @@ class AnalysisIndicators(BasePandasObject):
|
||||
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:
|
||||
fast = self._get_column(fast, f"{fast}")
|
||||
slow = self._get_column(slow, f"{slow}")
|
||||
result = long_run(fast=fast, slow=slow, length=length, offset=offset, **kwargs)
|
||||
self._append(result, **kwargs)
|
||||
return result
|
||||
|
||||
def qstick(self, open_=None, close=None, length=None, offset=None, **kwargs):
|
||||
open_ = self._get_column(open_, 'open')
|
||||
close = self._get_column(close, 'close')
|
||||
@@ -627,9 +636,7 @@ class AnalysisIndicators(BasePandasObject):
|
||||
|
||||
# Utility Indicators
|
||||
def cross(self, a=None, b=None, above=True, asint=True, offset=None, **kwargs):
|
||||
if a is None and b is None:
|
||||
print(f" [X] Series ")
|
||||
return self._df
|
||||
if a is None and b is None: return self._df
|
||||
else:
|
||||
a = self._get_column(a, f"{a}")
|
||||
b = self._get_column(b, f"{b}")
|
||||
|
||||
@@ -205,6 +205,36 @@ def increasing(close, length=None, asint=True, offset=None, **kwargs):
|
||||
return increasing
|
||||
|
||||
|
||||
def long_run(fast, slow, length=None, offset=None, **kwargs):
|
||||
"""Indicator: Long Run"""
|
||||
# Validate Arguments
|
||||
fast = verify_series(fast)
|
||||
slow = verify_series(slow)
|
||||
length = int(length) if length and length > 0 else 2
|
||||
offset = get_offset(offset)
|
||||
|
||||
# Calculate Result
|
||||
pb = increasing(fast, length) & decreasing(slow, length) # potential bottom or bottom
|
||||
bi = increasing(fast, length) & increasing(slow, length) # fast and slow are increasing
|
||||
long_run = pb | bi
|
||||
|
||||
# Offset
|
||||
if offset != 0:
|
||||
long_run = long_run.shift(offset)
|
||||
|
||||
# Handle fills
|
||||
if 'fillna' in kwargs:
|
||||
long_run.fillna(kwargs['fillna'], inplace=True)
|
||||
if 'fill_method' in kwargs:
|
||||
long_run.fillna(method=kwargs['fill_method'], inplace=True)
|
||||
|
||||
# Name and Categorize it
|
||||
long_run.name = f"LR_{length}"
|
||||
long_run.category = 'trend'
|
||||
|
||||
return long_run
|
||||
|
||||
|
||||
def qstick(open_, close, length=None, offset=None, **kwargs):
|
||||
"""Indicator: Q Stick"""
|
||||
# Validate Arguments
|
||||
|
||||
@@ -89,6 +89,11 @@ class TestTrend(TestCase):
|
||||
self.assertIsInstance(result, Series)
|
||||
self.assertEqual(result.name, 'INC_1')
|
||||
|
||||
def test_long_run(self):
|
||||
result = self.trend.long_run(self.close, self.open)
|
||||
self.assertIsInstance(result, Series)
|
||||
self.assertEqual(result.name, 'LR_2')
|
||||
|
||||
def test_qstick(self):
|
||||
result = self.trend.qstick(self.open, self.close)
|
||||
self.assertIsInstance(result, Series)
|
||||
|
||||
@@ -48,6 +48,16 @@ class TestTrendExtension(TestCase):
|
||||
self.assertIsInstance(self.data, DataFrame)
|
||||
self.assertEqual(self.data.columns[-1], 'INC_1')
|
||||
|
||||
def test_long_run_ext(self):
|
||||
# Nothing passed, return self
|
||||
self.assertEqual(self.data.ta.long_run(append=True).shape, self.data.shape)
|
||||
|
||||
fast = self.data.ta.ema('close', 8)
|
||||
slow = self.data.ta.ema('close', 21)
|
||||
self.data.ta.long_run(fast, slow, append=True)
|
||||
self.assertIsInstance(self.data, DataFrame)
|
||||
self.assertEqual(self.data.columns[-1], 'LR_2')
|
||||
|
||||
def test_qstick_ext(self):
|
||||
self.data.ta.qstick(append=True)
|
||||
self.assertIsInstance(self.data, DataFrame)
|
||||
|
||||
Reference in New Issue
Block a user