tournas commited on
Commit
f950ece
·
verified ·
1 Parent(s): facce6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -21
app.py CHANGED
@@ -23,7 +23,6 @@ def get_dcf_of_company(company_name: str) -> str:
23
  """
24
  try:
25
  # Step 1: Retrieve the stock ticker symbol
26
- global api_key
27
  search_url = f'https://www.alphavantage.co/query?function=SYMBOL_SEARCH&keywords={company_name}&apikey=NR9AFISYRYH2B5U3'
28
  response = requests.get(search_url)
29
  data = response.json()
@@ -32,35 +31,56 @@ def get_dcf_of_company(company_name: str) -> str:
32
  else:
33
  return f"No stock symbol found for company '{company_name}'."
34
 
35
- # Step 2: Retrieve the company's financial data
36
- overview_url = f'https://www.alphavantage.co/query?function=OVERVIEW&symbol={symbol}&apikey=NR9AFISYRYH2B5U3'
37
- response = requests.get(overview_url)
38
- overview = response.json()
39
- if not overview:
40
- return f"No financial data found for company with symbol '{symbol}'."
41
-
42
- # Extract necessary data
43
- free_cash_flow = float(overview.get('FreeCashFlow', 0))
44
- wacc = float(overview.get('WeightedAverageCostOfCapital', 0)) / 100
45
- growth_rate = float(overview.get('GrowthRate', 0)) / 100
46
-
47
- if free_cash_flow == 0 or wacc == 0:
48
- return f"Insufficient data to calculate DCF for company '{company_name}'."
49
-
50
- # Step 3: Calculate DCF
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  dcf_value = 0
52
- years = 5 # Number of years for forecasting
53
  for year in range(1, years + 1):
54
- future_cash_flow = free_cash_flow * ((1 + growth_rate) ** year)
55
- discounted_cash_flow = future_cash_flow / ((1 + wacc) ** year)
56
  dcf_value += discounted_cash_flow
57
 
58
- return f"The Discounted Cash Flow (DCF) for company '{company_name}' is: ${dcf_value:,.2f}"
 
 
 
 
 
 
 
59
 
60
  except Exception as e:
61
  return f"Error calculating DCF for company '{company_name}': {str(e)}"
62
 
63
 
 
64
  final_answer = FinalAnswerTool()
65
 
66
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
 
23
  """
24
  try:
25
  # Step 1: Retrieve the stock ticker symbol
 
26
  search_url = f'https://www.alphavantage.co/query?function=SYMBOL_SEARCH&keywords={company_name}&apikey=NR9AFISYRYH2B5U3'
27
  response = requests.get(search_url)
28
  data = response.json()
 
31
  else:
32
  return f"No stock symbol found for company '{company_name}'."
33
 
34
+ # Step 2: Retrieve the company's financial statements
35
+ financials = {}
36
+ for statement in ['INCOME_STATEMENT', 'BALANCE_SHEET', 'CASH_FLOW']:
37
+ url = f'https://www.alphavantage.co/query?function={statement}&symbol={symbol}&apikey=NR9AFISYRYH2B5U3'
38
+ response = requests.get(url)
39
+ financials[statement] = response.json()
40
+
41
+ # Extract necessary financial data
42
+ # Note: This is a simplified example. In practice, you'd need to handle
43
+ # multiple periods, calculate averages, and make growth assumptions.
44
+ income_statement = financials['INCOME_STATEMENT']['annualReports'][0]
45
+ cash_flow_statement = financials['CASH_FLOW']['annualReports'][0]
46
+ balance_sheet = financials['BALANCE_SHEET']['annualReports'][0]
47
+
48
+ net_income = float(income_statement['netIncome'])
49
+ depreciation = float(cash_flow_statement['depreciation'])
50
+ capex = float(cash_flow_statement['capitalExpenditures'])
51
+ change_in_working_capital = (
52
+ float(balance_sheet['totalCurrentAssets']) - float(balance_sheet['totalCurrentLiabilities'])
53
+ )
54
+
55
+ # Calculate Free Cash Flow (FCF)
56
+ fcf = net_income + depreciation - capex - change_in_working_capital
57
+
58
+ # Assume a growth rate and discount rate (WACC)
59
+ growth_rate = 0.03 # 3% growth rate
60
+ discount_rate = 0.10 # 10% discount rate
61
+ years = 5 # Number of years to project
62
+
63
+ # Calculate DCF
64
  dcf_value = 0
 
65
  for year in range(1, years + 1):
66
+ future_cash_flow = fcf * ((1 + growth_rate) ** year)
67
+ discounted_cash_flow = future_cash_flow / ((1 + discount_rate) ** year)
68
  dcf_value += discounted_cash_flow
69
 
70
+ # Calculate Terminal Value
71
+ terminal_value = (fcf * (1 + growth_rate)) / (discount_rate - growth_rate)
72
+ discounted_terminal_value = terminal_value / ((1 + discount_rate) ** years)
73
+
74
+ # Total DCF value
75
+ total_value = dcf_value + discounted_terminal_value
76
+
77
+ return f"The Discounted Cash Flow (DCF) for company '{company_name}' is: ${total_value:,.2f}"
78
 
79
  except Exception as e:
80
  return f"Error calculating DCF for company '{company_name}': {str(e)}"
81
 
82
 
83
+
84
  final_answer = FinalAnswerTool()
85
 
86
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder: