Fundamental Analysis of Stocks with Python

Jul 21, 2024

Lecture: Fundamental Analysis of Stocks with Python

Introduction

  • Purpose: Learn how to use Python for fundamental analysis of stocks
  • Key Takeaways:
    • Retrieve financial information for any stock (e.g., dividend yield, market cap, P/E ratio)
    • Compare stocks based on fundamentals
    • Example: Finding the highest dividend yield from an index

Libraries Used

  • yfinance - For getting fundamental information
  • pandas - For data handling
  • matplotlib - For visualizing results

Retrieving Stock Information

  1. Creating a Ticker Object
    • Use yf.Ticker('AAPL') to create a ticker object for Apple
  2. Extracting Information
    • Use ticker.info to get a dictionary with detailed information
    • Key data points include sector, business summary, 200-day moving average, beta, trailing P/E ratio, market cap, forward P/E ratio, dividend yield, etc.

Peer Group Comparison

  1. Define Tickers
    • Example: tickers = ['AAPL', 'MSFT', 'IBM']
  2. Loop Through Tickers
    • Create an empty list to gather info: info_list = []
    • Loop: for i in tickers: info_list.append(yf.Ticker(i).info)
  3. Store Efficiently
    • Store results in a SQL database for improved efficiency if needed

Defining Fundamentals

  1. Create Fundamentals List
    • Examples: fundamentals = ['dividendYield', 'marketCap', 'beta', 'forwardPE']
  2. Create DataFrame
    • Convert list of dictionaries into DataFrame for better overview: df = pd.DataFrame(info_list)
  3. Set Index
    • Use symbol as index: df.set_index('symbol', inplace=True)
  4. Filter for Fundamentals
    • Use boolean indexing to filter DataFrame: df_filtered = df[fundamentals]

Visualization

  1. Bar Plot for Dividend Yield
    • Example: Use plt.bar for visual comparison
    • Customize colors and labels for readability
  2. Other Comparisons
    • Similar logic for comparing metrics like P/E ratio

Practical Application: Dow Jones Analysis

  1. Retrieve Dow Jones Ticker Symbols
    • Scrape from Wikipedia: df_tickers = pd.read_html(url)[1]
    • Extract symbols list: tickers = df_tickers['Symbol'].tolist()
  2. Gather Fundamental Data
    • Loop through tickers and gather info in a list
  3. Create DataFrame and Set Index
    • Convert list to DataFrame: df_dow = pd.DataFrame(info_list)
    • Set index to symbol: df_dow.set_index('symbol', inplace=True)
  4. Filter and Visualize
    • Filter for specific fundamental data, e.g., dividend yield
    • Visualize using bar plots

Conclusion

  • Perform personalized analysis by selecting key fundamentals and visualizing results
  • Option to use subplots for comprehensive comparisons
  • Encouragement to explore and customize techniques for better investment decisions

Call to Action

  • Subscribe to channel, like the video, and comment for further content suggestions.