From 00906b86f38bf2851abdb4db057c2569c69f08cb Mon Sep 17 00:00:00 2001 From: Kevin Johnson Date: Fri, 30 Oct 2020 09:32:24 -0700 Subject: [PATCH] ENH PR #153 atr tv mamode alignment --- README.md | 1 + pandas_ta/volatility/atr.py | 23 ++++++++++------------- setup.py | 2 +- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 120e58e..1d27e1e 100644 --- a/README.md +++ b/README.md @@ -626,6 +626,7 @@ indicator consisting of two different simple moving averages. See: ```help(ta.tt * _Elder's Thermometer_ (**thermo**) Elder's Thermometer measures price volatility. See: ```help(ta.thermo)``` ## **Updated** +* _Average True Range_ (**atr**): The default ```mamode``` is now "**RMA**" and with the same ```mamode``` options as TradingView. See ```help(ta.atr)```. * _Trend Return_ (**trend_return**): Returns a DataFrame now instead of Series with pertinenet trade info for a _trend_. An example can be found in the [AI Example Notebook](https://github.com/twopirllc/pandas-ta/tree/master/examples/AIExample.ipynb). The notebook is still a work in progress and open to colloboration. diff --git a/pandas_ta/volatility/atr.py b/pandas_ta/volatility/atr.py index c682241..dfc1e4e 100644 --- a/pandas_ta/volatility/atr.py +++ b/pandas_ta/volatility/atr.py @@ -1,34 +1,30 @@ # -*- coding: utf-8 -*- -from pandas_ta.overlap import ema, rma, sma -from pandas_ta.overlap.wma import wma +from pandas_ta.overlap import ema, rma, sma, wma from .true_range import true_range from pandas_ta.utils import get_drift, get_offset, verify_series -def atr(high, low, close, length=None, mamode='rma', drift=None, offset=None, **kwargs): +def atr(high, low, close, length=None, mamode=None, drift=None, offset=None, **kwargs): """Indicator: Average True Range (ATR)""" # Validate arguments high = verify_series(high) low = verify_series(low) close = verify_series(close) length = int(length) if length and length > 0 else 14 - mamode = str(mamode).lower() - + mamode = mamode = mamode.lower() if mamode else "rma" drift = get_drift(drift) offset = get_offset(offset) # Calculate Result + _mode = "" tr = true_range(high=high, low=low, close=close, drift=drift) if mamode == "ema": - # alpha = (1.0 / length) if length > 0 else 0.5 - # atr = tr.ewm(alpha=alpha).mean() - atr = ema(tr, length=length) + atr, _mode = ema(tr, length=length), "ema" elif mamode == "sma": - # atr = tr.rolling(length).mean() - atr = sma(tr, length=length) + atr, _mode = sma(tr, length=length), "sma" elif mamode == "wma": - atr = wma(tr, length=length) - else: + atr, _mode = wma(tr, length=length), "wma" + else: # "rma" atr = rma(tr, length=length) percentage = kwargs.pop("percent", False) @@ -46,7 +42,8 @@ def atr(high, low, close, length=None, mamode='rma', drift=None, offset=None, ** atr.fillna(method=kwargs["fill_method"], inplace=True) # Name and Categorize it - atr.name = f"ATR{'p' if percentage else ''}_{length}" + # mamode_ = + atr.name = f"ATR{_mode}_{length}{'p' if percentage else ''}" atr.category = "volatility" return atr diff --git a/setup.py b/setup.py index ca0b17a..724f8c1 100644 --- a/setup.py +++ b/setup.py @@ -17,7 +17,7 @@ setup( "pandas_ta.volatility", "pandas_ta.volume" ], - version=".".join(("0", "2", "23b")), + version=".".join(("0", "2", "24b")), description=long_description, long_description=long_description, author="Kevin Johnson",