Skip to content

Commit ca329f3

Browse files
committed
Implement YahooFinanceProvider
1 parent 3510805 commit ca329f3

File tree

3 files changed

+131
-4
lines changed

3 files changed

+131
-4
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ streamlit
22
pytest
33
streamlit-extras
44
pandas
5+
yfinance

src/data_providers/FinancialDataProvider.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ def get_historical_data(self, period: str, interval: str) -> pd.DataFrame:
1212
pass
1313

1414
@abstractmethod
15-
def get_stock_info(self):
15+
def get_stock_info(self) -> dict:
1616
pass
1717

1818
@abstractmethod
19-
def get_balance_sheet(self):
19+
def get_balance_sheet(self) -> pd.DataFrame:
2020
pass
2121

2222
@abstractmethod
23-
def get_income_statement(self):
23+
def get_income_statement(self) -> pd.DataFrame:
2424
pass
2525

2626
@abstractmethod
27-
def get_cash_flow(self):
27+
def get_cash_flow(self) -> pd.DataFrame:
2828
pass
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import logging
2+
from abc import ABC
3+
4+
import pandas as pd
5+
import yfinance as yf
6+
7+
from .FinancialDataProvider import FinancialDataProvider
8+
9+
logging.basicConfig(filename="YahooFinanceProvider.log", level=logging.ERROR)
10+
11+
12+
class YahooFinanceProvider(FinancialDataProvider, ABC):
13+
def __init__(self, ticker):
14+
super().__init__(ticker)
15+
self.stock = yf.Ticker(ticker)
16+
self.general_info = self.stock.info
17+
self.balance_sheet = self.stock.balance_sheet
18+
self.income_stmt = self.stock.income_stmt
19+
self.cash_flow = self.stock.cash_flow
20+
21+
def get_historical_data(self, period: str, interval: str) -> pd.DataFrame:
22+
try:
23+
return self.stock.history(period=period, interval=interval)
24+
except Exception as e:
25+
logging.error(
26+
f"Error fetching historical data from Yahoo Finance for {self.stock}: {e}"
27+
)
28+
29+
def get_stock_info(self) -> dict:
30+
try:
31+
fields = [
32+
"country",
33+
"industry",
34+
"sector",
35+
"longBusinessSummary",
36+
"symbol",
37+
"longName",
38+
"trailingPE",
39+
"dividendYield",
40+
"payoutRatio",
41+
"trailingEps",
42+
"returnOnAssets",
43+
"returnOnEquity",
44+
"marketCap",
45+
"operatingCashflow",
46+
]
47+
48+
return {
49+
key: self.general_info[key]
50+
for key in fields
51+
if key in self.general_info
52+
}
53+
except Exception as e:
54+
logging.error(f"Error fetching stock info for {self.stock}: {e}")
55+
56+
def get_balance_sheet(self) -> pd.DataFrame:
57+
try:
58+
fields = [
59+
"Current Assets",
60+
"Current Liabilities",
61+
"Total Assets",
62+
"Total Liabilities Net Minority Interest",
63+
"Total Capitalization",
64+
"Working Capital",
65+
"Invested Capital",
66+
"Total Debt",
67+
"Net Debt",
68+
"Stockholders Equity",
69+
]
70+
71+
df_balance_sheet = self.balance_sheet.loc[fields, :]
72+
df_balance_sheet.loc["Debt to Equity"] = (
73+
df_balance_sheet.loc["Total Liabilities Net Minority Interest"]
74+
/ df_balance_sheet.loc["Stockholders Equity"]
75+
)
76+
df_balance_sheet.loc["Current Ratio"] = (
77+
df_balance_sheet.loc["Current Assets"]
78+
/ df_balance_sheet.loc["Current Liabilities"]
79+
)
80+
df_balance_sheet = df_balance_sheet.transpose()
81+
82+
return df_balance_sheet.sort_index()
83+
except Exception as e:
84+
logging.error(f"Error fetching balance sheet for {self.stock}: {e}")
85+
86+
def get_income_statement(self) -> pd.DataFrame:
87+
try:
88+
fields = [
89+
"Total Revenue",
90+
"Cost Of Revenue",
91+
"Gross Profit",
92+
"Operating Expense",
93+
"Operating Income",
94+
"Total Expenses",
95+
"Net Income From Continuing Operation Net Minority Interest",
96+
"EBIT",
97+
"EBITDA",
98+
"General And Administrative Expense",
99+
"Selling And Marketing Expense",
100+
"Research And Development",
101+
"Basic EPS",
102+
]
103+
104+
df_income_stmt = self.income_stmt.loc[fields, :]
105+
df_income_stmt = df_income_stmt.transpose()
106+
return df_income_stmt.sort_index()
107+
108+
except Exception as e:
109+
logging.error(f"Error fetching income statement for {self.stock}: {e}")
110+
111+
def get_cash_flow(self):
112+
try:
113+
fields = [
114+
"Operating Cash Flow",
115+
"Investing Cash Flow",
116+
"Financing Cash Flow",
117+
"Capital Expenditure",
118+
"Free Cash Flow",
119+
]
120+
121+
df_cashflow = self.cash_flow.loc[fields, :]
122+
df_cashflow = df_cashflow.transpose()
123+
return df_cashflow.sort_index()
124+
125+
except Exception as e:
126+
logging.error(f"Error fetching cash flow for {self.stock}: {e}")

0 commit comments

Comments
 (0)