Skip to content

sector

import "github.com/wnjoon/go-yfinance/pkg/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:

etfs, err := s.TopETFs()
funds, err := s.TopMutualFunds()

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:

s.ClearCache()

Custom Client

Provide a custom HTTP client:

c, _ := client.New()
s, _ := sector.New("technology", sector.WithClient(c))

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

Option is a function that configures a Sector instance.

type Option func(*Sector)

func WithClient

func WithClient(c *client.Client) Option

WithClient sets a custom HTTP client for the Sector instance.

func WithRegion

func WithRegion(region string) Option

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.

type Sector struct {
    // contains filtered or unexported fields
}

func New

func New(key string, opts ...Option) (*Sector, error)

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

func NewWithPredefined(sector models.PredefinedSector, opts ...Option) (*Sector, error)

NewWithPredefined creates a new Sector instance using a predefined sector constant.

Example:

s, err := sector.NewWithPredefined(models.SectorTechnology)
if err != nil {
    log.Fatal(err)
}

func (*Sector) ClearCache

func (s *Sector) ClearCache()

ClearCache clears the cached sector data. The next call to any data method will fetch fresh data.

func (*Sector) Close

func (s *Sector) Close()

Close releases resources used by the Sector instance.

func (*Sector) Data

func (s *Sector) Data() (*models.SectorData, error)

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

func (s *Sector) Industries() ([]models.SectorIndustry, error)

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

func (s *Sector) Key() string

Key returns the sector key.

func (*Sector) Name

func (s *Sector) Name() (string, error)

Name returns the sector name.

Example:

name, err := s.Name()
fmt.Println(name) // "Technology"

func (*Sector) Overview

func (s *Sector) Overview() (models.SectorOverview, error)

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

func (s *Sector) Region() string

Region returns the Yahoo region used to scope regional lists.

func (*Sector) ResearchReports

func (s *Sector) ResearchReports() ([]models.ResearchReport, error)

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

func (s *Sector) Symbol() (string, error)

Symbol returns the sector symbol.

func (*Sector) TopCompanies

func (s *Sector) TopCompanies() ([]models.SectorTopCompany, error)

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

func (s *Sector) TopETFs() (map[string]string, error)

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

func (s *Sector) TopMutualFunds() (map[string]string, error)

TopMutualFunds returns the top mutual funds for the sector.

Returns a map of mutual fund symbols to names.

Example:

funds, err := s.TopMutualFunds()
if err != nil {
    log.Fatal(err)
}
for symbol, name := range funds {
    fmt.Printf("%s: %s\n", symbol, name)
}