Source code for langchain_community.utilities.financial_datasets
"""Util that calls several of financial datasets stock market REST APIs.Docs: https://docs.financialdatasets.ai/"""importjsonfromtypingimportAny,List,Optionalimportrequestsfromlangchain_core.utilsimportget_from_dict_or_envfrompydanticimportBaseModelFINANCIAL_DATASETS_BASE_URL="https://api.financialdatasets.ai/"
[docs]classFinancialDatasetsAPIWrapper(BaseModel):"""Wrapper for financial datasets API."""financial_datasets_api_key:Optional[str]=Nonedef__init__(self,**data:Any):super().__init__(**data)self.financial_datasets_api_key=get_from_dict_or_env(data,"financial_datasets_api_key","FINANCIAL_DATASETS_API_KEY")@propertydef_api_key(self)->str:ifself.financial_datasets_api_keyisNone:raiseValueError("API key is required for the FinancialDatasetsAPIWrapper. ""Please provide the API key by either:\n""1. Manually specifying it when initializing the wrapper: ""FinancialDatasetsAPIWrapper(financial_datasets_api_key='your_api_key')\n""2. Setting it as an environment variable: FINANCIAL_DATASETS_API_KEY")returnself.financial_datasets_api_key
[docs]defget_income_statements(self,ticker:str,period:str,limit:Optional[int],)->Optional[dict]:""" Get the income statements for a stock `ticker` over a `period` of time. :param ticker: the stock ticker :param period: the period of time to get the balance sheets for. Possible values are: annual, quarterly, ttm. :param limit: the number of results to return, default is 10 :return: a list of income statements """url=(f"{FINANCIAL_DATASETS_BASE_URL}financials/income-statements/"f"?ticker={ticker}"f"&period={period}"f"&limit={limitiflimitelse10}")# Add the api key to the headersheaders={"X-API-KEY":self._api_key}# Execute the requestresponse=requests.get(url,headers=headers)data=response.json()returndata.get("income_statements",None)
[docs]defget_balance_sheets(self,ticker:str,period:str,limit:Optional[int],)->List[dict]:""" Get the balance sheets for a stock `ticker` over a `period` of time. :param ticker: the stock ticker :param period: the period of time to get the balance sheets for. Possible values are: annual, quarterly, ttm. :param limit: the number of results to return, default is 10 :return: a list of balance sheets """url=(f"{FINANCIAL_DATASETS_BASE_URL}financials/balance-sheets/"f"?ticker={ticker}"f"&period={period}"f"&limit={limitiflimitelse10}")# Add the api key to the headersheaders={"X-API-KEY":self._api_key}# Execute the requestresponse=requests.get(url,headers=headers)data=response.json()returndata.get("balance_sheets",None)
[docs]defget_cash_flow_statements(self,ticker:str,period:str,limit:Optional[int],)->List[dict]:""" Get the cash flow statements for a stock `ticker` over a `period` of time. :param ticker: the stock ticker :param period: the period of time to get the balance sheets for. Possible values are: annual, quarterly, ttm. :param limit: the number of results to return, default is 10 :return: a list of cash flow statements """url=(f"{FINANCIAL_DATASETS_BASE_URL}financials/cash-flow-statements/"f"?ticker={ticker}"f"&period={period}"f"&limit={limitiflimitelse10}")# Add the api key to the headersheaders={"X-API-KEY":self._api_key}# Execute the requestresponse=requests.get(url,headers=headers)data=response.json()returndata.get("cash_flow_statements",None)
[docs]defrun(self,mode:str,ticker:str,**kwargs:Any)->str:ifmode=="get_income_statements":period=kwargs.get("period","annual")limit=kwargs.get("limit",10)returnjson.dumps(self.get_income_statements(ticker,period,limit))elifmode=="get_balance_sheets":period=kwargs.get("period","annual")limit=kwargs.get("limit",10)returnjson.dumps(self.get_balance_sheets(ticker,period,limit))elifmode=="get_cash_flow_statements":period=kwargs.get("period","annual")limit=kwargs.get("limit",10)returnjson.dumps(self.get_cash_flow_statements(ticker,period,limit))else:raiseValueError(f"Invalid mode {mode} for financial datasets API.")