Skip to content

market

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

Package market provides Yahoo Finance market status and summary functionality.

Overview

The market package allows retrieving market trading hours, timezone information, and summary data for major market indices. This is useful for determining market state and getting an overview of major indices like S&P 500, Dow Jones, etc.

Basic Usage

m, err := market.New("us_market")
if err != nil {
    log.Fatal(err)
}
defer m.Close()

// Get market status (opening/closing times)
status, err := m.Status()
if err != nil {
    log.Fatal(err)
}
if status.Open != nil {
    fmt.Printf("Market opens at: %s\n", status.Open.Format("15:04"))
}
fmt.Printf("Timezone: %s\n", status.Timezone.Short)

Market Summary

Get an overview of major market indices:

summary, err := m.Summary()
if err != nil {
    log.Fatal(err)
}
for exchange, item := range summary {
    fmt.Printf("%s: %.2f (%.2f%%)\n",
        item.ShortName,
        item.RegularMarketPrice,
        item.RegularMarketChangePercent)
}

Predefined Markets

Common market identifiers are available as constants:

m, _ := market.NewWithPredefined(models.MarketUS)  // US market
m, _ := market.NewWithPredefined(models.MarketJP)  // Japan market
m, _ := market.NewWithPredefined(models.MarketGB)  // UK market

Available predefined markets:

  • MarketUS: United States
  • MarketGB: United Kingdom
  • MarketDE: Germany
  • MarketFR: France
  • MarketJP: Japan
  • MarketHK: Hong Kong
  • MarketCN: China
  • MarketCA: Canada
  • MarketAU: Australia
  • MarketIN: India
  • MarketKR: Korea
  • MarketBR: Brazil

Market State

Check if the market is currently open:

isOpen, err := m.IsOpen()
if isOpen {
    fmt.Println("Market is currently trading")
}

Caching

Market data is cached after the first fetch. To refresh:

m.ClearCache()

Custom Client

Provide a custom HTTP client:

c, _ := client.New()
m, _ := market.New("us_market", market.WithClient(c))

Thread Safety

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

Python Compatibility

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

Python                  | Go
------------------------|---------------------------
yf.Market("us_market")  | market.New("us_market")
market.status           | m.Status()
market.summary          | m.Summary()

Index

type Market

Market provides access to market status and summary information.

Market allows retrieving market opening/closing times, timezone information, and summary data for major market indices.

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

func New

func New(market string, opts ...Option) (*Market, error)

New creates a new Market instance for the given market identifier.

Common market identifiers:

  • "us_market" - United States
  • "gb_market" - United Kingdom
  • "de_market" - Germany
  • "jp_market" - Japan
  • "hk_market" - Hong Kong
  • "cn_market" - China

Example:

m, err := market.New("us_market")
if err != nil {
    log.Fatal(err)
}
defer m.Close()

status, err := m.Status()
fmt.Printf("Market opens at: %s\n", status.Open.Format("15:04"))

func NewWithPredefined

func NewWithPredefined(market models.PredefinedMarket, opts ...Option) (*Market, error)

NewWithPredefined creates a new Market instance using a predefined market constant.

Example:

m, err := market.NewWithPredefined(models.MarketUS)
if err != nil {
    log.Fatal(err)
}

func NewWithRegion

func NewWithRegion(region models.MarketRegion, opts ...Option) (*Market, error)

NewWithRegion creates a new Market instance using a Yahoo market region.

func (*Market) ClearCache

func (m *Market) ClearCache()

ClearCache clears the cached market data. The next call to Status() or Summary() will fetch fresh data.

func (*Market) Close

func (m *Market) Close()

Close releases resources used by the Market instance.

func (*Market) IsOpen

func (m *Market) IsOpen() (bool, error)

IsOpen returns true if the market is currently open. This is determined by checking if the current time is between open and close times.

func (*Market) Market

func (m *Market) Market() string

Market returns the market identifier string.

func (*Market) Status

func (m *Market) Status() (*models.MarketStatus, error)

Status returns the current market status including opening/closing times.

Example:

status, err := m.Status()
if err != nil {
    log.Fatal(err)
}
if status.Open != nil {
    fmt.Printf("Opens at: %s\n", status.Open.Format("15:04"))
}
if status.Timezone != nil {
    fmt.Printf("Timezone: %s\n", status.Timezone.Short)
}

func (*Market) Summary

func (m *Market) Summary() (models.MarketSummary, error)

Summary returns the market summary with major indices data.

The result is a map where keys are exchange codes (e.g., "SNP", "DJI") and values contain price and change information.

Example:

summary, err := m.Summary()
if err != nil {
    log.Fatal(err)
}
for exchange, item := range summary {
    fmt.Printf("%s (%s): %.2f (%.2f%%)\n",
        item.ShortName, exchange,
        item.RegularMarketPrice,
        item.RegularMarketChangePercent)
}

type Option

Option is a function that configures a Market instance.

type Option func(*Market)

func WithClient

func WithClient(c *client.Client) Option

WithClient sets a custom HTTP client for the Market instance.