Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Feature Financial Analysis, checks Yahoo if no data at Alpha Advantage. #1984

Merged
merged 38 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
930d229
Added code to look at Yahoo Finance Financials (Income, Balance sheet…
pauljsymonds Jun 24, 2022
c8e1eb4
Merge branch 'main' into fa_uk
pauljsymonds Jun 24, 2022
e331a53
Added code to look at Yahoo Finance Financials (Income, Balance sheet…
pauljsymonds Jun 24, 2022
7bfd4be
change
pauljsymonds Jun 24, 2022
c7f491f
Merge branch 'fa_uk' of github.com:pauljsymonds/OpenBBTerminal into f…
pauljsymonds Jun 24, 2022
4388911
Merge branch 'main' into fa_uk
colin99d Jun 24, 2022
d862560
refactored to keep yahoo fa financials (income, cash, balance) separate.
pauljsymonds Jun 28, 2022
664efcd
Merge branch 'main' into fa_uk
pauljsymonds Jun 28, 2022
6532905
refactored to keep yahoo fa financials (income, cash, balance) separate.
pauljsymonds Jun 28, 2022
1152214
refactored to keep yahoo fa financials (income, cash, balance) separate.
pauljsymonds Jun 28, 2022
3a5ffcc
refactored to keep yahoo fa financials (income, cash, balance) separa…
pauljsymonds Jun 28, 2022
156bb14
Merge branch 'main' into fa_uk
colin99d Jun 28, 2022
c3ad96e
Fixed source bug
colin99d Jun 28, 2022
bea85a6
Merge branch 'main' into fa_uk
pauljsymonds Jun 28, 2022
ff3cedb
empty commit
pauljsymonds Jun 28, 2022
3965129
Merge branch 'fa_uk' of github.com:pauljsymonds/OpenBBTerminal into f…
pauljsymonds Jun 28, 2022
86c9f46
fixed header date issue
pauljsymonds Jun 28, 2022
aeb09c1
Fixed source
colin99d Jun 28, 2022
a31b05a
Merge branch 'fa_uk' of https://github.com/pauljsymonds/OpenBBTermina…
colin99d Jun 28, 2022
236aba9
Merge branch 'main' into fa_uk
colin99d Jun 28, 2022
a333cd0
fixed bugs related to no data returned
pauljsymonds Jun 28, 2022
4e234d9
fixed bugs related to no data returned
pauljsymonds Jun 28, 2022
06907dc
fixed bugs related to no data returned
pauljsymonds Jun 28, 2022
a758659
Added yf to av when no data returned from av
pauljsymonds Jun 28, 2022
1c523f3
Added support for polygon statement of cash flows
colin99d Jun 28, 2022
cd4a756
Merge branch 'fa_uk' of https://github.com/pauljsymonds/OpenBBTermina…
colin99d Jun 28, 2022
9c5b0bf
Fixed issue I created
colin99d Jun 28, 2022
e19c107
fixed pylint error else after return
pauljsymonds Jun 29, 2022
cf30389
fixed pylint error with disable R1732
pauljsymonds Jun 29, 2022
3453b0a
fixed tests relating to av
pauljsymonds Jun 29, 2022
1b095ec
Merge branch 'main' into fa_uk
colin99d Jun 29, 2022
d2530ec
Updated polygon model and fixed tests
colin99d Jun 29, 2022
ec54fd0
Updated polygon model and fixed tests
colin99d Jun 29, 2022
0616466
fixed load GNR in fa after SNR.L in stocks
pauljsymonds Jun 29, 2022
20d5607
fixed load GNR in fa after SNR.L in stocks
pauljsymonds Jun 29, 2022
c39e3fa
Added newline to print
colin99d Jun 29, 2022
9431a2c
Added newlines
colin99d Jun 29, 2022
8db840c
Merge branch 'main' into fa_uk
colin99d Jun 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 51 additions & 28 deletions openbb_terminal/stocks/fundamental_analysis/av_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from openbb_terminal.decorators import log_start_end
from openbb_terminal.helper_funcs import lambda_long_number_format
from openbb_terminal.rich_config import console
from openbb_terminal.stocks.fundamental_analysis import yahoo_finance_model
from openbb_terminal.stocks.stocks_helper import clean_fraction
from openbb_terminal.stocks.fundamental_analysis.fa_helper import clean_df_index

Expand Down Expand Up @@ -45,7 +46,7 @@ def get_overview(ticker: str) -> pd.DataFrame:
else:
# check if json is empty
if not result_json:
console.print("No data found")
console.print("No data found from Alpha Vantage\n")
# Parse json data to dataframe
elif "Note" in result_json:
console.print(result_json["Note"], "\n")
Expand Down Expand Up @@ -108,7 +109,7 @@ def get_key_metrics(ticker: str) -> pd.DataFrame:
else:
# check if json is empty
if not result_json or len(result_json) < 2:
console.print("No data found")
console.print("No data found from Alpha Vantage\n")
return pd.DataFrame()

df_fa = pd.json_normalize(result_json)
Expand Down Expand Up @@ -177,7 +178,16 @@ def get_income_statements(
else:
# check if json is empty
if not response_json:
console.print("No data found")
console.print(
"No data found from Alpha Vantage, looking in Yahoo Finance\n"
)
if (
yahoo_finance_model.get_financials(ticker, financial="financials")
is not None
):
return yahoo_finance_model.get_financials(
ticker, financial="financials"
)
else:
statements = response_json
df_fa = pd.DataFrame()
Expand All @@ -190,7 +200,7 @@ def get_income_statements(
df_fa = pd.DataFrame(statements["annualReports"])

if df_fa.empty:
console.print("No data found")
console.print("No data found from Alpha Vantage\n")
return pd.DataFrame()

df_fa = df_fa.set_index("fiscalDateEnding")
Expand Down Expand Up @@ -218,7 +228,7 @@ def get_balance_sheet(
Returns
-------
pd.DataFrame
Dataframe of income statements
Dataframe of balance sheet statements
"""
url = f"https://www.alphavantage.co/query?function=BALANCE_SHEET&symbol={ticker}&apikey={cfg.API_KEY_ALPHAVANTAGE}"
r = requests.get(url)
Expand All @@ -227,29 +237,34 @@ def get_balance_sheet(
# If the returned data was unsuccessful
if "Error Message" in response_json:
console.print(response_json["Error Message"])

# check if json is empty
if not response_json:
console.print("No data found from Alpha Vantage, looking in Yahoo Finance\n")
if (
yahoo_finance_model.get_financials(ticker, financial="balance-sheet")
is not None
):
return yahoo_finance_model.get_financials(ticker, financial="balance-sheet")
else:
# check if json is empty
if not response_json:
console.print("No data found")
else:
statements = response_json
df_fa = pd.DataFrame()
statements = response_json
df_fa = pd.DataFrame()

if quarterly:
if "quarterlyReports" in statements:
df_fa = pd.DataFrame(statements["quarterlyReports"])
else:
if "annualReports" in statements:
df_fa = pd.DataFrame(statements["annualReports"])
if quarterly:
if "quarterlyReports" in statements:
df_fa = pd.DataFrame(statements["quarterlyReports"])
else:
if "annualReports" in statements:
df_fa = pd.DataFrame(statements["annualReports"])

if df_fa.empty:
console.print("No data found")
return pd.DataFrame()
if df_fa.empty:
console.print("No data found from Alpha Vantage\n")
return pd.DataFrame()

df_fa = df_fa.set_index("fiscalDateEnding")
df_fa = df_fa.head(number)
df_fa = df_fa.applymap(lambda x: lambda_long_number_format(x))
return df_fa[::-1].T
df_fa = df_fa.set_index("fiscalDateEnding")
df_fa = df_fa.head(number)
df_fa = df_fa.applymap(lambda x: lambda_long_number_format(x))
return df_fa[::-1].T
return pd.DataFrame()


Expand All @@ -269,7 +284,7 @@ def get_cash_flow(ticker: str, number: int, quarterly: bool = False) -> pd.DataF
Returns
-------
pd.DataFrame
Dataframe of income statements
Dataframe of cash flow statements
"""
url = f"https://www.alphavantage.co/query?function=CASH_FLOW&symbol={ticker}&apikey={cfg.API_KEY_ALPHAVANTAGE}"
r = requests.get(url)
Expand All @@ -281,7 +296,15 @@ def get_cash_flow(ticker: str, number: int, quarterly: bool = False) -> pd.DataF
else:
# check if json is empty
if not response_json:
console.print("No data found")
console.print(
"No data found from Alpha Vantage, looking in Yahoo Finance\n"
)

if (
yahoo_finance_model.get_financials(ticker, financial="cash-flow")
is not None
):
return yahoo_finance_model.get_financials(ticker, financial="cash-flow")
else:
statements = response_json
df_fa = pd.DataFrame()
Expand All @@ -294,7 +317,7 @@ def get_cash_flow(ticker: str, number: int, quarterly: bool = False) -> pd.DataF
df_fa = pd.DataFrame(statements["annualReports"])

if df_fa.empty:
console.print("No data found")
console.print("No data found from Alpha Vantage\n")
return pd.DataFrame()

df_fa = df_fa.set_index("fiscalDateEnding")
Expand Down Expand Up @@ -335,7 +358,7 @@ def get_earnings(ticker: str, quarterly: bool = False) -> pd.DataFrame:
else:
# check if json is empty
if not result_json or len(result_json) < 2:
console.print("No data found")
console.print("No data found from Alpha Vantage\n")
else:

df_fa = pd.json_normalize(result_json)
Expand Down
4 changes: 2 additions & 2 deletions openbb_terminal/stocks/fundamental_analysis/av_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def display_income_statement(
def display_balance_sheet(
ticker: str, limit: int, quarterly: bool = False, export: str = ""
):
"""Alpha Vantage income statement
"""Alpha Vantage balance sheet statement

Parameters
----------
Expand Down Expand Up @@ -174,7 +174,7 @@ def display_cash_flow(
print_rich_table(
df_cash,
headers=list(df_cash.columns),
title=f"{ticker} Balance Sheet",
title=f"{ticker} Cash flow",
show_index=True,
)

Expand Down
81 changes: 67 additions & 14 deletions openbb_terminal/stocks/fundamental_analysis/fa_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ def print_help(self):
mt.add_cmd("splits", "Yahoo Finance", not self.suffix)
mt.add_cmd("web", "Yahoo Finance", not self.suffix)
mt.add_cmd("hq", "Yahoo Finance", not self.suffix)
mt.add_cmd("income", "Alpha Vantage / Polygon")
mt.add_cmd("balance", "Alpha Vantage / Polygon")
mt.add_cmd("income", "Alpha Vantage / Polygon / Yahoo Finance")
mt.add_cmd("balance", "Alpha Vantage / Polygon / Yahoo Finance")
mt.add_cmd("overview", "Alpha Vantage")
mt.add_cmd("key", "Alpha Vantage")
mt.add_cmd("cash", "Alpha Vantage")
mt.add_cmd("cash", "Alpha Vantage / Yahoo Finance")
mt.add_cmd("earnings", "Alpha Vantage")
mt.add_cmd("fraud", "Alpha Vantage")
mt.add_cmd("dupont", "Alpha Vantage")
Expand Down Expand Up @@ -197,7 +197,7 @@ def call_data(self, other_args: List[str]):
ROI, 52W High, Beta, Quick Ratio, Sales past 5Y, Gross Margin, 52W Low, ATR,
Employees, Current Ratio, Sales Q/Q, Operating Margin, RSI (14), Volatility, Optionable,
Debt/Eq, EPS Q/Q, Profit Margin, Rel Volume, Prev Close, Shortable, LT Debt/Eq,
Earnings, Payout, Avg Volume, Price, Recomendation, SMA20, SMA50, SMA200, Volume, Change.
Earnings, Payout, Avg Volume, Price, Recommendation, SMA20, SMA50, SMA200, Volume, Change.
[Source: Finviz]
""",
)
Expand Down Expand Up @@ -528,12 +528,19 @@ def call_income(self, other_args: List[str]):
dest="b_quarter",
help="Quarter fundamental data flag.",
)
parser.add_argument(
"-s",
"--source",
default="av",
dest="source",
choices=["polygon", "av", "yf"],
help="The source to get the data from",
)
ns_parser = self.parse_known_args_and_warn(
parser,
other_args,
export_allowed=EXPORT_ONLY_RAW_DATA_ALLOWED,
limit=5,
sources=["polygon", "av"],
)
if ns_parser:
if ns_parser.source == "av":
Expand All @@ -551,6 +558,12 @@ def call_income(self, other_args: List[str]):
quarterly=ns_parser.b_quarter,
export=ns_parser.export,
)
elif ns_parser.source == "yf":
yahoo_finance_view.display_fundamentals(
ticker=self.ticker,
financial="financials",
export=ns_parser.export,
)

@log_start_end(log=logger)
def call_balance(self, other_args: List[str]):
Expand Down Expand Up @@ -584,12 +597,19 @@ def call_balance(self, other_args: List[str]):
dest="b_quarter",
help="Quarter fundamental data flag.",
)
parser.add_argument(
"-s",
"--source",
default="av",
dest="source",
choices=["polygon", "av", "yf"],
help="The source to get the data from",
)
ns_parser = self.parse_known_args_and_warn(
parser,
other_args,
export_allowed=EXPORT_ONLY_RAW_DATA_ALLOWED,
limit=5,
sources=["polygon", "av"],
)
if ns_parser:
if ns_parser.source == "av":
Expand All @@ -607,6 +627,12 @@ def call_balance(self, other_args: List[str]):
quarterly=ns_parser.b_quarter,
export=ns_parser.export,
)
elif ns_parser.source == "yf":
yahoo_finance_view.display_fundamentals(
ticker=self.ticker,
financial="balance-sheet",
export=ns_parser.export,
)

@log_start_end(log=logger)
def call_cash(self, other_args: List[str]):
Expand Down Expand Up @@ -647,16 +673,41 @@ def call_cash(self, other_args: List[str]):
dest="b_quarter",
help="Quarter fundamental data flag.",
)
parser.add_argument(
"-s",
"--source",
default="av",
dest="source",
choices=["polygon", "av", "yf"],
help="The source to get the data from",
)
ns_parser = self.parse_known_args_and_warn(
parser, other_args, export_allowed=EXPORT_ONLY_RAW_DATA_ALLOWED
parser,
other_args,
export_allowed=EXPORT_ONLY_RAW_DATA_ALLOWED,
)
if ns_parser:
av_view.display_cash_flow(
ticker=self.ticker,
limit=ns_parser.limit,
quarterly=ns_parser.b_quarter,
export=ns_parser.export,
)
if ns_parser.source == "av":
av_view.display_cash_flow(
ticker=self.ticker,
limit=ns_parser.limit,
quarterly=ns_parser.b_quarter,
export=ns_parser.export,
)
elif ns_parser.source == "polygon":
polygon_view.display_fundamentals(
ticker=self.ticker,
financial="cash",
limit=ns_parser.limit,
quarterly=ns_parser.b_quarter,
export=ns_parser.export,
)
elif ns_parser.source == "yf":
yahoo_finance_view.display_fundamentals(
ticker=self.ticker,
financial="cash-flow",
export=ns_parser.export,
)

@log_start_end(log=logger)
def call_earnings(self, other_args: List[str]):
Expand Down Expand Up @@ -688,7 +739,9 @@ def call_earnings(self, other_args: List[str]):
help="Number of latest info",
)
ns_parser = self.parse_known_args_and_warn(
parser, other_args, EXPORT_ONLY_RAW_DATA_ALLOWED
parser,
other_args,
EXPORT_ONLY_RAW_DATA_ALLOWED,
)
if ns_parser:
av_view.display_earnings(
Expand Down
Loading