6
6
import random
7
7
from collections import defaultdict
8
8
from datetime import date , datetime , timedelta
9
- from typing import Annotated , Any , List , Optional
10
9
from textwrap import dedent
10
+ from typing import Annotated , Any , List , Optional
11
11
12
12
import pandas as pd
13
13
35
35
import requests
36
36
from pydantic import BaseModel
37
37
38
-
39
38
FINANCIAL_DATASETS_BASE_URL = "https://api.financialdatasets.ai/"
40
39
40
+
41
41
class FinancialDatasetsAPIWrapper (BaseModel ):
42
42
"""Wrapper for financial datasets API."""
43
43
@@ -65,8 +65,7 @@ def get_income_statements(
65
65
period : str ,
66
66
limit : Optional [int ],
67
67
) -> Optional [dict ]:
68
- """
69
- Get the income statements for a stock `ticker` over a `period` of time.
68
+ """Get the income statements for a stock `ticker` over a `period` of time.
70
69
71
70
:param ticker: the stock ticker
72
71
:param period: the period of time to get the balance sheets for.
@@ -96,8 +95,7 @@ def get_balance_sheets(
96
95
period : str ,
97
96
limit : Optional [int ],
98
97
) -> List [dict ]:
99
- """
100
- Get the balance sheets for a stock `ticker` over a `period` of time.
98
+ """Get the balance sheets for a stock `ticker` over a `period` of time.
101
99
102
100
:param ticker: the stock ticker
103
101
:param period: the period of time to get the balance sheets for.
@@ -127,8 +125,7 @@ def get_cash_flow_statements(
127
125
period : str ,
128
126
limit : Optional [int ],
129
127
) -> List [dict ]:
130
- """
131
- Get the cash flow statements for a stock `ticker` over a `period` of time.
128
+ """Get the cash flow statements for a stock `ticker` over a `period` of time.
132
129
133
130
:param ticker: the stock ticker
134
131
:param period: the period of time to get the balance sheets for.
@@ -183,7 +180,6 @@ def run(self, mode: str, ticker: str, **kwargs: Any) -> str:
183
180
print (str (e ))
184
181
185
182
186
-
187
183
def get_company_profile (symbol : Annotated [str , "ticker symbol" ]) -> str :
188
184
"""Get a company's profile information."""
189
185
profile = finnhub_client .company_profile2 (symbol = symbol )
@@ -310,21 +306,24 @@ def combine_prompt(instruction, resource):
310
306
prompt = f"Resource: { resource } \n \n Instruction: { instruction } "
311
307
return prompt
312
308
309
+
313
310
def analyze_balance_sheet (
314
311
symbol : Annotated [str , "ticker symbol" ],
315
- period : Annotated [str , "the period of time to get the balance sheets for. Possible values are: annual, quarterly, ttm." ],
316
- limit : int = 10
312
+ period : Annotated [
313
+ str , "the period of time to get the balance sheets for. Possible values are: annual, quarterly, ttm."
314
+ ],
315
+ limit : int = 10 ,
317
316
) -> str :
318
- """
319
- Retrieve the balance sheet for the given ticker symbol with the related section of its 10-K report.
317
+ """Retrieve the balance sheet for the given ticker symbol with the related section of its 10-K report.
318
+
320
319
Then return with an instruction on how to analyze the balance sheet.
321
320
"""
322
321
323
322
balance_sheet = financial_datasets_client .run (
324
- mode = "get_balance_sheets" ,
325
- ticker = symbol ,
326
- period = period ,
327
- limit = limit ,
323
+ mode = "get_balance_sheets" ,
324
+ ticker = symbol ,
325
+ period = period ,
326
+ limit = limit ,
328
327
)
329
328
330
329
df_string = "Balance sheet:\n " + balance_sheet
@@ -347,19 +346,21 @@ def analyze_balance_sheet(
347
346
348
347
def analyze_income_stmt (
349
348
symbol : Annotated [str , "ticker symbol" ],
350
- period : Annotated [str , "the period of time to get the balance sheets for. Possible values are: annual, quarterly, ttm." ],
351
- limit : int = 10
349
+ period : Annotated [
350
+ str , "the period of time to get the balance sheets for. Possible values are: annual, quarterly, ttm."
351
+ ],
352
+ limit : int = 10 ,
352
353
) -> str :
353
- """
354
- Retrieve the income statement for the given ticker symbol with the related section of its 10-K report.
354
+ """Retrieve the income statement for the given ticker symbol with the related section of its 10-K report.
355
+
355
356
Then return with an instruction on how to analyze the income statement.
356
357
"""
357
358
# Retrieve the income statement
358
359
income_stmt = financial_datasets_client .run (
359
- mode = "get_income_statements" ,
360
- ticker = symbol ,
361
- period = period ,
362
- limit = limit ,
360
+ mode = "get_income_statements" ,
361
+ ticker = symbol ,
362
+ period = period ,
363
+ limit = limit ,
363
364
)
364
365
df_string = "Income statement:\n " + income_stmt
365
366
@@ -379,19 +380,21 @@ def analyze_income_stmt(
379
380
"""
380
381
)
381
382
382
-
383
383
# Combine the instruction, section text, and income statement
384
384
prompt = combine_prompt (instruction , df_string )
385
385
386
386
return prompt
387
387
388
+
388
389
def analyze_cash_flow (
389
390
symbol : Annotated [str , "ticker symbol" ],
390
- period : Annotated [str , "the period of time to get the balance sheets for. Possible values are: annual, quarterly, ttm." ],
391
- limit : int = 10
391
+ period : Annotated [
392
+ str , "the period of time to get the balance sheets for. Possible values are: annual, quarterly, ttm."
393
+ ],
394
+ limit : int = 10 ,
392
395
) -> str :
393
- """
394
- Retrieve the cash flow statement for the given ticker symbol with the related section of its 10-K report.
396
+ """Retrieve the cash flow statement for the given ticker symbol with the related section of its 10-K report.
397
+
395
398
Then return with an instruction on how to analyze the cash flow statement.
396
399
"""
397
400
@@ -419,37 +422,38 @@ def analyze_cash_flow(
419
422
prompt = combine_prompt (instruction , df_string )
420
423
return prompt
421
424
425
+
422
426
def get_share_performance (
423
427
symbol : Annotated [str , "Ticker symbol of the stock (e.g., 'AAPL' for Apple)" ],
424
428
end_date : Annotated [
425
429
str ,
426
430
"end date of the search period for the company's basic financials, yyyy-mm-dd" ,
427
431
],
428
- ) -> str :
432
+ ) -> str :
429
433
"""Plot the stock performance of a company compared to the S&P 500 over the past year."""
430
434
filing_date = datetime .strptime (end_date , "%Y-%m-%d" )
431
435
432
436
start = (filing_date - timedelta (days = 60 )).strftime ("%Y-%m-%d" )
433
437
end = filing_date .strftime ("%Y-%m-%d" )
434
- interval = ' day' # possible values are {'second', 'minute', 'day', 'week', 'month', 'year'}
435
- interval_multiplier = 1 # every 1 day
438
+ interval = " day" # possible values are {'second', 'minute', 'day', 'week', 'month', 'year'}
439
+ interval_multiplier = 1 # every 1 day
436
440
437
441
# create the URL
438
442
url = (
439
- f' https://api.financialdatasets.ai/prices/'
440
- f' ?ticker={ symbol } '
441
- f' &interval={ interval } '
442
- f' &interval_multiplier={ interval_multiplier } '
443
- f' &start_date={ start } '
444
- f' &end_date={ end } '
443
+ f" https://api.financialdatasets.ai/prices/"
444
+ f" ?ticker={ symbol } "
445
+ f" &interval={ interval } "
446
+ f" &interval_multiplier={ interval_multiplier } "
447
+ f" &start_date={ start } "
448
+ f" &end_date={ end } "
445
449
)
446
450
447
451
headers = {"X-API-KEY" : "your_api_key_here" }
448
452
449
453
response = requests .get (url , headers = headers )
450
454
# parse prices from the response
451
455
452
- prices = response .json ().get (' prices' )
456
+ prices = response .json ().get (" prices" )
453
457
454
458
df_string = "Past 60 days Stock prices:\n " + json .dumps (prices )
455
459
@@ -462,4 +466,3 @@ def get_share_performance(
462
466
463
467
prompt = combine_prompt (instruction , df_string )
464
468
return prompt
465
-
0 commit comments