Skip to content

Commit

Permalink
Fixes #3873 and adds generic naming to financial statements (#4142)
Browse files Browse the repository at this point in the history
* Fix plotting error

I choose to fix it by removing the denomination transform, since it doesn't affect the result (even when fixed).

* Add generic naming to financial statements, except for EODHD source

* Fix linting

* Fix linting

* Fix bugs

* Fix linting

* Handle invalid plot options for sources

* Fix linting

* Fix linting

* Some tests

* more tests

---------

Co-authored-by: James Maslek <jmaslek11@gmail.com>
  • Loading branch information
northern-64bit and jmaslek authored Feb 20, 2023
1 parent 6e5b399 commit 33e79a2
Show file tree
Hide file tree
Showing 26 changed files with 1,126 additions and 1,304 deletions.
84 changes: 34 additions & 50 deletions openbb_terminal/stocks/fundamental_analysis/av_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
from openbb_terminal.config_terminal import theme
from openbb_terminal.decorators import check_api_key, log_start_end
from openbb_terminal.helper_funcs import (
camel_case_split,
export_data,
is_valid_axes_count,
plot_autoscale,
print_rich_table,
)
from openbb_terminal.helpers_denomination import transform as transform_by_denomination
from openbb_terminal.rich_config import console
from openbb_terminal.stocks import stocks_helper
from openbb_terminal.stocks.fundamental_analysis import av_model

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -122,41 +121,36 @@ def display_income_statement(
if df_income.empty:
return

df_income.index = [
stocks_helper.INCOME_PLOT["AlphaVantage"][i]
for i in [i.replace(" ", "_") for i in df_income.index.str.lower()]
]

if plot:
rows_plot = len(plot)
income_plot_data = df_income.transpose()
income_plot_data.columns = income_plot_data.columns.str.lower()

if not ratios:
(df_rounded, denomination) = transform_by_denomination(income_plot_data)
if denomination == "Units":
denomination = ""
else:
df_rounded = income_plot_data
denomination = ""

if rows_plot == 1:
fig, ax = plt.subplots(figsize=plot_autoscale(), dpi=PLOT_DPI)
df_rounded[plot[0].replace("_", "")].plot()
income_plot_data[plot[0]].plot()
title = (
f"{plot[0].replace('_', ' ').lower()} {'QoQ' if quarterly else 'YoY'} Growth of {symbol.upper()}"
if ratios
else f"{plot[0].replace('_', ' ')} of {symbol.upper()} {denomination}"
else f"{plot[0].replace('_', ' ')} of {symbol.upper()}"
)
plt.title(title)
theme.style_primary_axis(ax)
theme.visualize_output()
else:
fig, axes = plt.subplots(rows_plot)
for i in range(rows_plot):
axes[i].plot(df_rounded[plot[i].replace("_", "")])
axes[i].set_title(f"{plot[i].replace('_', ' ')} {denomination}")
axes[i].plot(income_plot_data[plot[i]])
axes[i].set_title(f"{plot[i].replace('_', ' ')}")
theme.style_primary_axis(axes[0])
fig.autofmt_xdate()
else:
indexes = df_income.index
new_indexes = [camel_case_split(ind) for ind in indexes]
df_income.index = new_indexes
# Snake case to english
df_income.index = [x.replace("_", " ").title() for x in df_income.index]

print_rich_table(
df_income,
Expand Down Expand Up @@ -213,42 +207,37 @@ def display_balance_sheet(
if df_balance.empty:
return

df_balance.index = [
stocks_helper.BALANCE_PLOT["AlphaVantage"][i]
for i in [i.replace(" ", "_") for i in df_balance.index.str.lower()]
]

if plot:
rows_plot = len(plot)
balance_plot_data = df_balance.transpose()
balance_plot_data.columns = balance_plot_data.columns.str.lower()

if not ratios:
(df_rounded, denomination) = transform_by_denomination(balance_plot_data)
if denomination == "Units":
denomination = ""
else:
df_rounded = balance_plot_data
denomination = ""

if rows_plot == 1:
fig, ax = plt.subplots(figsize=plot_autoscale(), dpi=PLOT_DPI)
df_rounded[plot[0].replace("_", "")].plot()
balance_plot_data[plot[0]].plot()
title = (
f"{plot[0].replace('_', ' ').lower()} {'QoQ' if quarterly else 'YoY'} Growth of {symbol.upper()}"
if ratios
else f"{plot[0].replace('_', ' ')} of {symbol.upper()} {denomination}"
else f"{plot[0].replace('_', ' ')} of {symbol.upper()}"
)
plt.title(title)
theme.style_primary_axis(ax)
theme.visualize_output()
else:
fig, axes = plt.subplots(rows_plot)
for i in range(rows_plot):
axes[i].plot(df_rounded[plot[i].replace("_", "")])
axes[i].set_title(f"{plot[i].replace('_', ' ')} {denomination}")
axes[i].plot(balance_plot_data[plot[i]])
axes[i].set_title(f"{plot[i].replace('_', ' ')}")
theme.style_primary_axis(axes[0])
fig.autofmt_xdate()

else:
indexes = df_balance.index
new_indexes = [camel_case_split(ind) for ind in indexes]
df_balance.index = new_indexes
# Snake case to english
df_balance.index = [x.replace("_", " ").title() for x in df_balance.index]

print_rich_table(
df_balance,
Expand Down Expand Up @@ -303,42 +292,37 @@ def display_cash_flow(
if df_cash.empty:
return

df_cash.index = [
stocks_helper.CASH_PLOT["AlphaVantage"][i]
for i in [i.replace(" ", "_") for i in df_cash.index.str.lower()]
]

if plot:
rows_plot = len(plot)
cash_plot_data = df_cash.transpose()
cash_plot_data.columns = cash_plot_data.columns.str.lower()

if not ratios:
(df_rounded, denomination) = transform_by_denomination(cash_plot_data)
if denomination == "Units":
denomination = ""
else:
df_rounded = cash_plot_data
denomination = ""

if rows_plot == 1:
fig, ax = plt.subplots(figsize=plot_autoscale(), dpi=PLOT_DPI)
df_rounded[plot[0].replace("_", "")].plot()
cash_plot_data[plot[0]].plot()
title = (
f"{plot[0].replace('_', ' ').lower()} {'QoQ' if quarterly else 'YoY'} Growth of {symbol.upper()}"
if ratios
else f"{plot[0].replace('_', ' ')} of {symbol.upper()} {denomination}"
else f"{plot[0].replace('_', ' ')} of {symbol.upper()}"
)
plt.title(title)
theme.style_primary_axis(ax)
theme.visualize_output()
else:
fig, axes = plt.subplots(rows_plot)
for i in range(rows_plot):
axes[i].plot(df_rounded[plot[i].replace("_", "")])
axes[i].set_title(f"{plot[i].replace('_', ' ')} {denomination}")
axes[i].plot(cash_plot_data[plot[i]])
axes[i].set_title(f"{plot[i].replace('_', ' ')}")
theme.style_primary_axis(axes[0])
fig.autofmt_xdate()

else:
indexes = df_cash.index
new_indexes = [camel_case_split(ind) for ind in indexes]
df_cash.index = new_indexes
# Snake case to english
df_cash.index = [x.replace("_", " ").title() for x in df_cash.index]

print_rich_table(
df_cash,
Expand Down
18 changes: 15 additions & 3 deletions openbb_terminal/stocks/fundamental_analysis/fa_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ def call_income(self, other_args: List[str]):
"--plot",
action="store",
metavar="column",
choices=stocks_helper.INCOME_PLOT[self.default_income],
choices=stocks_helper.INCOME_PLOT_CHOICES,
type=str,
default=None,
dest="plot",
Expand All @@ -910,6 +910,10 @@ def call_income(self, other_args: List[str]):
"[red]Quarterly data currently unavailable for yfinance"
", showing yearly.[/red]\n"
)
if stocks_helper.verify_plot_options(
"income", ns_parser.source, ns_parser.plot
):
return
if ns_parser.source == "AlphaVantage":
av_view.display_income_statement(
symbol=self.ticker,
Expand Down Expand Up @@ -1024,7 +1028,7 @@ def call_balance(self, other_args: List[str]):
"-p",
"--plot",
action="store",
choices=stocks_helper.BALANCE_PLOT[self.default_balance],
choices=stocks_helper.BALANCE_PLOT_CHOICES,
type=str,
metavar="column",
default=None,
Expand All @@ -1047,6 +1051,10 @@ def call_balance(self, other_args: List[str]):
if ns_parser.source == "YahooFinance" and ns_parser.b_quarter:
text = "Quarterly data currently unavailable for yfinance"
console.print(f"[red]{text}, showing yearly.[/red]\n")
if stocks_helper.verify_plot_options(
"balance", ns_parser.source, ns_parser.plot
):
return
if ns_parser.source == "AlphaVantage":
av_view.display_balance_sheet(
symbol=self.ticker,
Expand Down Expand Up @@ -1169,7 +1177,7 @@ def call_cash(self, other_args: List[str]):
"--plot",
action="store",
type=str,
choices=stocks_helper.CASH_PLOT[self.default_cash],
choices=stocks_helper.CASH_PLOT_CHOICES,
metavar="column",
default=None,
dest="plot",
Expand All @@ -1190,6 +1198,10 @@ def call_cash(self, other_args: List[str]):
if ns_parser.source == "YahooFinance" and ns_parser.b_quarter:
text = "Quarterly data currently unavailable for yfinance"
console.print(f"[red]{text}, showing yearly.[/red]\n")
if stocks_helper.verify_plot_options(
"cash", ns_parser.source, ns_parser.plot
):
return
if ns_parser.source == "AlphaVantage":
av_view.display_cash_flow(
symbol=self.ticker,
Expand Down
3 changes: 3 additions & 0 deletions openbb_terminal/stocks/fundamental_analysis/fmp_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ def get_income(

df_fa = df_fa.iloc[:, 0:limit]
df_fa_c = clean_metrics_df(df_fa, num=limit)
df_fa.index = df_fa_c.index

return df_fa_c if not plot else df_fa

Expand Down Expand Up @@ -316,6 +317,7 @@ def get_balance(

df_fa = df_fa.iloc[:, 0:limit]
df_fa_c = clean_metrics_df(df_fa, num=limit)
df_fa.index = df_fa_c.index

return df_fa_c if not plot else df_fa

Expand Down Expand Up @@ -384,6 +386,7 @@ def get_cash(

df_fa = df_fa.iloc[:, 0:limit]
df_fa_c = clean_metrics_df(df_fa, num=limit)
df_fa.index = df_fa_c.index

return df_fa_c if not plot else df_fa

Expand Down
Loading

0 comments on commit 33e79a2

Please sign in to comment.