mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-30 05:03:54 +00:00
@@ -111,7 +111,7 @@ def kc(
|
||||
|
||||
Returns
|
||||
-------
|
||||
df_en : pd.DataFrame
|
||||
pd.DataFrame
|
||||
Dataframe of rolling kc
|
||||
"""
|
||||
# Daily
|
||||
@@ -126,3 +126,46 @@ def kc(
|
||||
offset=offset,
|
||||
).dropna()
|
||||
)
|
||||
|
||||
|
||||
@log_start_end(log=logger)
|
||||
def atr(
|
||||
high_prices: pd.Series,
|
||||
low_prices: pd.Series,
|
||||
close_prices: pd.Series,
|
||||
window: int = 14,
|
||||
mamode: str = "ema",
|
||||
offset: int = 0,
|
||||
) -> pd.DataFrame:
|
||||
"""Average True Range
|
||||
|
||||
Parameters
|
||||
----------
|
||||
high_prices : pd.DataFrame
|
||||
High prices
|
||||
low_prices : pd.DataFrame
|
||||
Low prices
|
||||
close_prices : pd.DataFrame
|
||||
Close prices
|
||||
window : int
|
||||
Length of window
|
||||
mamode: str
|
||||
Type of filter
|
||||
offset : int
|
||||
Offset value
|
||||
|
||||
Returns
|
||||
-------
|
||||
pd.DataFrame
|
||||
Dataframe of atr
|
||||
"""
|
||||
return pd.DataFrame(
|
||||
ta.atr(
|
||||
high=high_prices,
|
||||
low=low_prices,
|
||||
close=close_prices,
|
||||
length=window,
|
||||
mamode=mamode,
|
||||
offset=offset,
|
||||
).dropna()
|
||||
)
|
||||
|
||||
@@ -277,3 +277,80 @@ def view_kc(
|
||||
"kc",
|
||||
df_ta,
|
||||
)
|
||||
|
||||
|
||||
@log_start_end(log=logger)
|
||||
def display_atr(
|
||||
data: pd.DataFrame,
|
||||
symbol: str = "",
|
||||
window: int = 14,
|
||||
mamode: str = "sma",
|
||||
offset: int = 0,
|
||||
export: str = "",
|
||||
external_axes: Optional[List[plt.Axes]] = None,
|
||||
):
|
||||
"""Show ATR
|
||||
|
||||
Parameters
|
||||
----------
|
||||
data : pd.DataFrame
|
||||
Dataframe of ohlc prices
|
||||
symbol : str
|
||||
Ticker symbol
|
||||
window : int
|
||||
Length of window to calculate upper channel
|
||||
export : str
|
||||
Format of export file
|
||||
external_axes : Optional[List[plt.Axes]], optional
|
||||
External axes (1 axis is expected in the list), by default None
|
||||
"""
|
||||
df_ta = volatility_model.atr(
|
||||
data["High"],
|
||||
data["Low"],
|
||||
data["Adj Close"],
|
||||
window=window,
|
||||
mamode=mamode,
|
||||
offset=offset,
|
||||
)
|
||||
|
||||
# This plot has 2 axes
|
||||
if external_axes is None:
|
||||
_, axes = plt.subplots(
|
||||
2, 1, figsize=plot_autoscale(), sharex=True, dpi=PLOT_DPI
|
||||
)
|
||||
(ax1, ax2) = axes
|
||||
elif is_valid_axes_count(external_axes, 2):
|
||||
(ax1, ax2) = external_axes
|
||||
else:
|
||||
return
|
||||
|
||||
plot_data = pd.merge(data, df_ta, how="outer", left_index=True, right_index=True)
|
||||
plot_data = reindex_dates(plot_data)
|
||||
|
||||
ax1.plot(plot_data.index, plot_data.iloc[:, 1].values)
|
||||
ax1.set_title(f"{symbol} ATR")
|
||||
ax1.set_xlim(plot_data.index[0], plot_data.index[-1])
|
||||
ax1.set_ylabel("Share Price ($)")
|
||||
theme.style_primary_axis(
|
||||
ax=ax1,
|
||||
data_index=plot_data.index.to_list(),
|
||||
tick_labels=plot_data["date"].to_list(),
|
||||
)
|
||||
|
||||
ax2.plot(plot_data.index, plot_data[df_ta.columns[0]].values)
|
||||
ax2.set_xlim(plot_data.index[0], plot_data.index[-1])
|
||||
ax2.set_ylabel("ATR")
|
||||
theme.style_primary_axis(
|
||||
ax=ax2,
|
||||
data_index=plot_data.index.to_list(),
|
||||
tick_labels=plot_data["date"].to_list(),
|
||||
)
|
||||
if external_axes is None:
|
||||
theme.visualize_output()
|
||||
|
||||
export_data(
|
||||
export,
|
||||
os.path.dirname(os.path.abspath(__file__)).replace("common", "stocks"),
|
||||
"atr",
|
||||
df_ta,
|
||||
)
|
||||
|
||||
1
openbb_terminal/miscellaneous/i18n/en.yml
vendored
1
openbb_terminal/miscellaneous/i18n/en.yml
vendored
@@ -425,6 +425,7 @@ en:
|
||||
stocks/ta/bbands: bollinger bands
|
||||
stocks/ta/donchian: donchian channels
|
||||
stocks/ta/kc: keltner channels
|
||||
stocks/ta/atr: average true range
|
||||
stocks/ta/_volume_: Volume
|
||||
stocks/ta/ad: accumulation/distribution line
|
||||
stocks/ta/adosc: chaikin oscillator
|
||||
|
||||
@@ -326,6 +326,10 @@ functions = {
|
||||
"model": "openbb_terminal.common.technical_analysis.volume_model.obv",
|
||||
"view": "openbb_terminal.common.technical_analysis.volume_view.display_obv",
|
||||
},
|
||||
"common.ta.atr": {
|
||||
"model": "openbb_terminal.common.technical_analysis.volatility_model.atr",
|
||||
"view": "openbb_terminal.common.technical_analysis.volatility_view.display_atr",
|
||||
},
|
||||
"crypto.defi.vaults": {
|
||||
"model": "openbb_terminal.cryptocurrency.defi.coindix_model.get_defi_vaults",
|
||||
"view": "openbb_terminal.cryptocurrency.defi.coindix_view.display_defi_vaults",
|
||||
|
||||
@@ -79,6 +79,7 @@ class TechnicalAnalysisController(StockBaseController):
|
||||
"tv",
|
||||
"clenow",
|
||||
"demark",
|
||||
"atr",
|
||||
]
|
||||
PATH = "/stocks/ta/"
|
||||
|
||||
@@ -277,6 +278,7 @@ class TechnicalAnalysisController(StockBaseController):
|
||||
mt.add_cmd("bbands")
|
||||
mt.add_cmd("donchian")
|
||||
mt.add_cmd("kc")
|
||||
mt.add_cmd("atr")
|
||||
mt.add_info("_volume_")
|
||||
mt.add_cmd("ad")
|
||||
mt.add_cmd("adosc")
|
||||
@@ -1579,3 +1581,57 @@ class TechnicalAnalysisController(StockBaseController):
|
||||
min_to_show=ns_parser.min_to_show,
|
||||
export=ns_parser.export,
|
||||
)
|
||||
|
||||
@log_start_end(log=logger)
|
||||
def call_atr(self, other_args: List[str]):
|
||||
"""Process atr command"""
|
||||
parser = argparse.ArgumentParser(
|
||||
add_help=False,
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
||||
prog="atr",
|
||||
description="""
|
||||
Averge True Range is used to measure volatility, especially volatility caused by
|
||||
gaps or limit moves.
|
||||
""",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-l",
|
||||
"--length",
|
||||
action="store",
|
||||
dest="n_length",
|
||||
type=check_positive,
|
||||
default=14,
|
||||
help="Window length",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-m",
|
||||
"--mamode",
|
||||
action="store",
|
||||
dest="s_mamode",
|
||||
default="ema",
|
||||
choices=volatility_model.MAMODES,
|
||||
help="mamode",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-o",
|
||||
"--offset",
|
||||
action="store",
|
||||
dest="n_offset",
|
||||
type=int,
|
||||
default=0,
|
||||
help="offset",
|
||||
)
|
||||
|
||||
ns_parser = self.parse_known_args_and_warn(
|
||||
parser, other_args, EXPORT_BOTH_RAW_DATA_AND_FIGURES
|
||||
)
|
||||
|
||||
if ns_parser:
|
||||
volatility_view.display_atr(
|
||||
data=self.stock,
|
||||
symbol=self.ticker,
|
||||
window=ns_parser.n_length,
|
||||
mamode=ns_parser.s_mamode,
|
||||
offset=ns_parser.n_offset,
|
||||
export=ns_parser.export,
|
||||
)
|
||||
|
||||
@@ -608,6 +608,20 @@ def test_call_func_expect_queue(expected_queue, func, queue):
|
||||
export="csv",
|
||||
),
|
||||
),
|
||||
(
|
||||
"call_atr",
|
||||
["--mamode=sma", "--export=csv"],
|
||||
"volatility_view.display_atr",
|
||||
[],
|
||||
dict(
|
||||
symbol="MOCK_TICKER",
|
||||
data=MOCK_STOCK_DF,
|
||||
mamode="sma",
|
||||
window=14,
|
||||
offset=0,
|
||||
export="csv",
|
||||
),
|
||||
),
|
||||
(
|
||||
"call_donchian",
|
||||
[
|
||||
|
||||
@@ -29,6 +29,7 @@ Volatility:
|
||||
bbands bollinger bands
|
||||
donchian donchian channels
|
||||
kc keltner channels
|
||||
atr average true range
|
||||
Volume:
|
||||
ad accumulation/distribution line
|
||||
adosc chaikin oscillator
|
||||
|
||||
@@ -29,6 +29,7 @@ Volatility:
|
||||
bbands bollinger bands
|
||||
donchian donchian channels
|
||||
kc keltner channels
|
||||
atr average true range
|
||||
Volume:
|
||||
ad accumulation/distribution line
|
||||
adosc chaikin oscillator
|
||||
|
||||
Reference in New Issue
Block a user