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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def handle_response(response):
|
2 |
+
if response.status_code == 200:
|
3 |
+
return response.json()
|
4 |
+
else:
|
5 |
+
return {"error": f"Failed to fetch data. Status code: {response.status_code}"}
|
6 |
+
|
7 |
+
def get_income_statement(ticker, period='annual', limit=5):
|
8 |
+
"""
|
9 |
+
Fetches the income statement for a given company (ticker) over a specified period and with a limit on the number of records returned.
|
10 |
+
|
11 |
+
Parameters:
|
12 |
+
-----------
|
13 |
+
ticker : str
|
14 |
+
The stock symbol or CIK (Central Index Key) for the company (e.g., 'AAPL' for Apple or '0000320193' for its CIK).
|
15 |
+
|
16 |
+
period : str, optional
|
17 |
+
The reporting period for the income statement. Allowable values are:
|
18 |
+
- 'annual' : Retrieves the annual income statement (default).
|
19 |
+
- 'quarter' : Retrieves the quarterly income statement.
|
20 |
+
|
21 |
+
limit : int, optional
|
22 |
+
Limits the number of records returned. The default value is 5.
|
23 |
+
|
24 |
+
Returns:
|
25 |
+
--------
|
26 |
+
dict or list of dict
|
27 |
+
The income statement data, including fields like date, symbol, reported currency, filing date, etc.
|
28 |
+
|
29 |
+
Example:
|
30 |
+
--------
|
31 |
+
get_income_statement('AAPL', period='annual', limit=5)
|
32 |
+
|
33 |
+
Response format:
|
34 |
+
----------------
|
35 |
+
[
|
36 |
+
{
|
37 |
+
"date": "2022-09-24",
|
38 |
+
"symbol": "AAPL",
|
39 |
+
"reportedCurrency": "USD",
|
40 |
+
"cik": "0000320193",
|
41 |
+
"fillingDate": "2022-10-28",
|
42 |
+
"acceptedDate": "2022-10-27 18:01:14",
|
43 |
+
...
|
44 |
+
},
|
45 |
+
...
|
46 |
+
]
|
47 |
+
"""
|
48 |
+
BASE_URL = "https://financialmodelingprep.com/api/v3"
|
49 |
+
params = {
|
50 |
+
"period": period, # Accepts 'annual' or 'quarter'
|
51 |
+
"limit": limit, # Limits the number of records returned
|
52 |
+
"apikey": os.environ['FMP_API_KEY'] # API Key for authentication
|
53 |
+
}
|
54 |
+
|
55 |
+
# Construct the full URL with query parameters
|
56 |
+
endpoint = f"{BASE_URL}/income-statement/{ticker}?{urlencode(params)}"
|
57 |
+
|
58 |
+
response = requests.get(endpoint)
|
59 |
+
return handle_response(response)
|
60 |
+
|
61 |
+
def ticker_search(query, limit=10, exchange='NYSE'):
|
62 |
+
"""
|
63 |
+
Searches for ticker symbols and exchanges for both equity securities and exchange-traded funds (ETFs)
|
64 |
+
by searching with the company name or ticker symbol.
|
65 |
+
|
66 |
+
Parameters:
|
67 |
+
-----------
|
68 |
+
query : str
|
69 |
+
The name or ticker symbol to search for (e.g., 'AA' for Alcoa).
|
70 |
+
|
71 |
+
limit : int, optional
|
72 |
+
Limits the number of records returned. The default is 10.
|
73 |
+
|
74 |
+
exchange : str, optional
|
75 |
+
Specifies the exchange to filter results by. Allowable values include:
|
76 |
+
- 'NYSE' : New York Stock Exchange (default).
|
77 |
+
- 'NASDAQ' : NASDAQ Exchange.
|
78 |
+
- Other exchange codes supported by the API.
|
79 |
+
|
80 |
+
Returns:
|
81 |
+
--------
|
82 |
+
dict or list of dict
|
83 |
+
The search results, including the symbol, name, currency, stock exchange, and exchange short name.
|
84 |
+
|
85 |
+
Example:
|
86 |
+
--------
|
87 |
+
ticker_search('AA', limit=10, exchange='NASDAQ')
|
88 |
+
|
89 |
+
Response format:
|
90 |
+
----------------
|
91 |
+
[
|
92 |
+
{
|
93 |
+
"symbol": "PRAA",
|
94 |
+
"name": "PRA Group, Inc.",
|
95 |
+
"currency": "USD",
|
96 |
+
"stockExchange": "NasdaqGS",
|
97 |
+
"exchangeShortName": "NASDAQ"
|
98 |
+
},
|
99 |
+
{
|
100 |
+
"symbol": "PAAS",
|
101 |
+
"name": "Pan American Silver Corp.",
|
102 |
+
"currency": "USD",
|
103 |
+
"stockExchange": "NasdaqGS",
|
104 |
+
"exchangeShortName": "NASDAQ"
|
105 |
+
},
|
106 |
+
...
|
107 |
+
]
|
108 |
+
"""
|
109 |
+
BASE_URL = "https://financialmodelingprep.com/api/v3"
|
110 |
+
|
111 |
+
params = {
|
112 |
+
"limit": limit,
|
113 |
+
"exchange": exchange,
|
114 |
+
"apikey": os.environ['FMP_API_KEY']
|
115 |
+
}
|
116 |
+
|
117 |
+
endpoint = f"{BASE_URL}/search?query={query}&{urlencode(params)}"
|
118 |
+
response = requests.get(endpoint)
|
119 |
+
return handle_response(response)
|
120 |
+
|
121 |
+
def company_profile(symbol):
|
122 |
+
"""
|
123 |
+
Fetches a company's profile, including key stats such as price, market capitalization, beta, and other essential details.
|
124 |
+
|
125 |
+
Parameters:
|
126 |
+
-----------
|
127 |
+
symbol : str
|
128 |
+
The stock ticker symbol or CIK (Central Index Key) for the company (e.g., 'AAPL' for Apple).
|
129 |
+
|
130 |
+
Returns:
|
131 |
+
--------
|
132 |
+
dict or list of dict
|
133 |
+
The company's profile data, including fields such as symbol, price, beta, market cap, industry, CEO, and description.
|
134 |
+
|
135 |
+
Example:
|
136 |
+
--------
|
137 |
+
company_profile('AAPL')
|
138 |
+
|
139 |
+
Response format:
|
140 |
+
----------------
|
141 |
+
[
|
142 |
+
{
|
143 |
+
"symbol": "AAPL",
|
144 |
+
"price": 145.30,
|
145 |
+
"beta": 1.25,
|
146 |
+
"volAvg": 98364732,
|
147 |
+
"mktCap": 2423446000000,
|
148 |
+
"lastDiv": 0.88,
|
149 |
+
"range": "122.25-157.33",
|
150 |
+
"changes": -2.00,
|
151 |
+
"companyName": "Apple Inc.",
|
152 |
+
"currency": "USD",
|
153 |
+
"cik": "0000320193",
|
154 |
+
"isin": "US0378331005",
|
155 |
+
"cusip": "037833100",
|
156 |
+
"exchange": "NasdaqGS",
|
157 |
+
"exchangeShortName": "NASDAQ",
|
158 |
+
"industry": "Consumer Electronics",
|
159 |
+
"website": "https://www.apple.com",
|
160 |
+
"description": "Apple Inc. designs, manufactures, and markets smartphones, personal computers, tablets, wearables, and accessories worldwide."
|
161 |
+
}
|
162 |
+
]
|
163 |
+
"""
|
164 |
+
|
165 |
+
BASE_URL = "https://financialmodelingprep.com/api/v3"
|
166 |
+
|
167 |
+
params = {
|
168 |
+
'apikey': os.environ['FMP_API_KEY']
|
169 |
+
}
|
170 |
+
|
171 |
+
endpoint = f"{BASE_URL}/profile/{symbol}?{urlencode(params)}"
|
172 |
+
response = requests.get(endpoint)
|
173 |
+
return handle_response(response)
|
174 |
+
|
175 |
+
def stock_grade(symbol, limit = 500):
|
176 |
+
|
177 |
+
BASE_URL = "https://financialmodelingprep.com/api/v3"
|
178 |
+
|
179 |
+
params = {
|
180 |
+
'apikey':os.environ['FMP_API_KEY'],
|
181 |
+
'limit':limit
|
182 |
+
}
|
183 |
+
|
184 |
+
endpoint = f"{BASE_URL}/grade/{symbol}?{urlencode(params)}"
|
185 |
+
response = requests.get(endpoint)
|
186 |
+
return handle_response(response)
|
187 |
+
|
188 |
+
def current_market_cap(symbol):
|
189 |
+
"""
|
190 |
+
Fetches the current market capitalization of a given company based on its stock symbol.
|
191 |
+
|
192 |
+
Parameters:
|
193 |
+
-----------
|
194 |
+
symbol : str
|
195 |
+
The stock ticker symbol for the company (e.g., 'AAPL' for Apple).
|
196 |
+
|
197 |
+
Returns:
|
198 |
+
--------
|
199 |
+
dict or list of dict
|
200 |
+
The market capitalization data, including fields such as symbol, date, and market cap.
|
201 |
+
|
202 |
+
Example:
|
203 |
+
--------
|
204 |
+
current_market_cap('AAPL')
|
205 |
+
|
206 |
+
Response format:
|
207 |
+
----------------
|
208 |
+
[
|
209 |
+
{
|
210 |
+
"symbol": "AAPL",
|
211 |
+
"date": "2023-03-02",
|
212 |
+
"marketCap": 2309048053309
|
213 |
+
}
|
214 |
+
]
|
215 |
+
"""
|
216 |
+
BASE_URL = "https://financialmodelingprep.com/api/v3"
|
217 |
+
params = {
|
218 |
+
'apikey': os.environ['FMP_API_KEY']
|
219 |
+
}
|
220 |
+
|
221 |
+
endpoint = f"{BASE_URL}/market-capitalization/{symbol}?{urlencode(params)}"
|
222 |
+
response = requests.get(endpoint)
|
223 |
+
return handle_response(response)
|
224 |
+
|
225 |
+
def historical_market_cap(symbol, from_date=None, to_date=None, limit=None):
|
226 |
+
"""
|
227 |
+
Fetches the historical market capitalization of a given company within a specified date range.
|
228 |
+
|
229 |
+
Parameters:
|
230 |
+
-----------
|
231 |
+
symbol : str
|
232 |
+
The stock ticker symbol for the company (e.g., 'AAPL' for Apple).
|
233 |
+
|
234 |
+
from_date : str, optional
|
235 |
+
The start date for the historical data in 'YYYY-MM-DD' format (e.g., '2023-10-10').
|
236 |
+
Default is None, which fetches data from the earliest available date.
|
237 |
+
|
238 |
+
to_date : str, optional
|
239 |
+
The end date for the historical data in 'YYYY-MM-DD' format (e.g., '2023-12-10').
|
240 |
+
Default is None, which fetches data up to the latest available date.
|
241 |
+
|
242 |
+
limit : int, optional
|
243 |
+
Limits the number of records returned. Default is None, which fetches all available records.
|
244 |
+
|
245 |
+
Returns:
|
246 |
+
--------
|
247 |
+
dict or list of dict
|
248 |
+
The historical market cap data, including fields such as symbol, date, and market capitalization.
|
249 |
+
|
250 |
+
Example:
|
251 |
+
--------
|
252 |
+
historical_market_cap('AAPL', from_date='2023-10-10', to_date='2023-12-10', limit=100)
|
253 |
+
|
254 |
+
Response format:
|
255 |
+
----------------
|
256 |
+
[
|
257 |
+
{
|
258 |
+
"symbol": "AAPL",
|
259 |
+
"date": "2023-03-02",
|
260 |
+
"marketCap": 2313794623242
|
261 |
+
}
|
262 |
+
]
|
263 |
+
"""
|
264 |
+
BASE_URL = "https://financialmodelingprep.com/api/v3"
|
265 |
+
params = {
|
266 |
+
'apikey': os.environ['FMP_API_KEY'],
|
267 |
+
'from': from_date,
|
268 |
+
'to': to_date,
|
269 |
+
'limit': limit
|
270 |
+
}
|
271 |
+
|
272 |
+
endpoint = f"{BASE_URL}/historical-market-capitalization/{symbol}?{urlencode(params)}"
|
273 |
+
response = requests.get(endpoint)
|
274 |
+
return handle_response(response)
|
275 |
+
|
276 |
+
def analyst_recommendations(symbol):
|
277 |
+
"""
|
278 |
+
Fetches the analyst recommendations for a given company based on its stock symbol.
|
279 |
+
This includes buy, hold, and sell ratings.
|
280 |
+
|
281 |
+
Parameters:
|
282 |
+
-----------
|
283 |
+
symbol : str
|
284 |
+
The stock ticker symbol for the company (e.g., 'AAPL' for Apple).
|
285 |
+
|
286 |
+
Returns:
|
287 |
+
--------
|
288 |
+
dict or list of dict
|
289 |
+
The analyst recommendation data, including fields such as buy, hold, sell, and strong buy ratings.
|
290 |
+
|
291 |
+
Example:
|
292 |
+
--------
|
293 |
+
analyst_recommendations('AAPL')
|
294 |
+
|
295 |
+
Response format:
|
296 |
+
----------------
|
297 |
+
[
|
298 |
+
{
|
299 |
+
"symbol": "AAPL",
|
300 |
+
"date": "2023-08-01",
|
301 |
+
"analystRatingsBuy": 21,
|
302 |
+
"analystRatingsHold": 6,
|
303 |
+
"analystRatingsSell": 0,
|
304 |
+
"analystRatingsStrongSell": 0,
|
305 |
+
"analystRatingsStrongBuy": 11
|
306 |
+
}
|
307 |
+
]
|
308 |
+
"""
|
309 |
+
BASE_URL = "https://financialmodelingprep.com/api/v3"
|
310 |
+
params = {
|
311 |
+
'apikey': os.environ['FMP_API_KEY']
|
312 |
+
}
|
313 |
+
|
314 |
+
endpoint = f"{BASE_URL}/analyst-stock-recommendations/{symbol}?{urlencode(params)}"
|
315 |
+
response = requests.get(endpoint)
|
316 |
+
return handle_response(response)
|
317 |
+
|
318 |
+
def stock_peers(symbol):
|
319 |
+
"""
|
320 |
+
Fetches a list of companies that are considered peers of the given company.
|
321 |
+
These peers are companies that trade on the same exchange, are in the same sector,
|
322 |
+
and have a similar market capitalization.
|
323 |
+
|
324 |
+
Parameters:
|
325 |
+
-----------
|
326 |
+
symbol : str
|
327 |
+
The stock ticker symbol for the company (e.g., 'AAPL' for Apple).
|
328 |
+
|
329 |
+
Returns:
|
330 |
+
--------
|
331 |
+
dict or list of dict
|
332 |
+
The peers data, including a list of peer company ticker symbols.
|
333 |
+
|
334 |
+
Example:
|
335 |
+
--------
|
336 |
+
stock_peers('AAPL')
|
337 |
+
|
338 |
+
Response format:
|
339 |
+
----------------
|
340 |
+
[
|
341 |
+
{
|
342 |
+
"symbol": "AAPL",
|
343 |
+
"peersList": [
|
344 |
+
"LPL",
|
345 |
+
"SNEJF",
|
346 |
+
"PCRFY",
|
347 |
+
"SONO",
|
348 |
+
"VZIO",
|
349 |
+
...
|
350 |
+
]
|
351 |
+
}
|
352 |
+
]
|
353 |
+
"""
|
354 |
+
params = {
|
355 |
+
'apikey': os.environ['FMP_API_KEY'],
|
356 |
+
'symbol': symbol
|
357 |
+
}
|
358 |
+
|
359 |
+
BASE_URL = "https://financialmodelingprep.com/api/v4"
|
360 |
+
endpoint = f"{BASE_URL}/stock_peers?{urlencode(params)}"
|
361 |
+
response = requests.get(endpoint)
|
362 |
+
return handle_response(response)
|
363 |
+
|
364 |
+
def earnings_historical_and_upcoming(symbol, limit=100):
|
365 |
+
"""
|
366 |
+
Fetches historical and upcoming earnings announcements for a given company.
|
367 |
+
The response includes the date, EPS (earnings per share), estimated EPS, revenue, and estimated revenue.
|
368 |
+
|
369 |
+
Parameters:
|
370 |
+
-----------
|
371 |
+
symbol : str
|
372 |
+
The stock ticker symbol for the company (e.g., 'AAPL' for Apple).
|
373 |
+
|
374 |
+
limit : int, optional
|
375 |
+
Limits the number of records returned. The default is 100.
|
376 |
+
|
377 |
+
Returns:
|
378 |
+
--------
|
379 |
+
dict or list of dict
|
380 |
+
The earnings data, including fields such as date, EPS, estimated EPS, revenue, and estimated revenue.
|
381 |
+
|
382 |
+
Example:
|
383 |
+
--------
|
384 |
+
earnings_historical_and_upcoming('AAPL', limit=100)
|
385 |
+
|
386 |
+
Response format:
|
387 |
+
----------------
|
388 |
+
[
|
389 |
+
{
|
390 |
+
"date": "1998-10-14",
|
391 |
+
"symbol": "AAPL",
|
392 |
+
"eps": 0.0055,
|
393 |
+
"epsEstimated": 0.00393,
|
394 |
+
"time": "amc",
|
395 |
+
"revenue": 1556000000,
|
396 |
+
"revenueEstimated": 2450700000,
|
397 |
+
"updatedFromDate": "2023-12-04",
|
398 |
+
"fiscalDateEnding": "1998-09-25"
|
399 |
+
}
|
400 |
+
]
|
401 |
+
"""
|
402 |
+
params = {
|
403 |
+
'apikey': os.environ['FMP_API_KEY'],
|
404 |
+
'limit': limit
|
405 |
+
}
|
406 |
+
|
407 |
+
endpoint = f"{BASE_URL}/historical/earning_calendar/{symbol}?{urlencode(params)}"
|
408 |
+
response = requests.get(endpoint)
|
409 |
+
return handle_response(response)
|
410 |
+
|
411 |
+
def intraday_stock_prices(timeframe, symbol, from_date=None, to_date=None, extended='false'):
|
412 |
+
"""
|
413 |
+
Fetches the historical intraday stock price for a given company over a specified timeframe.
|
414 |
+
|
415 |
+
Parameters:
|
416 |
+
-----------
|
417 |
+
timeframe : str
|
418 |
+
The time interval for the stock data. Allowable values are:
|
419 |
+
- '1min' : 1 minute interval
|
420 |
+
- '5min' : 5 minute interval
|
421 |
+
- '15min' : 15 minute interval
|
422 |
+
- '30min' : 30 minute interval
|
423 |
+
- '1hour' : 1 hour interval
|
424 |
+
- '4hour' : 4 hour interval
|
425 |
+
|
426 |
+
symbol : str
|
427 |
+
The stock symbol for which to retrieve the data (e.g., 'AAPL' for Apple).
|
428 |
+
|
429 |
+
from_date : str, optional
|
430 |
+
The start date for the historical data in 'YYYY-MM-DD' format (e.g., '2023-08-10').
|
431 |
+
Default is None, which fetches all available data up to the present.
|
432 |
+
|
433 |
+
to_date : str, optional
|
434 |
+
The end date for the historical data in 'YYYY-MM-DD' format (e.g., '2023-09-10').
|
435 |
+
Default is None, which fetches data from the beginning up to the current date.
|
436 |
+
|
437 |
+
extended : str, optional
|
438 |
+
Whether to fetch extended market data (pre-market and after-hours).
|
439 |
+
Allowable values:
|
440 |
+
- 'true' : Fetch extended market data
|
441 |
+
- 'false' : Fetch only regular market hours data (default).
|
442 |
+
|
443 |
+
Returns:
|
444 |
+
--------
|
445 |
+
dict or list of dict
|
446 |
+
The historical intraday stock data, including open, high, low, close, and volume for each time interval.
|
447 |
+
|
448 |
+
Example:
|
449 |
+
--------
|
450 |
+
intraday_stock_price('5min', 'AAPL', from_date='2023-08-10', to_date='2023-09-10', extended='false')
|
451 |
+
|
452 |
+
Response format:
|
453 |
+
----------------
|
454 |
+
[
|
455 |
+
{
|
456 |
+
"date": "2023-03-02 16:00:00",
|
457 |
+
"open": 145.92,
|
458 |
+
"low": 145.72,
|
459 |
+
"high": 146.00,
|
460 |
+
"close": 145.79,
|
461 |
+
"volume": 1492644
|
462 |
+
},
|
463 |
+
...
|
464 |
+
]
|
465 |
+
"""
|
466 |
+
BASE_URL = "https://financialmodelingprep.com/api/v3"
|
467 |
+
params = {
|
468 |
+
'apikey': os.environ['FMP_API_KEY'],
|
469 |
+
'from': from_date,
|
470 |
+
'to': to_date,
|
471 |
+
'extended': extended
|
472 |
+
}
|
473 |
+
|
474 |
+
endpoint = f"{BASE_URL}/historical-chart/{timeframe}/{symbol}?{urlencode(params)}"
|
475 |
+
response = requests.get(endpoint)
|
476 |
+
return handle_response(response)
|
477 |
+
|
478 |
+
|
479 |
+
|
480 |
+
def daily_stock_prices(symbol, from_date=None, to_date=None, serietype='line'):
|
481 |
+
"""
|
482 |
+
Fetches the daily End-Of-Day (EOD) stock price for a given company over a specified date range.
|
483 |
+
|
484 |
+
Parameters:
|
485 |
+
-----------
|
486 |
+
symbol : str
|
487 |
+
The stock symbol for which to retrieve the data (e.g., 'AAPL' for Apple).
|
488 |
+
|
489 |
+
from_date : str, optional
|
490 |
+
The start date for the historical data in 'YYYY-MM-DD' format (e.g., '1990-10-10').
|
491 |
+
Default is None, which fetches the earliest available data.
|
492 |
+
|
493 |
+
to_date : str, optional
|
494 |
+
The end date for the historical data in 'YYYY-MM-DD' format (e.g., '2023-10-10').
|
495 |
+
Default is None, which fetches data up to the most recent date.
|
496 |
+
|
497 |
+
serietype : str, optional
|
498 |
+
The type of data series to return. Allowable values are:
|
499 |
+
- 'line' : Line chart data (default).
|
500 |
+
- 'other types' can be specified if supported by the API in the future.
|
501 |
+
|
502 |
+
Returns:
|
503 |
+
--------
|
504 |
+
dict or list of dict
|
505 |
+
The daily stock data, including open, high, low, close, volume, adjusted close, etc.
|
506 |
+
|
507 |
+
Example:
|
508 |
+
--------
|
509 |
+
daily_stock_price('AAPL', from_date='1990-10-10', to_date='2023-10-10', serietype='line')
|
510 |
+
|
511 |
+
Response format:
|
512 |
+
----------------
|
513 |
+
{
|
514 |
+
"symbol": "AAPL",
|
515 |
+
"historical": [
|
516 |
+
{
|
517 |
+
"date": "2023-10-06",
|
518 |
+
"open": 173.8,
|
519 |
+
"high": 176.61,
|
520 |
+
"low": 173.18,
|
521 |
+
"close": 176.53,
|
522 |
+
"adjClose": 176.53,
|
523 |
+
"volume": 21712747,
|
524 |
+
"unadjustedVolume": 21712747,
|
525 |
+
"change": 2.73,
|
526 |
+
"changePercent": 1.57077,
|
527 |
+
"vwap": 175.44,
|
528 |
+
"label": "October 06, 23",
|
529 |
+
"changeOverTime": 0.0157077
|
530 |
+
},
|
531 |
+
...
|
532 |
+
]
|
533 |
+
}
|
534 |
+
"""
|
535 |
+
BASE_URL = "https://financialmodelingprep.com/api/v3"
|
536 |
+
params = {
|
537 |
+
'apikey': os.environ['FMP_API_KEY'],
|
538 |
+
'from': from_date,
|
539 |
+
'to': to_date,
|
540 |
+
'serietype': serietype
|
541 |
+
}
|
542 |
+
|
543 |
+
endpoint = f"{BASE_URL}/historical-price-full/{symbol}?{urlencode(params)}"
|
544 |
+
response = requests.get(endpoint)
|
545 |
+
return handle_response(response)
|