OnurKerimoglu commited on
Commit
e931d99
·
1 Parent(s): 2ac2093

introduced nb/fundamental_analysis.ipynb

Browse files
Files changed (1) hide show
  1. notebooks/fundamental_analysis.ipynb +281 -0
notebooks/fundamental_analysis.ipynb ADDED
@@ -0,0 +1,281 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "import yfinance as yf"
10
+ ]
11
+ },
12
+ {
13
+ "cell_type": "code",
14
+ "execution_count": null,
15
+ "metadata": {},
16
+ "outputs": [
17
+ {
18
+ "data": {
19
+ "text/plain": [
20
+ "['52WeekChange',\n",
21
+ " 'SandP52WeekChange',\n",
22
+ " 'address1',\n",
23
+ " 'ask',\n",
24
+ " 'askSize',\n",
25
+ " 'averageDailyVolume10Day',\n",
26
+ " 'averageVolume',\n",
27
+ " 'averageVolume10days',\n",
28
+ " 'beta',\n",
29
+ " 'bid',\n",
30
+ " 'bidSize',\n",
31
+ " 'bookValue',\n",
32
+ " 'city',\n",
33
+ " 'companyOfficers',\n",
34
+ " 'compensationAsOfEpochDate',\n",
35
+ " 'country',\n",
36
+ " 'currency',\n",
37
+ " 'currentPrice',\n",
38
+ " 'currentRatio',\n",
39
+ " 'dateShortInterest',\n",
40
+ " 'dayHigh',\n",
41
+ " 'dayLow',\n",
42
+ " 'debtToEquity',\n",
43
+ " 'dividendRate',\n",
44
+ " 'dividendYield',\n",
45
+ " 'earningsGrowth',\n",
46
+ " 'earningsQuarterlyGrowth',\n",
47
+ " 'ebitda',\n",
48
+ " 'ebitdaMargins',\n",
49
+ " 'enterpriseToEbitda',\n",
50
+ " 'enterpriseToRevenue',\n",
51
+ " 'enterpriseValue',\n",
52
+ " 'exDividendDate',\n",
53
+ " 'exchange',\n",
54
+ " 'fiftyDayAverage',\n",
55
+ " 'fiftyTwoWeekHigh',\n",
56
+ " 'fiftyTwoWeekLow',\n",
57
+ " 'financialCurrency',\n",
58
+ " 'firstTradeDateEpochUtc',\n",
59
+ " 'floatShares',\n",
60
+ " 'forwardEps',\n",
61
+ " 'forwardPE',\n",
62
+ " 'freeCashflow',\n",
63
+ " 'fullTimeEmployees',\n",
64
+ " 'gmtOffSetMilliseconds',\n",
65
+ " 'grossMargins',\n",
66
+ " 'grossProfits',\n",
67
+ " 'heldPercentInsiders',\n",
68
+ " 'heldPercentInstitutions',\n",
69
+ " 'impliedSharesOutstanding',\n",
70
+ " 'industry',\n",
71
+ " 'industryDisp',\n",
72
+ " 'industryKey',\n",
73
+ " 'lastDividendDate',\n",
74
+ " 'lastDividendValue',\n",
75
+ " 'lastFiscalYearEnd',\n",
76
+ " 'lastSplitDate',\n",
77
+ " 'lastSplitFactor',\n",
78
+ " 'longBusinessSummary',\n",
79
+ " 'longName',\n",
80
+ " 'marketCap',\n",
81
+ " 'maxAge',\n",
82
+ " 'messageBoardId',\n",
83
+ " 'mostRecentQuarter',\n",
84
+ " 'netIncomeToCommon',\n",
85
+ " 'nextFiscalYearEnd',\n",
86
+ " 'numberOfAnalystOpinions',\n",
87
+ " 'open',\n",
88
+ " 'operatingCashflow',\n",
89
+ " 'operatingMargins',\n",
90
+ " 'payoutRatio',\n",
91
+ " 'phone',\n",
92
+ " 'previousClose',\n",
93
+ " 'priceHint',\n",
94
+ " 'priceToBook',\n",
95
+ " 'priceToSalesTrailing12Months',\n",
96
+ " 'profitMargins',\n",
97
+ " 'quickRatio',\n",
98
+ " 'quoteType',\n",
99
+ " 'recommendationKey',\n",
100
+ " 'recommendationMean',\n",
101
+ " 'regularMarketDayHigh',\n",
102
+ " 'regularMarketDayLow',\n",
103
+ " 'regularMarketOpen',\n",
104
+ " 'regularMarketPreviousClose',\n",
105
+ " 'regularMarketVolume',\n",
106
+ " 'returnOnAssets',\n",
107
+ " 'returnOnEquity',\n",
108
+ " 'revenueGrowth',\n",
109
+ " 'revenuePerShare',\n",
110
+ " 'sector',\n",
111
+ " 'sectorDisp',\n",
112
+ " 'sectorKey',\n",
113
+ " 'sharesOutstanding',\n",
114
+ " 'sharesPercentSharesOut',\n",
115
+ " 'sharesShort',\n",
116
+ " 'sharesShortPreviousMonthDate',\n",
117
+ " 'sharesShortPriorMonth',\n",
118
+ " 'shortName',\n",
119
+ " 'shortRatio',\n",
120
+ " 'state',\n",
121
+ " 'symbol',\n",
122
+ " 'targetHighPrice',\n",
123
+ " 'targetLowPrice',\n",
124
+ " 'targetMeanPrice',\n",
125
+ " 'targetMedianPrice',\n",
126
+ " 'timeZoneFullName',\n",
127
+ " 'timeZoneShortName',\n",
128
+ " 'totalCash',\n",
129
+ " 'totalCashPerShare',\n",
130
+ " 'totalDebt',\n",
131
+ " 'totalRevenue',\n",
132
+ " 'trailingAnnualDividendRate',\n",
133
+ " 'trailingAnnualDividendYield',\n",
134
+ " 'trailingEps',\n",
135
+ " 'trailingPE',\n",
136
+ " 'trailingPegRatio',\n",
137
+ " 'twoHundredDayAverage',\n",
138
+ " 'underlyingSymbol',\n",
139
+ " 'uuid',\n",
140
+ " 'volume',\n",
141
+ " 'website',\n",
142
+ " 'zip']"
143
+ ]
144
+ },
145
+ "execution_count": 22,
146
+ "metadata": {},
147
+ "output_type": "execute_result"
148
+ }
149
+ ],
150
+ "source": [
151
+ "info = yf.Ticker('GOOG').info\n",
152
+ "fundamentals = ['forwardPE', 'priceToBook', 'priceToSalesTrailing12Months' 'dividentYield', 'marketCap', 'beta', ]\n",
153
+ "sorted(info.keys())"
154
+ ]
155
+ },
156
+ {
157
+ "cell_type": "code",
158
+ "execution_count": 24,
159
+ "metadata": {},
160
+ "outputs": [
161
+ {
162
+ "name": "stdout",
163
+ "output_type": "stream",
164
+ "text": [
165
+ "Company Name: Alphabet Inc.\n",
166
+ "Sector: Communication Services\n",
167
+ "Industry: Internet Content & Information\n",
168
+ "Market Capitalization: 2506161389568\n",
169
+ "P/E Ratio (Trailing): 27.231789\n",
170
+ "P/E Ratio (Forward): 22.853231\n",
171
+ "P/B Ratio (Price to Book): 8.027174\n",
172
+ "P/S Ratio (Price to Sales): 7.3741207\n",
173
+ "Dividend Yield (%): 0.39\n",
174
+ "Earnings Per Share (EPS): 7.55\n",
175
+ "Return on Equity (ROE): 0.32101002\n",
176
+ "Return on Assets (ROA): 0.16483\n",
177
+ "Gross Margin (%): 58.13\n",
178
+ "Operating Margin (%): 32.31\n",
179
+ "Debt-to-Equity Ratio: 9.324\n",
180
+ "Current Ratio: 1.95\n",
181
+ "Quick Ratio: 1.761\n",
182
+ "Interest Coverage Ratio: 4.22\n",
183
+ "Revenue Growth (%): 15.1\n",
184
+ "EPS Growth (%): 36.6\n",
185
+ "Institutional Ownership (%): 61.46\n",
186
+ "Insider Ownership (%): 0.02\n"
187
+ ]
188
+ }
189
+ ],
190
+ "source": [
191
+ "def get_stock_fundamentals(ticker: str) -> dict:\n",
192
+ " \"\"\"\n",
193
+ " Attempts to fetches key fundamental metrics for a given stock ticker using yfinance.\n",
194
+ " \n",
195
+ " :param ticker: Stock symbol (e.g., 'GOOG' for Google)\n",
196
+ " :return: Dictionary of fundamental metrics with human-readable keys\n",
197
+ " \"\"\"\n",
198
+ " stock = yf.Ticker(ticker)\n",
199
+ " info = stock.info # Fetch general info\n",
200
+ "\n",
201
+ " # Define a safe fetch method to avoid entire function failure\n",
202
+ " def safe_get(key, transform=None):\n",
203
+ " value = info.get(key, \"Metric not available\")\n",
204
+ " if value != \"Metric not available\" and transform:\n",
205
+ " try:\n",
206
+ " return transform(value)\n",
207
+ " except Exception:\n",
208
+ " return \"metric not available\"\n",
209
+ " return value\n",
210
+ "\n",
211
+ " # Extract key financial metrics\n",
212
+ " fundamentals = {\n",
213
+ " \"Company Name\": safe_get(\"longName\"),\n",
214
+ " \"Sector\": safe_get(\"sector\"),\n",
215
+ " \"Industry\": safe_get(\"industry\"),\n",
216
+ " \"Market Capitalization\": safe_get(\"marketCap\"),\n",
217
+ " \n",
218
+ " # Valuation Metrics\n",
219
+ " \"P/E Ratio (Trailing)\": safe_get(\"trailingPE\"),\n",
220
+ " \"P/E Ratio (Forward)\": safe_get(\"forwardPE\"),\n",
221
+ " \"P/B Ratio (Price to Book)\": safe_get(\"priceToBook\"),\n",
222
+ " \"P/S Ratio (Price to Sales)\": safe_get(\"priceToSalesTrailing12Months\"),\n",
223
+ " \"Dividend Yield (%)\": safe_get(\"dividendYield\", lambda x: round(x * 100, 2)),\n",
224
+ "\n",
225
+ " # Profitability Metrics\n",
226
+ " \"Earnings Per Share (EPS)\": safe_get(\"trailingEps\"),\n",
227
+ " \"Return on Equity (ROE)\": safe_get(\"returnOnEquity\"),\n",
228
+ " \"Return on Assets (ROA)\": safe_get(\"returnOnAssets\"),\n",
229
+ " \"Gross Margin (%)\": safe_get(\"grossMargins\", lambda x: round(x * 100, 2)),\n",
230
+ " \"Operating Margin (%)\": safe_get(\"operatingMargins\", lambda x: round(x * 100, 2)),\n",
231
+ "\n",
232
+ " # Financial Health Metrics\n",
233
+ " \"Debt-to-Equity Ratio\": safe_get(\"debtToEquity\"),\n",
234
+ " \"Current Ratio\": safe_get(\"currentRatio\"),\n",
235
+ " \"Quick Ratio\": safe_get(\"quickRatio\"),\n",
236
+ " \"Interest Coverage Ratio\": safe_get(\n",
237
+ " \"ebitda\",\n",
238
+ " lambda ebitda: round(ebitda / info[\"totalDebt\"], 2) if info.get(\"totalDebt\") else \"Metric not available\"\n",
239
+ " ),\n",
240
+ "\n",
241
+ " # Growth Metrics\n",
242
+ " \"Revenue Growth (%)\": safe_get(\"revenueGrowth\", lambda x: round(x * 100, 2)),\n",
243
+ " \"EPS Growth (%)\": safe_get(\"earningsGrowth\", lambda x: round(x * 100, 2)),\n",
244
+ "\n",
245
+ " # Market & Ownership\n",
246
+ " \"Institutional Ownership (%)\": safe_get(\"heldPercentInstitutions\", lambda x: round(x * 100, 2)),\n",
247
+ " \"Insider Ownership (%)\": safe_get(\"heldPercentInsiders\", lambda x: round(x * 100, 2)),\n",
248
+ " }\n",
249
+ " \n",
250
+ " return fundamentals\n",
251
+ "\n",
252
+ "# Example Usage\n",
253
+ "ticker = \"GOOG\"\n",
254
+ "dict_fundamentals = get_stock_fundamentals(ticker)\n",
255
+ "for key, value in dict_fundamentals.items():\n",
256
+ " print(f\"{key}: {value}\")"
257
+ ]
258
+ }
259
+ ],
260
+ "metadata": {
261
+ "kernelspec": {
262
+ "display_name": "finagents_py311",
263
+ "language": "python",
264
+ "name": "python3"
265
+ },
266
+ "language_info": {
267
+ "codemirror_mode": {
268
+ "name": "ipython",
269
+ "version": 3
270
+ },
271
+ "file_extension": ".py",
272
+ "mimetype": "text/x-python",
273
+ "name": "python",
274
+ "nbconvert_exporter": "python",
275
+ "pygments_lexer": "ipython3",
276
+ "version": "3.11.1"
277
+ }
278
+ },
279
+ "nbformat": 4,
280
+ "nbformat_minor": 2
281
+ }