added long_run indicator and tests

This commit is contained in:
Kevin Johnson
2019-04-24 11:19:02 -07:00
parent 6e2a6d278b
commit 82d0d25bd2
5 changed files with 57 additions and 4 deletions
+2 -1
View File
@@ -163,13 +163,14 @@ Use parameter: cumulative=**True** for cumulative results.
|:--------:|
| ![Example Z Score](/images/SPY_ZScore.png) |
## _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
View File
@@ -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}")
+30
View File
@@ -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
+5
View File
@@ -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)
+10
View File
@@ -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)