66import random
77from collections import defaultdict
88from datetime import date , datetime , timedelta
9- from typing import Annotated , Any , List , Optional
109from textwrap import dedent
10+ from typing import Annotated , Any , List , Optional
1111
1212import pandas as pd
1313
3535import requests
3636from pydantic import BaseModel
3737
38-
3938FINANCIAL_DATASETS_BASE_URL = "https://api.financialdatasets.ai/"
4039
40+
4141class FinancialDatasetsAPIWrapper (BaseModel ):
4242 """Wrapper for financial datasets API."""
4343
@@ -65,8 +65,7 @@ def get_income_statements(
6565 period : str ,
6666 limit : Optional [int ],
6767 ) -> 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.
7069
7170 :param ticker: the stock ticker
7271 :param period: the period of time to get the balance sheets for.
@@ -96,8 +95,7 @@ def get_balance_sheets(
9695 period : str ,
9796 limit : Optional [int ],
9897 ) -> 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.
10199
102100 :param ticker: the stock ticker
103101 :param period: the period of time to get the balance sheets for.
@@ -127,8 +125,7 @@ def get_cash_flow_statements(
127125 period : str ,
128126 limit : Optional [int ],
129127 ) -> 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.
132129
133130 :param ticker: the stock ticker
134131 :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:
183180 print (str (e ))
184181
185182
186-
187183def get_company_profile (symbol : Annotated [str , "ticker symbol" ]) -> str :
188184 """Get a company's profile information."""
189185 profile = finnhub_client .company_profile2 (symbol = symbol )
@@ -310,21 +306,24 @@ def combine_prompt(instruction, resource):
310306 prompt = f"Resource: { resource } \n \n Instruction: { instruction } "
311307 return prompt
312308
309+
313310def analyze_balance_sheet (
314311 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 ,
317316) -> 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+
320319 Then return with an instruction on how to analyze the balance sheet.
321320 """
322321
323322 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 ,
328327 )
329328
330329 df_string = "Balance sheet:\n " + balance_sheet
@@ -347,19 +346,21 @@ def analyze_balance_sheet(
347346
348347def analyze_income_stmt (
349348 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 ,
352353) -> 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+
355356 Then return with an instruction on how to analyze the income statement.
356357 """
357358 # Retrieve the income statement
358359 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 ,
363364 )
364365 df_string = "Income statement:\n " + income_stmt
365366
@@ -379,19 +380,21 @@ def analyze_income_stmt(
379380 """
380381 )
381382
382-
383383 # Combine the instruction, section text, and income statement
384384 prompt = combine_prompt (instruction , df_string )
385385
386386 return prompt
387387
388+
388389def analyze_cash_flow (
389390 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 ,
392395) -> 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+
395398 Then return with an instruction on how to analyze the cash flow statement.
396399 """
397400
@@ -419,37 +422,38 @@ def analyze_cash_flow(
419422 prompt = combine_prompt (instruction , df_string )
420423 return prompt
421424
425+
422426def get_share_performance (
423427 symbol : Annotated [str , "Ticker symbol of the stock (e.g., 'AAPL' for Apple)" ],
424428 end_date : Annotated [
425429 str ,
426430 "end date of the search period for the company's basic financials, yyyy-mm-dd" ,
427431 ],
428- ) -> str :
432+ ) -> str :
429433 """Plot the stock performance of a company compared to the S&P 500 over the past year."""
430434 filing_date = datetime .strptime (end_date , "%Y-%m-%d" )
431435
432436 start = (filing_date - timedelta (days = 60 )).strftime ("%Y-%m-%d" )
433437 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
436440
437441 # create the URL
438442 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 } "
445449 )
446450
447451 headers = {"X-API-KEY" : "your_api_key_here" }
448452
449453 response = requests .get (url , headers = headers )
450454 # parse prices from the response
451455
452- prices = response .json ().get (' prices' )
456+ prices = response .json ().get (" prices" )
453457
454458 df_string = "Past 60 days Stock prices:\n " + json .dumps (prices )
455459
@@ -462,4 +466,3 @@ def get_share_performance(
462466
463467 prompt = combine_prompt (instruction , df_string )
464468 return prompt
465-
0 commit comments