Skip to content

lookup

import "github.com/wnjoon/go-yfinance/pkg/lookup"

Package lookup provides Yahoo Finance ticker lookup functionality.

Overview

The lookup package allows searching for financial instruments (stocks, ETFs, mutual funds, indices, futures, currencies, and cryptocurrencies) by query string. This is useful for finding ticker symbols when you know the company name or a partial symbol.

Unlike the search package which provides general search across news, lists, and research, lookup is specifically designed for finding ticker symbols with optional filtering by instrument type.

Basic Usage

l, err := lookup.New("Apple")
if err != nil {
    log.Fatal(err)
}
defer l.Close()

// Get all matching instruments
results, err := l.All(10)
if err != nil {
    log.Fatal(err)
}
for _, doc := range results {
    fmt.Printf("%s: %s (%s)\n", doc.Symbol, doc.Name, doc.QuoteType)
}

Filtering by Type

You can filter results by specific instrument types:

// Get only stocks
stocks, err := l.Stock(10)

// Get only ETFs
etfs, err := l.ETF(10)

// Get only mutual funds
funds, err := l.MutualFund(10)

// Get only indices
indices, err := l.Index(10)

// Get only futures
futures, err := l.Future(10)

// Get only currencies
currencies, err := l.Currency(10)

// Get only cryptocurrencies
cryptos, err := l.Cryptocurrency(10)

Pricing Data

By default, lookup results include real-time pricing data such as:

  • RegularMarketPrice: Current market price
  • RegularMarketChange: Price change
  • RegularMarketChangePercent: Percentage change
  • RegularMarketVolume: Trading volume
  • MarketCap: Market capitalization
  • FiftyTwoWeekHigh/Low: 52-week price range

Caching

Lookup results are cached per query/type combination. To clear the cache:

l.ClearCache()

Custom Client

You can provide a custom HTTP client for the Lookup instance:

c, _ := client.New()
l, _ := lookup.New("AAPL", lookup.WithClient(c))

Thread Safety

All Lookup methods are safe for concurrent use from multiple goroutines.

Python Compatibility

This package implements the same functionality as Python yfinance's Lookup class:

Python                      | Go
----------------------------|---------------------------
yf.Lookup("AAPL")           | lookup.New("AAPL")
lookup.get_all(count=10)    | l.All(10)
lookup.get_stock(count=10)  | l.Stock(10)
lookup.get_etf(count=10)    | l.ETF(10)
lookup.get_mutualfund(10)   | l.MutualFund(10)
lookup.get_index(count=10)  | l.Index(10)
lookup.get_future(count=10) | l.Future(10)
lookup.get_currency(10)     | l.Currency(10)
lookup.get_cryptocurrency() | l.Cryptocurrency(10)

Index

type Lookup

Lookup provides Yahoo Finance ticker lookup functionality.

Lookup allows searching for financial instruments by query string, with optional filtering by instrument type.

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

func New

func New(query string, opts ...Option) (*Lookup, error)

New creates a new Lookup instance for the given query.

The query can be a ticker symbol, company name, or partial match.

Example:

l, err := lookup.New("AAPL")
if err != nil {
    log.Fatal(err)
}
defer l.Close()

results, err := l.All(10)
for _, doc := range results {
    fmt.Printf("%s: %s\n", doc.Symbol, doc.Name)
}

func (*Lookup) All

func (l *Lookup) All(count int) ([]models.LookupDocument, error)

All returns all types of financial instruments matching the query.

Parameters:

  • count: Maximum number of results to return (default 25 if \<= 0)

Example:

results, err := l.All(10)
for _, doc := range results {
    fmt.Printf("%s: %s (%s)\n", doc.Symbol, doc.Name, doc.QuoteType)
}

func (*Lookup) ClearCache

func (l *Lookup) ClearCache()

ClearCache clears the cached lookup results.

func (*Lookup) Close

func (l *Lookup) Close()

Close releases resources used by the Lookup instance.

func (*Lookup) Cryptocurrency

func (l *Lookup) Cryptocurrency(count int) ([]models.LookupDocument, error)

Cryptocurrency returns cryptocurrency instruments matching the query.

Parameters:

  • count: Maximum number of results to return (default 25 if \<= 0)

Example:

cryptos, err := l.Cryptocurrency(10)
for _, doc := range cryptos {
    fmt.Printf("%s: %s\n", doc.Symbol, doc.Name)
}

func (*Lookup) Currency

func (l *Lookup) Currency(count int) ([]models.LookupDocument, error)

Currency returns currency instruments matching the query.

Parameters:

  • count: Maximum number of results to return (default 25 if \<= 0)

Example:

currencies, err := l.Currency(10)
for _, doc := range currencies {
    fmt.Printf("%s: %s\n", doc.Symbol, doc.Name)
}

func (*Lookup) ETF

func (l *Lookup) ETF(count int) ([]models.LookupDocument, error)

ETF returns ETF instruments matching the query.

Parameters:

  • count: Maximum number of results to return (default 25 if \<= 0)

Example:

etfs, err := l.ETF(10)
for _, doc := range etfs {
    fmt.Printf("%s: %s\n", doc.Symbol, doc.Name)
}

func (*Lookup) Future

func (l *Lookup) Future(count int) ([]models.LookupDocument, error)

Future returns futures instruments matching the query.

Parameters:

  • count: Maximum number of results to return (default 25 if \<= 0)

Example:

futures, err := l.Future(10)
for _, doc := range futures {
    fmt.Printf("%s: %s\n", doc.Symbol, doc.Name)
}

func (*Lookup) Index

func (l *Lookup) Index(count int) ([]models.LookupDocument, error)

Index returns index instruments matching the query.

Parameters:

  • count: Maximum number of results to return (default 25 if \<= 0)

Example:

indices, err := l.Index(10)
for _, doc := range indices {
    fmt.Printf("%s: %s\n", doc.Symbol, doc.Name)
}

func (*Lookup) MutualFund

func (l *Lookup) MutualFund(count int) ([]models.LookupDocument, error)

MutualFund returns mutual fund instruments matching the query.

Parameters:

  • count: Maximum number of results to return (default 25 if \<= 0)

Example:

funds, err := l.MutualFund(10)
for _, doc := range funds {
    fmt.Printf("%s: %s\n", doc.Symbol, doc.Name)
}

func (*Lookup) Query

func (l *Lookup) Query() string

Query returns the search query string.

func (*Lookup) Stock

func (l *Lookup) Stock(count int) ([]models.LookupDocument, error)

Stock returns equity/stock instruments matching the query.

Parameters:

  • count: Maximum number of results to return (default 25 if \<= 0)

Example:

stocks, err := l.Stock(10)
for _, doc := range stocks {
    fmt.Printf("%s: %s\n", doc.Symbol, doc.Name)
}

type Option

Option is a function that configures a Lookup instance.

type Option func(*Lookup)

func WithClient

func WithClient(c *client.Client) Option

WithClient sets a custom HTTP client for the Lookup instance.