yahoo

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 4, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package yahoo provides a client for fetching quotes from Yahoo Finance.

Index

Constants

This section is empty.

Variables

View Source
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

func NormalizeTicker(sym string) string

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 New

func New(opts ...Option) (*Client, error)

New creates a Client with a cookie jar and optional overrides.

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

func (c *Client) FetchQuotes(ctx context.Context, symbols []string) (map[string]float64, error)

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.

func (*Client) GetQuote

func (c *Client) GetQuote(ctx context.Context, ticker string) (*Quote, error)

GetQuote returns the current price for a ticker. Forex pairs like "USD-EUR" are resolved automatically.

func (*Client) GetYearlyBar added in v0.1.2

func (c *Client) GetYearlyBar(ctx context.Context, ticker string, year int) (*YearlyBar, error)

GetYearlyBar returns yearly OHLC data by aggregating 4 quarters. 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

func WithBaseURL(u string) Option

WithBaseURL overrides the Yahoo Finance API base URL.

func WithCrumb

func WithCrumb(crumb string) Option

WithCrumb injects a pre-fetched crumb, skipping the consent flow.

func WithCrumbURL

func WithCrumbURL(u string) Option

WithCrumbURL overrides the crumb endpoint URL.

func WithHTTPClient

func WithHTTPClient(hc *http.Client) Option

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.

Directories

Path Synopsis
Package mocks provides test doubles for the yahoo package.
Package mocks provides test doubles for the yahoo package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL