Files
OpenBB/bots/stocks/technical_analysis/donchian.py
Colin Delahunty fae93c67ad Refactor Bot (#1326)
* First commit

* Renamed to bot

* change from discordbot to bots imports

* refractor cmds

+ autocomplete helpers
+ updated dps plots/candle
+ quote cmd

* isort fix

* Update helpers.py

* Added first refactor

* Added commands and refactor

* Added description handler

* Through grains

* ta + image border helper

* Added indices

* merged

* Finished econ

* options refactor

* dark_pool_shorts, candle, quote done

* pagination/dropdown logic to helpers + fixes

* dd/disc/insider done + add image logic to helpers

* linting

* removed plt style

* Almost done with Gov

* fixing conflicts

* Finished gov (excpect ones mentioned)

* screener done

* Test bug refactor

* Store changes to switch

* Finished refactor

* Fixed error

* Delete ::40

* Fixed issue

* Fixed black

* finished

* fixes

Co-authored-by: teh_coderer <me@tehcoderer.com>
Co-authored-by: didierlopes.eth <dro.lopes@campus.fct.unl.pt>
2022-02-20 10:36:59 -05:00

97 lines
2.8 KiB
Python

from datetime import datetime, timedelta
from matplotlib import pyplot as plt
import bots.config_discordbot as cfg
import bots.helpers
from bots.config_discordbot import logger
from bots.helpers import image_border
from gamestonk_terminal.common.technical_analysis import volatility_model
from gamestonk_terminal.config_plot import PLOT_DPI
from gamestonk_terminal.helper_funcs import plot_autoscale
def donchian_command(
ticker="", upper_length="25", lower_length="100", start="", end=""
):
"""Displays chart with donchian channel [Yahoo Finance]"""
# Debug
if cfg.DEBUG:
logger.debug(
"ta donchian %s %s %s %s %s",
ticker,
upper_length,
lower_length,
start,
end,
)
# Check for argument
if ticker == "":
raise Exception("Stock ticker is required")
if start == "":
start = datetime.now() - timedelta(days=365)
else:
start = datetime.strptime(start, cfg.DATE_FORMAT)
if end == "":
end = datetime.now()
else:
end = datetime.strptime(end, cfg.DATE_FORMAT)
if not upper_length.lstrip("-").isnumeric():
raise Exception("Number has to be an integer")
upper_length = float(upper_length)
if not lower_length.lstrip("-").isnumeric():
raise Exception("Number has to be an integer")
lower_length = float(lower_length)
ticker = ticker.upper()
df_stock = bots.helpers.load(ticker, start)
if df_stock.empty:
raise Exception("Stock ticker is invalid")
# Retrieve Data
df_stock = df_stock.loc[(df_stock.index >= start) & (df_stock.index < end)]
df_ta = volatility_model.donchian(
df_stock["High"], df_stock["Low"], upper_length, lower_length
)
# Output Data
fig, ax = plt.subplots(figsize=plot_autoscale(), dpi=PLOT_DPI)
ax.plot(df_stock.index, df_stock["Adj Close"].values, color="k", lw=3)
ax.plot(df_ta.index, df_ta.iloc[:, 0].values, "b", lw=1.5, label="upper")
ax.plot(df_ta.index, df_ta.iloc[:, 1].values, "b", lw=1.5, ls="--")
ax.plot(df_ta.index, df_ta.iloc[:, 2].values, "b", lw=1.5, label="lower")
ax.set_title(f"{ticker} donchian")
ax.set_xlim(df_stock.index[0], df_stock.index[-1])
ax.set_xlabel("Time")
ax.set_ylabel("Price ($)")
ax.legend([ticker, df_ta.columns[0], df_ta.columns[1], df_ta.columns[2]])
ax.fill_between(
df_ta.index,
df_ta.iloc[:, 0].values,
df_ta.iloc[:, 2].values,
alpha=0.1,
color="b",
)
ax.grid(b=True, which="major", color="#666666", linestyle="-")
plt.gcf().autofmt_xdate()
fig.tight_layout(pad=1)
plt.legend()
imagefile = "ta_donchian.png"
plt.savefig(imagefile)
imagefile = image_border(imagefile)
return {
"title": f"Stocks: Donchian-Channels {ticker}",
"imagefile": imagefile,
}