sector
Package sector provides Yahoo Finance sector data functionality.
Overview
The sector package allows retrieving financial sector information including overview data, top companies, industries within the sector, top ETFs, and top mutual funds. This is useful for sector-based analysis and screening.
Basic Usage
s, err := sector.New("technology")
if err != nil {
log.Fatal(err)
}
defer s.Close()
// Get sector overview
overview, err := s.Overview()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Companies: %d\n", overview.CompaniesCount)
fmt.Printf("Market Cap: $%.2f\n", overview.MarketCap)
Top Companies
Get the top companies within a sector:
companies, err := s.TopCompanies()
if err != nil {
log.Fatal(err)
}
for _, c := range companies {
fmt.Printf("%s: %s (Weight: %.4f)\n",
c.Symbol, c.Name, c.MarketWeight)
}
Industries
Get industries within the sector:
industries, err := s.Industries()
if err != nil {
log.Fatal(err)
}
for _, i := range industries {
fmt.Printf("%s: %s\n", i.Key, i.Name)
}
Top ETFs and Mutual Funds
Get top ETFs and mutual funds for the sector:
Predefined Sectors
Common sector identifiers are available as constants:
s, _ := sector.NewWithPredefined(models.SectorTechnology)
s, _ := sector.NewWithPredefined(models.SectorHealthcare)
s, _ := sector.NewWithPredefined(models.SectorFinancialServices)
Available predefined sectors:
- SectorBasicMaterials: Basic Materials
- SectorCommunicationServices: Communication Services
- SectorConsumerCyclical: Consumer Cyclical
- SectorConsumerDefensive: Consumer Defensive
- SectorEnergy: Energy
- SectorFinancialServices: Financial Services
- SectorHealthcare: Healthcare
- SectorIndustrials: Industrials
- SectorRealEstate: Real Estate
- SectorTechnology: Technology
- SectorUtilities: Utilities
Caching
Sector data is cached after the first fetch. To refresh:
Custom Client
Provide a custom HTTP client:
Thread Safety
All Sector methods are safe for concurrent use from multiple goroutines.
Python Compatibility
This package implements the same functionality as Python yfinance's Sector class:
Python | Go
--------------------------------|--------------------------------
yf.Sector("technology") | sector.New("technology")
sector.key | s.Key()
sector.name | s.Name()
sector.symbol | s.Symbol()
sector.overview | s.Overview()
sector.top_companies | s.TopCompanies()
sector.industries | s.Industries()
sector.top_etfs | s.TopETFs()
sector.top_mutual_funds | s.TopMutualFunds()
sector.research_reports | s.ResearchReports()
Index
- type Option
- func WithClient(c *client.Client) Option
- func WithRegion(region string) Option
- type Sector
- func New(key string, opts ...Option) (*Sector, error)
- func NewWithPredefined(sector models.PredefinedSector, opts ...Option) (*Sector, error)
- func (s *Sector) ClearCache()
- func (s *Sector) Close()
- func (s *Sector) Data() (*models.SectorData, error)
- func (s *Sector) Industries() ([]models.SectorIndustry, error)
- func (s *Sector) Key() string
- func (s *Sector) Name() (string, error)
- func (s *Sector) Overview() (models.SectorOverview, error)
- func (s *Sector) Region() string
- func (s *Sector) ResearchReports() ([]models.ResearchReport, error)
- func (s *Sector) Symbol() (string, error)
- func (s *Sector) TopCompanies() ([]models.SectorTopCompany, error)
- func (s *Sector) TopETFs() (map[string]string, error)
- func (s *Sector) TopMutualFunds() (map[string]string, error)
type Option
Option is a function that configures a Sector instance.
func WithClient
WithClient sets a custom HTTP client for the Sector instance.
func WithRegion
WithRegion scopes regional lists to a Yahoo region such as "US", "GB", or "JP".
type Sector
Sector provides access to financial sector data from Yahoo Finance.
Sector allows retrieving sector-related information such as overview, top companies, industries, top ETFs, and top mutual funds.
func New
New creates a new Sector instance for the given sector key.
Sector keys are lowercase with hyphens, e.g., "technology", "basic-materials". Use predefined constants from models package for convenience.
Example:
s, err := sector.New("technology")
if err != nil {
log.Fatal(err)
}
defer s.Close()
overview, err := s.Overview()
fmt.Printf("Sector has %d companies\n", overview.CompaniesCount)
func NewWithPredefined
NewWithPredefined creates a new Sector instance using a predefined sector constant.
Example:
func (*Sector) ClearCache
ClearCache clears the cached sector data. The next call to any data method will fetch fresh data.
func (*Sector) Close
Close releases resources used by the Sector instance.
func (*Sector) Data
Data returns all sector data.
This fetches and returns the complete sector information including overview, top companies, industries, top ETFs, and mutual funds.
Example:
data, err := s.Data()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Sector: %s\n", data.Name)
fmt.Printf("Companies: %d\n", data.Overview.CompaniesCount)
func (*Sector) Industries
Industries returns the industries within the sector.
Example:
industries, err := s.Industries()
if err != nil {
log.Fatal(err)
}
for _, i := range industries {
fmt.Printf("%s: %s\n", i.Key, i.Name)
}
func (*Sector) Key
Key returns the sector key.
func (*Sector) Name
Name returns the sector name.
Example:
func (*Sector) Overview
Overview returns the sector overview information.
Example:
overview, err := s.Overview()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Companies: %d\n", overview.CompaniesCount)
fmt.Printf("Market Cap: $%.2f\n", overview.MarketCap)
func (*Sector) Region
Region returns the Yahoo region used to scope regional lists.
func (*Sector) ResearchReports
ResearchReports returns research reports for the sector.
Example:
reports, err := s.ResearchReports()
if err != nil {
log.Fatal(err)
}
for _, r := range reports {
fmt.Printf("%s: %s\n", r.Provider, r.Title)
}
func (*Sector) Symbol
Symbol returns the sector symbol.
func (*Sector) TopCompanies
TopCompanies returns the top companies in the sector.
Example:
companies, err := s.TopCompanies()
if err != nil {
log.Fatal(err)
}
for _, c := range companies {
fmt.Printf("%s: %s (Weight: %.2f%%)\n",
c.Symbol, c.Name, c.MarketWeight*100)
}
func (*Sector) TopETFs
TopETFs returns the top ETFs for the sector.
Returns a map of ETF symbols to names.
Example:
etfs, err := s.TopETFs()
if err != nil {
log.Fatal(err)
}
for symbol, name := range etfs {
fmt.Printf("%s: %s\n", symbol, name)
}
func (*Sector) TopMutualFunds
TopMutualFunds returns the top mutual funds for the sector.
Returns a map of mutual fund symbols to names.
Example: