From f47b8ad37168b5de00a5beb6f519a4707446f7f8 Mon Sep 17 00:00:00 2001 From: Kevin Johnson Date: Mon, 18 May 2020 16:07:05 -0700 Subject: [PATCH] ENH added indicator Weighted Closing Price (wcp) --- README.md | 4 +++- pandas_ta/__init__.py | 1 + pandas_ta/core.py | 9 +++++++++ pandas_ta/overlap/wcp.py | 23 +++++++++++++++++++++++ setup.py | 2 +- tests/test_indicator_overlap.py | 15 +++++++++++++++ tests/test_indicator_overlap_ext.py | 5 +++++ 7 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 pandas_ta/overlap/wcp.py diff --git a/README.md b/README.md index 6f9b03f..d7bbc13 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ All the indicators return a named Series or a DataFrame in uppercase underscore - __KDJ__ (kdj) - __Parabolic Stop and Reverse__ (psar) - __Psycholigical Line__ (psl) + - __Weighted Closing Price__ (wcp) * User Added Indicators: - __Aberration__ (aberration) - __BRAR__ (brar) @@ -148,7 +149,7 @@ df.ta.adjusted = None |:--------:| | ![Example MACD](/images/SPY_MACD.png) | -## _Overlap_ (24) +## _Overlap_ (25) * _Double Exponential Moving Average_: **dema** * _Exponential Moving Average_: **ema** @@ -174,6 +175,7 @@ df.ta.adjusted = None * _Triangular Moving Average_: **trima** * _Volume Weighted Average Price_: **vwap** * _Volume Weighted Moving Average_: **vwma** +* _Weighted Closing Price_: **wcp** * _Weighted Moving Average_: **wma** * _Zero Lag Moving Average_: **zlma** diff --git a/pandas_ta/__init__.py b/pandas_ta/__init__.py index 0aa7139..27a5628 100644 --- a/pandas_ta/__init__.py +++ b/pandas_ta/__init__.py @@ -68,6 +68,7 @@ from .overlap.tema import tema from .overlap.trima import trima from .overlap.vwap import vwap from .overlap.vwma import vwma +from .overlap.wcp import wcp from .overlap.wma import wma from .overlap.zlma import zlma diff --git a/pandas_ta/core.py b/pandas_ta/core.py index 6eb579d..be33f0c 100644 --- a/pandas_ta/core.py +++ b/pandas_ta/core.py @@ -627,6 +627,15 @@ class AnalysisIndicators(BasePandasObject): self._append(result, **kwargs) return result + def wcp(self, high=None, low=None, close=None, offset=None, **kwargs): + high = self._get_column(high, 'high') + low = self._get_column(low, 'low') + close = self._get_column(close, 'close') + from .overlap.wcp import wcp + result = wcp(high=high, low=low, close=close, offset=offset, **kwargs) + self._append(result, **kwargs) + return result + def wma(self, close=None, length=None, offset=None, **kwargs): close = self._get_column(close, 'close') from .overlap.wma import wma diff --git a/pandas_ta/overlap/wcp.py b/pandas_ta/overlap/wcp.py new file mode 100644 index 0000000..541319e --- /dev/null +++ b/pandas_ta/overlap/wcp.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +from ..utils import get_offset, verify_series + +def wcp(high, low, close, offset=None, **kwargs): + """Indicator: WCP""" + # Validate Arguments + high = verify_series(high) + low = verify_series(low) + close = verify_series(close) + offset = get_offset(offset) + + # Calculate Result + wcp = (high + low + 2 * close) / 4 + + # Offset + if offset != 0: + wcp = wcp.shift(offset) + + # Name & Category + wcp.name = "WCP" + wcp.category = 'overlap' + + return wcp \ No newline at end of file diff --git a/setup.py b/setup.py index 230787f..828b472 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ long_description = "An easy to use Python 3 Pandas Extension with 95+ Technical setup( name ="pandas_ta", packages =['pandas_ta', 'pandas_ta.momentum', 'pandas_ta.overlap', 'pandas_ta.performance', 'pandas_ta.statistics', 'pandas_ta.trend', 'pandas_ta.volatility', 'pandas_ta.volume'], - version ="0.1.42b", + version ="0.1.43b", description =long_description, long_description =long_description, author ="Kevin Johnson", diff --git a/tests/test_indicator_overlap.py b/tests/test_indicator_overlap.py index 270978b..6d8526f 100644 --- a/tests/test_indicator_overlap.py +++ b/tests/test_indicator_overlap.py @@ -298,6 +298,21 @@ class TestOverlap(TestCase): self.assertIsInstance(result, Series) self.assertEqual(result.name, 'VWMA_10') + def test_wcp(self): + result = pandas_ta.wcp(self.high, self.low, self.close) + self.assertIsInstance(result, Series) + self.assertEqual(result.name, 'WCP') + + try: + expected = tal.WCLPRICE(self.high, self.low, self.close) + pdt.assert_series_equal(result, expected, check_names=False) + except AssertionError as ae: + try: + corr = pandas_ta.utils.df_error_analysis(result, expected, col=CORRELATION) + self.assertGreater(corr, CORRELATION_THRESHOLD) + except Exception as ex: + error_analysis(result, CORRELATION, ex) + def test_wma(self): result = pandas_ta.wma(self.close) self.assertIsInstance(result, Series) diff --git a/tests/test_indicator_overlap_ext.py b/tests/test_indicator_overlap_ext.py index f911ef9..6015116 100644 --- a/tests/test_indicator_overlap_ext.py +++ b/tests/test_indicator_overlap_ext.py @@ -133,6 +133,11 @@ class TestOverlapExtension(TestCase): self.assertIsInstance(self.data, DataFrame) self.assertEqual(self.data.columns[-1], 'VWMA_10') + def test_wcp_ext(self): + self.data.ta.wcp(append=True) + self.assertIsInstance(self.data, DataFrame) + self.assertEqual(self.data.columns[-1], 'WCP') + def test_wma_ext(self): self.data.ta.wma(append=True) self.assertIsInstance(self.data, DataFrame)