Documentation
¶
Overview ¶
Package yahoo provides a client for fetching quotes from Yahoo Finance.
Index ¶
- Variables
- func NormalizeTicker(sym string) string
- type Client
- func (c *Client) FetchFXRates(ctx context.Context, currencies []string, base string) (map[string]float64, error)
- func (c *Client) FetchQuotes(ctx context.Context, symbols []string) (map[string]float64, error)
- func (c *Client) GetMonthlyBar(ctx context.Context, ticker string, year, month int) (*HistoricalBar, error)
- func (c *Client) GetQuote(ctx context.Context, ticker string) (*Quote, error)
- func (c *Client) GetYearlyBar(ctx context.Context, ticker string, year int) (*YearlyBar, error)
- type HistoricalBar
- type Option
- type Quote
- type YearlyBar
Constants ¶
This section is empty.
Variables ¶
var ( // ErrTickerNotFound is returned when the requested symbol has no results on Yahoo Finance. ErrTickerNotFound = errors.New("ticker not found") // ErrAPIError is returned when Yahoo Finance responds with a non-200 status or an API-level error. ErrAPIError = errors.New("yahoo finance api error") // ErrNoData is returned when Yahoo Finance has no data for the requested period (e.g. future dates or delisted symbols). ErrNoData = errors.New("no data available for the requested period") )
Functions ¶
func NormalizeTicker ¶ added in v0.1.3
NormalizeTicker converts broker tickers to Yahoo Finance format. e.g. "BRK B" → "BRK-B"
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client fetches real-time and historical quotes from Yahoo Finance. Use New to create a Client; it handles the session cookie and crumb handshake required by the Yahoo Finance API automatically.
func (*Client) FetchFXRates ¶ added in v0.1.3
func (c *Client) FetchFXRates(ctx context.Context, currencies []string, base string) (map[string]float64, error)
FetchFXRates returns spot rates for each currency relative to base (e.g. "USD"), fetching in parallel via the v8 chart endpoint. The base currency always gets rate 1.0. Partial results are returned when only some fetches fail.
func (*Client) FetchQuotes ¶ added in v0.1.3
FetchQuotes returns a map of symbol → current price for each symbol in the list, fetching in parallel via the v8 chart endpoint (no crumb required). Both the original and normalized ticker are stored in the result map. Partial results are returned when only some fetches fail.
func (*Client) GetMonthlyBar ¶ added in v0.1.2
func (c *Client) GetMonthlyBar(ctx context.Context, ticker string, year, month int) (*HistoricalBar, error)
GetMonthlyBar returns the OHLC data for a symbol in a given month. Forex pairs like "USD-EUR" are resolved automatically.
type HistoricalBar ¶ added in v0.1.2
type HistoricalBar struct {
Symbol string `json:"symbol"` // Yahoo Finance ticker
Year int `json:"year"` // Calendar year (e.g. 2024)
Month int `json:"month"` // Calendar month (1–12)
Open float64 `json:"open"`
High float64 `json:"high"`
Low float64 `json:"low"`
Close float64 `json:"close"`
Avg float64 `json:"avg"` // (Open + High + Low + Close) / 4
}
HistoricalBar holds OHLC price data for a single calendar month. Avg is the simple average of Open, High, Low, and Close.
type Option ¶
type Option func(*Client)
Option is a functional option for configuring a Client.
func WithBaseURL ¶
WithBaseURL overrides the Yahoo Finance API base URL.
func WithCrumbURL ¶
WithCrumbURL overrides the crumb endpoint URL.
func WithHTTPClient ¶
WithHTTPClient overrides the default HTTP client.
type Quote ¶
type Quote struct {
Symbol string `json:"symbol"` // Yahoo Finance ticker (e.g. "AAPL", "BTC-USD", "USD-EUR")
Price float64 `json:"price"` // Regular market price
Currency string `json:"currency"` // ISO 4217 currency code (e.g. "USD", "EUR")
}
Quote holds the current price data returned for a single symbol.
type YearlyBar ¶ added in v0.1.2
type YearlyBar struct {
Symbol string `json:"symbol"` // Yahoo Finance ticker
Year int `json:"year"` // Calendar year (e.g. 2024)
Open float64 `json:"open"`
High float64 `json:"high"`
Low float64 `json:"low"`
Close float64 `json:"close"`
Avg float64 `json:"avg"` // (Open + High + Low + Close) / 4
}
YearlyBar holds OHLC price data aggregated across a full calendar year. Open comes from Q1, Close from Q4, High and Low are the extremes across all four quarters. Avg is the simple average of Open, High, Low, and Close.