Advertisement
samipote

Untitled

Sep 26th, 2023
750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.34 KB | None | 0 0
  1. import yfinance as yf
  2. from yahooquery import Ticker
  3. from yahoo_fin import stock_info as si
  4.  
  5. def fetch_eps_estimate(ticker_name, replacement_value=0.0):
  6.     data = si.get_analysts_info(ticker_name)
  7.     # Extract the EPS Trend data
  8.     eps_trend = data["EPS Trend"]
  9.     # Locate the 'Next Year' EPS estimate for the given ticker
  10.     eps_next_year_row = eps_trend[eps_trend["EPS Trend"] == "Current Estimate"]
  11.     eps_next_year_column = [col for col in eps_next_year_row.columns if "Next Year" in col][0]
  12.     eps_next_year = eps_next_year_row[eps_next_year_column].values[0]
  13.     print(eps_next_year)
  14.     # Check for NaN or 'nan' string and replace with the replacement_value if needed
  15.     try:
  16.         eps_estimate = float(eps_next_year)
  17.     except ValueError:
  18.         eps_estimate = replacement_value
  19.     return eps_estimate
  20.  
  21. def fetch_growth_estimate(ticker_name, replacement_value=0.0):
  22.     data = si.get_analysts_info(ticker_name)
  23.     growth_estimates = data["Growth Estimates"]
  24.     growth_next_5_years_row = growth_estimates[growth_estimates["Growth Estimates"] == "Next 5 Years (per annum)"]
  25.     growth_next_5_years = growth_next_5_years_row[ticker_name].values[0]
  26.     # Check for NaN and replace with the replacement_value if needed
  27.     print(growth_next_5_years)
  28.     if growth_next_5_years != growth_next_5_years or growth_next_5_years == 'nan':
  29.         return replacement_value
  30.     elif isinstance(growth_next_5_years, str):
  31.         # Check if it's a string before stripping
  32.         return float(growth_next_5_years.strip('%')) / 100 # Convert to a decimal form
  33.     else:
  34.         return growth_next_5_years/100
  35.  
  36. def fetch_data(ticker_name):
  37.     ticker = yf.Ticker(ticker_name)
  38.     # Fetch the Forward EPS (earnings per share) using yahooquery
  39.     eps = fetch_eps_estimate(ticker_name)
  40.     # Fetch the number of shares outstanding
  41.     num_shares = ticker.info['sharesOutstanding']
  42.     # Fetch the growth rate using the provided function
  43.     growth5 = fetch_growth_estimate(ticker_name)
  44.     # Fetch dividend payments to calculate payout ratio
  45.     history = ticker.history(period="5y")
  46.     dividend = history['Dividends'].sum()
  47.     payout_ratio = dividend / (eps * 5) if eps > 0 else 0 # Over 5 years. Assume 0 if no EPS.
  48.     print(payout_ratio)
  49.     return eps, num_shares, growth5, payout_ratio
  50.  
  51. def calculate_intrinsic_value(eps, growth_rate, payout_ratio, discount_rate, n, num_shares):
  52.     perpetual_growth = 0.02 # Assumption: 3% perpetual growth rate for terminal value
  53.     terminal_value = eps * (1 + perpetual_growth) / (discount_rate - perpetual_growth)
  54.     intrinsic_value_per_share = (eps * (1 + growth_rate) * (1 - payout_ratio) / (discount_rate - growth_rate)) + (terminal_value / (1 + discount_rate)**n) / num_shares
  55.     return intrinsic_value_per_share
  56.  
  57. if __name__ == "__main__":
  58.     ticker_name = input("Enter the ticker name: ")
  59.    
  60.     # Fetch the required data
  61.     eps, num_shares, growth_rate, payout_ratio = fetch_data(ticker_name)
  62.  
  63.     # Assumptions
  64.     discount_rate = 0.08 # 8% WACC, can be refined further based on company-specific details
  65.     n = 5 # Forecast for 5 years ahead
  66.    
  67.     # Calculate the intrinsic value
  68.     intrinsic_value = calculate_intrinsic_value(eps, growth_rate, payout_ratio, discount_rate, n, num_shares)
  69.    
  70.     print(f"The intrinsic value per share for {ticker_name} is: ${intrinsic_value:.2f}")
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement