Skip to content

multi

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

Package multi provides functionality for downloading data for multiple tickers.

Overview

The multi package allows you to efficiently download historical data for multiple stock symbols at once, with optional parallel processing.

Basic Usage

result, err := multi.Download([]string{"AAPL", "MSFT", "GOOGL"}, nil)
if err != nil {
    log.Fatal(err)
}
for symbol, bars := range result.Data {
    fmt.Printf("%s: %d bars\n", symbol, len(bars))
}

With Parameters

params := &models.DownloadParams{
    Period:   "3mo",
    Interval: "1d",
    Threads:  4,
}
result, err := multi.Download([]string{"AAPL", "MSFT"}, params)

Tickers Class

tickers, err := multi.NewTickers([]string{"AAPL", "MSFT", "GOOGL"})
if err != nil {
    log.Fatal(err)
}
defer tickers.Close()

// Access individual ticker
aapl := tickers.Get("AAPL")
info, _ := aapl.Info()

// Download history for all
result, _ := tickers.History(nil)

Thread Safety

All multi package functions are safe for concurrent use.

Index

func Download

func Download(symbols []string, params *models.DownloadParams) (*models.MultiTickerResult, error)

Download is a convenience function to download data for multiple tickers.

Example:

result, err := multi.Download([]string{"AAPL", "MSFT"}, nil)
if err != nil {
    log.Fatal(err)
}
for symbol, bars := range result.Data {
    fmt.Printf("%s: %d bars\n", symbol, len(bars))
}

func DownloadString

func DownloadString(tickerStr string, params *models.DownloadParams) (*models.MultiTickerResult, error)

DownloadString is like Download but accepts a space/comma separated string.

Example:

result, err := multi.DownloadString("AAPL MSFT GOOGL", nil)

type Option

Option is a function that configures Tickers.

type Option func(*Tickers)

func WithClient

func WithClient(c *client.Client) Option

WithClient sets a custom HTTP client.

type Tickers

Tickers represents a collection of multiple ticker symbols.

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

func NewTickers

func NewTickers(symbols []string, opts ...Option) (*Tickers, error)

NewTickers creates a new Tickers instance for multiple symbols.

Symbols can be provided as a slice of strings.

Example:

tickers, err := multi.NewTickers([]string{"AAPL", "MSFT", "GOOGL"})
if err != nil {
    log.Fatal(err)
}
defer tickers.Close()

func NewTickersFromString

func NewTickersFromString(tickerStr string, opts ...Option) (*Tickers, error)

NewTickersFromString creates Tickers from a space or comma separated string.

Example:

tickers, err := multi.NewTickersFromString("AAPL MSFT GOOGL")
// or
tickers, err := multi.NewTickersFromString("AAPL,MSFT,GOOGL")

func (*Tickers) Close

func (t *Tickers) Close()

Close releases all resources used by Tickers.

func (*Tickers) Count

func (t *Tickers) Count() int

Count returns the number of tickers.

func (*Tickers) Download

func (t *Tickers) Download() (*models.MultiTickerResult, error)

Download downloads historical data for all tickers with default parameters.

func (*Tickers) Get

func (t *Tickers) Get(symbol string) *ticker.Ticker

Get returns the Ticker instance for a specific symbol.

Returns nil if the symbol is not found.

func (*Tickers) History

func (t *Tickers) History(params *models.DownloadParams) (*models.MultiTickerResult, error)

History downloads historical data for all tickers.

Example:

result, err := tickers.History(&models.DownloadParams{
    Period:   "1mo",
    Interval: "1d",
})

func (*Tickers) Symbols

func (t *Tickers) Symbols() []string

Symbols returns the list of ticker symbols.