Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import yfinance as yf
- from yahooquery import Ticker
- from yahoo_fin import stock_info as si
- def fetch_eps_estimate(ticker_name, replacement_value=0.0):
- data = si.get_analysts_info(ticker_name)
- # Extract the EPS Trend data
- eps_trend = data["EPS Trend"]
- # Locate the 'Next Year' EPS estimate for the given ticker
- eps_next_year_row = eps_trend[eps_trend["EPS Trend"] == "Current Estimate"]
- eps_next_year_column = [col for col in eps_next_year_row.columns if "Next Year" in col][0]
- eps_next_year = eps_next_year_row[eps_next_year_column].values[0]
- print(eps_next_year)
- # Check for NaN or 'nan' string and replace with the replacement_value if needed
- try:
- eps_estimate = float(eps_next_year)
- except ValueError:
- eps_estimate = replacement_value
- return eps_estimate
- def fetch_growth_estimate(ticker_name, replacement_value=0.0):
- data = si.get_analysts_info(ticker_name)
- growth_estimates = data["Growth Estimates"]
- growth_next_5_years_row = growth_estimates[growth_estimates["Growth Estimates"] == "Next 5 Years (per annum)"]
- growth_next_5_years = growth_next_5_years_row[ticker_name].values[0]
- # Check for NaN and replace with the replacement_value if needed
- print(growth_next_5_years)
- if growth_next_5_years != growth_next_5_years or growth_next_5_years == 'nan':
- return replacement_value
- elif isinstance(growth_next_5_years, str):
- # Check if it's a string before stripping
- return float(growth_next_5_years.strip('%')) / 100 # Convert to a decimal form
- else:
- return growth_next_5_years/100
- def fetch_data(ticker_name):
- ticker = yf.Ticker(ticker_name)
- # Fetch the Forward EPS (earnings per share) using yahooquery
- eps = fetch_eps_estimate(ticker_name)
- # Fetch the number of shares outstanding
- num_shares = ticker.info['sharesOutstanding']
- # Fetch the growth rate using the provided function
- growth5 = fetch_growth_estimate(ticker_name)
- # Fetch dividend payments to calculate payout ratio
- history = ticker.history(period="5y")
- dividend = history['Dividends'].sum()
- payout_ratio = dividend / (eps * 5) if eps > 0 else 0 # Over 5 years. Assume 0 if no EPS.
- print(payout_ratio)
- return eps, num_shares, growth5, payout_ratio
- def calculate_intrinsic_value(eps, growth_rate, payout_ratio, discount_rate, n, num_shares):
- perpetual_growth = 0.02 # Assumption: 3% perpetual growth rate for terminal value
- terminal_value = eps * (1 + perpetual_growth) / (discount_rate - perpetual_growth)
- intrinsic_value_per_share = (eps * (1 + growth_rate) * (1 - payout_ratio) / (discount_rate - growth_rate)) + (terminal_value / (1 + discount_rate)**n) / num_shares
- return intrinsic_value_per_share
- if __name__ == "__main__":
- ticker_name = input("Enter the ticker name: ")
- # Fetch the required data
- eps, num_shares, growth_rate, payout_ratio = fetch_data(ticker_name)
- # Assumptions
- discount_rate = 0.08 # 8% WACC, can be refined further based on company-specific details
- n = 5 # Forecast for 5 years ahead
- # Calculate the intrinsic value
- intrinsic_value = calculate_intrinsic_value(eps, growth_rate, payout_ratio, discount_rate, n, num_shares)
- print(f"The intrinsic value per share for {ticker_name} is: ${intrinsic_value:.2f}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement