tournas commited on
Commit
d2a188e
·
verified ·
1 Parent(s): 508fe78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -18
app.py CHANGED
@@ -11,38 +11,41 @@ from tools.final_answer import FinalAnswerTool
11
 
12
  from Gradio_UI import GradioUI
13
 
14
- api_key = os.getenv("ALPHAVANTAGE")
15
-
16
  # Define a tool to retrieve the Discounted Cash Flow (DCF) of a company
17
 
18
  @tool
19
  def get_dcf_of_company(symbol: str) -> str:
20
- """A tool that retrieves the DCF of a company based on its name.
 
21
  Args:
22
- symbol: The symbol name of the company in the stock market, always in capitals.
23
- e.g. AAPL for apple, IBM for IBM
 
 
24
  """
25
  try:
26
-
27
  # Retrieve the company's financial statements
28
  financials = {}
29
  for statement in ['INCOME_STATEMENT', 'BALANCE_SHEET', 'CASH_FLOW']:
30
- url = f'https://www.alphavantage.co/query?function={statement}&symbol={symbol}&apikey=NR9AFISYRYH2B5U3'
31
  response = requests.get(url)
32
- financials[statement] = response.json()
 
 
 
 
33
 
34
  # Extract necessary financial data
35
- # Note: This is a simplified example. In practice, you'd need to handle
36
- # multiple periods, calculate averages, and make growth assumptions.
37
- income_statement = financials['INCOME_STATEMENT']['annualReports'][0]
38
- cash_flow_statement = financials['CASH_FLOW']['annualReports'][0]
39
- balance_sheet = financials['BALANCE_SHEET']['annualReports'][0]
40
-
41
- net_income = float(income_statement['netIncome'])
42
- depreciation = float(cash_flow_statement['depreciationDepletionAndAmortization'])
43
- capex = float(cash_flow_statement['capitalExpenditures'])
44
  change_in_working_capital = (
45
- float(balance_sheet['totalCurrentAssets']) - float(balance_sheet['totalCurrentLiabilities'])
46
  )
47
 
48
  # Calculate Free Cash Flow (FCF)
 
11
 
12
  from Gradio_UI import GradioUI
13
 
 
 
14
  # Define a tool to retrieve the Discounted Cash Flow (DCF) of a company
15
 
16
  @tool
17
  def get_dcf_of_company(symbol: str) -> str:
18
+ """Retrieve the Discounted Cash Flow (DCF) of a company based on its stock symbol.
19
+
20
  Args:
21
+ symbol (str): The stock symbol of the company (e.g., 'AAPL' for Apple, 'IBM' for IBM).
22
+
23
+ Returns:
24
+ str: A message with the DCF value or an error message if the calculation fails.
25
  """
26
  try:
27
+ api_key = 'NR9AFISYRYH2B5U3'
28
  # Retrieve the company's financial statements
29
  financials = {}
30
  for statement in ['INCOME_STATEMENT', 'BALANCE_SHEET', 'CASH_FLOW']:
31
+ url = f'https://www.alphavantage.co/query?function={statement}&symbol={symbol}&apikey={api_key}'
32
  response = requests.get(url)
33
+ data = response.json()
34
+ if 'annualReports' in data:
35
+ financials[statement] = data['annualReports'][0] # Use the most recent annual report
36
+ else:
37
+ return f"Error: Unable to retrieve {statement} for symbol '{symbol}'."
38
 
39
  # Extract necessary financial data
40
+ income_statement = financials['INCOME_STATEMENT']
41
+ cash_flow_statement = financials['CASH_FLOW']
42
+ balance_sheet = financials['BALANCE_SHEET']
43
+
44
+ net_income = float(income_statement.get('netIncome', 0))
45
+ depreciation = float(cash_flow_statement.get('depreciationDepletionAndAmortization', 0)) # Corrected key
46
+ capex = float(cash_flow_statement.get('capitalExpenditures', 0))
 
 
47
  change_in_working_capital = (
48
+ float(balance_sheet.get('totalCurrentAssets', 0)) - float(balance_sheet.get('totalCurrentLiabilities', 0))
49
  )
50
 
51
  # Calculate Free Cash Flow (FCF)