Day 69 of 100 Days Coding Challenge: Python
Today’s stop on the Stock Tracker Express was “Sector Classification,” and I decided to pull into that station before the Diversification Warning stop. Why? Because you can’t warn about lopsided allocations until you know where your money is hiding. Sector classification is like labeling your pantry—if you find 14 jars of peanut butter and no pasta, you might reconsider your meal plan.
In investing, this helps spot “unsystematic risk,” which is just the fancy way of saying, “If one company or industry tanks, you don’t want your whole portfolio going down with it.” Sometimes, yes, you might choose to overload in one sector—maybe tech is hot or utilities are shining—but the point is to know when you’re doing it. With the sector view in place, you can pair it with performance metrics, macroeconomic signals, or just your gut to make better decisions. My roadmap had diversification before classification, but order matters—this way, we start with a map before we set up roadblocks.
Today’s Motivation / Challenge
If you’ve ever played a board game without reading the rules first, you’ll understand why knowing your sector exposure before diversifying is important. Classification turns a pile of tickers into an organized story, letting you see not just what you own, but where it lives.
Purpose of the Code (Object)
This function takes your tracked tickers, peeks under the hood, and tells you what sector and industry they belong to. For ETFs and funds, it makes an educated guess—flagging them as multi-sector or fixed income when appropriate. The idea is to give you a quick visual of portfolio balance before you go hunting for weak spots.
AI Prompt
Creates the following functions: Fetch sector and industry, identify ETFs and label them as multi-sector or fixed income, and finally print a tidy classification table and sector summary.
Functions & Features
- Fetch sector and industry data for each tracked stock.
- Identify ETFs and label them as multi-sector or fixed income.
- Print a tidy classification table and sector summary.
- Optionally export results to CSV.
Requirements / Setup
- Python 3.10+
Install dependencies:
pip install yfinance pandas matplotlib
Minimal Code Sample
import yfinance as yf
def get_sector(symbol):
info = yf.Ticker(symbol).info
return info.get(“sector”), info.get(“industry”)
print(get_sector(“MSFT”)) # (‘Technology’, ‘Software—Infrastructure’)
Pulls sector and industry info for a ticker.
Notes / Lessons Learned
ETFs can be divas—some announce their sector proudly, others just say “multi-sector” or “bond fund” and walk away. Fixed income ETFs (bond funds) make perfect hedges for my tech-heavy holdings, so I labeled those separately. This classification step also reminded me how ambiguous data can be; without it, I might’ve assumed my ETFs were more focused—or more diversified—than they really are.
Optional Ideas for Expansion
- Show top three sector weights for ETFs when available.
- Include historical sector rotation charts.
- Add filters to display only certain sectors.

