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:
Caching
Market data is cached after the first fetch. To refresh:
Custom Client
Provide a custom HTTP client:
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
- func New(market string, opts ...Option) (*Market, error)
- func NewWithPredefined(market models.PredefinedMarket, opts ...Option) (*Market, error)
- func NewWithRegion(region models.MarketRegion, opts ...Option) (*Market, error)
- func (m *Market) ClearCache()
- func (m *Market) Close()
- func (m *Market) IsOpen() (bool, error)
- func (m *Market) Market() string
- func (m *Market) Status() (*models.MarketStatus, error)
- func (m *Market) Summary() (models.MarketSummary, error)
- type Option
- func WithClient(c *client.Client) Option
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.
func New
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
NewWithPredefined creates a new Market instance using a predefined market constant.
Example:
func NewWithRegion
NewWithRegion creates a new Market instance using a Yahoo market region.
func (*Market) ClearCache
ClearCache clears the cached market data. The next call to Status() or Summary() will fetch fresh data.
func (*Market) Close
Close releases resources used by the Market instance.
func (*Market) IsOpen
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
Market returns the market identifier string.
func (*Market) Status
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
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.
func WithClient
WithClient sets a custom HTTP client for the Market instance.