Documentation
¶
Overview ¶
Package quote is a free quote downloader library and cli.
It downloads historical price quotes from Tiingo and Coinbase.
Copyright 2025 Mark Chenoweth Licensed under terms of MIT license (see LICENSE)
Index ¶
- Constants
- Variables
- func CheckPeriod(p Provider, period Period) error
- func FormatNames() []string
- func MarketRequiresToken(market string) bool
- func Markets() []string
- func NewEtfFile(filename string) error
- func NewEtfList() ([]string, error)
- func NewMarketFile(market, filename string) error
- func NewMarketList(market string) ([]string, error)deprecated
- func NewSymbolsFromFile(filename string) ([]string, error)
- func ParseDateString(dt string) time.Time
- func PeriodNames() []string
- func PeriodString(p Period) string
- func ProviderNames() []string
- func RegisterProvider(name string, f providerFactory)
- func UpdateFileTiingo(path string, token string, backfillDays int, fullRedownload bool, ...) error
- func ValidMarket(market string) bool
- type Client
- func (c *Client) Binance(ctx context.Context, symbol string, start, end time.Time, period Period) (Quote, error)
- func (c *Client) BinanceSyms(ctx context.Context, symbols []string, from, to time.Time, period Period) (Quotes, error)
- func (c *Client) Coinbase(ctx context.Context, symbol string, start, end time.Time, period Period) (Quote, error)
- func (c *Client) CoinbaseSyms(ctx context.Context, symbols []string, from, to time.Time, period Period) (Quotes, error)
- func (c *Client) FetchAll(ctx context.Context, symbols []string, fetch FetchFunc) (Quotes, error)
- func (c *Client) FetchSymbols(ctx context.Context, p Provider, symbols []string, req Request) (Quotes, error)
- func (c *Client) MarketList(ctx context.Context, market string) ([]string, error)
- func (c *Client) Provider(name string) (Provider, error)
- func (c *Client) TiingoCrypto(ctx context.Context, symbol string, from, to time.Time, period Period, ...) (Quote, error)
- func (c *Client) TiingoCryptoSyms(ctx context.Context, symbols []string, from, to time.Time, period Period, ...) (Quotes, error)
- func (c *Client) TiingoDaily(ctx context.Context, symbol string, from, to time.Time, period Period, ...) (Quote, error)
- func (c *Client) TiingoDailySyms(ctx context.Context, symbols []string, from, to time.Time, period Period, ...) (Quotes, error)
- type FetchFunc
- type Format
- type HTTPError
- type Period
- type Provider
- type Quote
- func NewQuote(symbol string, bars int) Quote
- func NewQuoteFromBinance(symbol string, startDate, endDate string, period Period) (Quote, error)deprecated
- func NewQuoteFromCSV(symbol, csv string) (Quote, error)
- func NewQuoteFromCSVDateFormat(symbol, csv string, format string) (Quote, error)
- func NewQuoteFromCSVFile(symbol, filename string) (Quote, error)
- func NewQuoteFromCSVFileDateFormat(symbol, filename string, format string) (Quote, error)
- func NewQuoteFromCoinbase(symbol, startDate, endDate string, period Period) (Quote, error)deprecated
- func NewQuoteFromJSON(jsn string) (Quote, error)
- func NewQuoteFromJSONFile(filename string) (Quote, error)
- func NewQuoteFromTiingo(symbol, startDate, endDate string, period Period, token string) (Quote, error)deprecated
- func NewQuoteFromTiingoCrypto(symbol, startDate, endDate string, period Period, token string) (Quote, error)deprecated
- func (q Quote) Amibroker() string
- func (q Quote) CSV() string
- func (q Quote) Encode(f Format) (string, error)
- func (q Quote) Highstock() string
- func (q Quote) JSON(indent bool) string
- func (q Quote) WriteAmibroker(filename string) errordeprecated
- func (q Quote) WriteCSV(filename string) errordeprecated
- func (q Quote) WriteFile(filename string, f Format) error
- func (q Quote) WriteHighstock(filename string) errordeprecated
- func (q Quote) WriteJSON(filename string, indent bool) error
- type Quotes
- func NewQuotesFromBinance(filename string, startDate, endDate string, period Period) (Quotes, error)
- func NewQuotesFromBinanceSyms(symbols []string, startDate, endDate string, period Period) (Quotes, error)deprecated
- func NewQuotesFromCSV(csv string) (Quotes, error)
- func NewQuotesFromCSVFile(filename string) (Quotes, error)
- func NewQuotesFromCoinbase(filename, startDate, endDate string, period Period) (Quotes, error)
- func NewQuotesFromCoinbaseSyms(symbols []string, startDate, endDate string, period Period) (Quotes, error)deprecated
- func NewQuotesFromJSON(jsn string) (Quotes, error)
- func NewQuotesFromJSONFile(filename string) (Quotes, error)
- func NewQuotesFromTiingoCryptoSyms(symbols []string, startDate, endDate string, period Period, token string) (Quotes, error)deprecated
- func NewQuotesFromTiingoSyms(symbols []string, startDate, endDate string, period Period, token string) (Quotes, error)deprecated
- func (q Quotes) Amibroker() string
- func (q Quotes) CSV() string
- func (q Quotes) Encode(f Format) (string, error)
- func (q Quotes) Highstock() string
- func (q Quotes) JSON(indent bool) string
- func (q Quotes) WriteAmibroker(filename string) error
- func (q Quotes) WriteCSV(filename string) error
- func (q Quotes) WriteFile(filename string, f Format) error
- func (q Quotes) WriteHighstock(filename string) error
- func (q Quotes) WriteJSON(filename string, indent bool) error
- type Request
- type RetryPolicy
- type SymbolNotFoundError
Constants ¶
const ( // PrecisionEquity - decimal places for equities PrecisionEquity = 2 // PrecisionCrypto - decimal places for cryptocurrencies PrecisionCrypto = 8 )
Decimal places used when formatting prices.
const ClientTimeout = 10 * time.Second
ClientTimeout - connect/read timeout for client requests
Deprecated: set Client.HTTP with the timeout you want.
Variables ¶
var DefaultClient = &Client{}
DefaultClient backs all package-level functions.
var Delay time.Duration
Delay - time delay in milliseconds between quote requests (default=100) Be nice, don't get blocked
Note the units: despite the time.Duration type this holds a raw millisecond count, so Delay = 100 means 100ms. Client.Delay is an honest duration instead.
Deprecated: set Client.Delay instead. This is shared mutable state, read without synchronization by the concurrent update workers.
var Log *log.Logger
Log - standard logger, disabled by default
Deprecated: set Client.Log instead. This package-level logger is shared mutable state; a Client carries its own.
var ValidMarkets = [...]string{
"etf",
"nasdaq",
"nasdaq100",
"amex",
"nyse",
"megacap",
"largecap",
"midcap",
"smallcap",
"microcap",
"nanocap",
"telecommunications",
"health_care",
"finance",
"real_estate",
"consumer_discretionary",
"consumer_staples",
"industrials",
"basic_materials",
"energy",
"utilities",
"technology",
"tiingo-btc",
"tiingo-eth",
"tiingo-usd",
"coinbase",
"binance-usdt",
"binance-usdc",
"binance-btc",
"binance-eth",
}
ValidMarkets list of markets that can be downloaded
Deprecated: use Markets, which returns a slice. The length of an array is part of its type, so every market added here changes the type of this variable.
Functions ¶
func CheckPeriod ¶ added in v0.2.0
CheckPeriod reports whether p supports period.
func FormatNames ¶ added in v0.2.0
func FormatNames() []string
FormatNames lists the supported format names in sorted order.
func MarketRequiresToken ¶ added in v0.2.0
MarketRequiresToken - reports whether a market needs TIINGO_API_TOKEN set. ValidMarket answers only "is this a known market name"; the credential check is separate so callers can report it themselves.
func Markets ¶ added in v0.2.0
func Markets() []string
Markets lists the downloadable markets in sorted order.
func NewEtfFile ¶
NewEtfFile - download a list of etf symbols to a file
func NewEtfList ¶
NewEtfList - download a list of etf symbols to an array of strings
func NewMarketFile ¶
NewMarketFile - download a list of market symbols to a file
func NewMarketList
deprecated
func NewSymbolsFromFile ¶
NewSymbolsFromFile - read symbols from a file
func ParseDateString ¶
ParseDateString - parse a potentially partial date string to Time
func PeriodNames ¶ added in v0.2.0
func PeriodNames() []string
PeriodNames lists the accepted period spellings, shortest first.
func PeriodString ¶ added in v0.2.0
PeriodString returns the canonical spelling of p.
func ProviderNames ¶ added in v0.2.0
func ProviderNames() []string
ProviderNames lists the registered sources in sorted order.
func RegisterProvider ¶ added in v0.2.0
func RegisterProvider(name string, f providerFactory)
RegisterProvider adds a named source to the registry, replacing any existing entry with the same name.
func UpdateFileTiingo ¶
func UpdateFileTiingo(path string, token string, backfillDays int, fullRedownload bool, concurrency int, end time.Time) error
UpdateFileTiingo updates an existing CSV (single or multi-symbol) in place using Tiingo daily data. It preserves original symbol order, rewrites a small overlap window (backfillDays), and optionally fully re-downloads a symbol's history if a corporate action is detected in the overlap.
Types ¶
type Client ¶ added in v0.2.0
type Client struct {
// HTTP is the underlying client. Defaults to a shared client with
// ClientTimeout.
HTTP *http.Client
// Delay is the pause between consecutive symbol requests. Unlike the
// deprecated package-level Delay, this is a real duration: use
// 100*time.Millisecond, not 100.
Delay time.Duration
// Log receives progress and error messages. Defaults to the package-level
// Log, which discards output unless SetOutput is called.
Log *log.Logger
// Token is the Tiingo API token used when a call does not supply one.
Token string
// UserAgent is sent with every request when non-empty.
UserAgent string
// Retry controls retry behavior. The zero value does not retry.
Retry RetryPolicy
// contains filtered or unexported fields
}
Client holds the configuration for fetching quotes: which HTTP client to use, how long to pause between requests, credentials, and where to log.
The package-level functions delegate to DefaultClient, so existing code keeps working unchanged. Prefer a Client for new code: it accepts a context.Context, allows a per-caller timeout and retry policy, and does not rely on mutable package state.
func (*Client) Binance ¶ added in v0.2.0
func (c *Client) Binance(ctx context.Context, symbol string, start, end time.Time, period Period) (Quote, error)
Binance returns Binance historical prices for a symbol.
func (*Client) BinanceSyms ¶ added in v0.2.0
func (c *Client) BinanceSyms(ctx context.Context, symbols []string, from, to time.Time, period Period) (Quotes, error)
BinanceSyms fetches Binance prices for a list of symbols.
func (*Client) Coinbase ¶ added in v0.2.0
func (c *Client) Coinbase(ctx context.Context, symbol string, start, end time.Time, period Period) (Quote, error)
Coinbase returns Coinbase historical prices for a symbol, paging through the 200-bar limit imposed by the candles endpoint.
func (*Client) CoinbaseSyms ¶ added in v0.2.0
func (c *Client) CoinbaseSyms(ctx context.Context, symbols []string, from, to time.Time, period Period) (Quotes, error)
CoinbaseSyms fetches Coinbase prices for a list of symbols.
func (*Client) FetchAll ¶ added in v0.2.0
FetchAll fetches every symbol in order, pausing Client.Delay between requests and skipping symbols that fail. It replaces four copies of the same append/log/sleep loop, one per data source.
func (*Client) FetchSymbols ¶ added in v0.2.0
func (c *Client) FetchSymbols(ctx context.Context, p Provider, symbols []string, req Request) (Quotes, error)
FetchSymbols fetches every symbol through p, pausing Client.Delay between requests and skipping symbols that fail.
func (*Client) MarketList ¶ added in v0.2.0
MarketList downloads the list of symbols for a market.
func (*Client) TiingoCrypto ¶ added in v0.2.0
func (c *Client) TiingoCrypto(ctx context.Context, symbol string, from, to time.Time, period Period, token string) (Quote, error)
TiingoCrypto returns Tiingo crypto historical prices for a symbol.
func (*Client) TiingoCryptoSyms ¶ added in v0.2.0
func (c *Client) TiingoCryptoSyms(ctx context.Context, symbols []string, from, to time.Time, period Period, token string) (Quotes, error)
TiingoCryptoSyms fetches Tiingo crypto prices for a list of symbols.
type FetchFunc ¶ added in v0.2.0
FetchFunc fetches one symbol. FetchAll drives it across a symbol list.
type Format ¶ added in v0.2.0
type Format string
Format identifies an output encoding.
const ( // FormatCSV - comma separated values FormatCSV Format = "csv" // FormatJSON - json FormatJSON Format = "json" // FormatHighstock - Highstock json FormatHighstock Format = "hs" // FormatAmibroker - Amibroker csv FormatAmibroker Format = "ami" )
Supported output formats.
func ParseFormat ¶ added in v0.2.0
ParseFormat converts a user-supplied format string to a Format.
The CLI previously matched formats with an if/else chain that had no final else, so an unrecognized -format silently wrote nothing and reported success.
type HTTPError ¶ added in v0.2.0
type HTTPError struct {
StatusCode int
Status string
URL string
Body []byte
// contains filtered or unexported fields
}
HTTPError reports a non-2xx response from a data source.
type Period ¶
type Period string
Period - for quote history
const ( // Min1 - 1 Minute time period Min1 Period = "60" // Min3 - 3 Minute time period Min3 Period = "3m" // Min5 - 5 Minute time period Min5 Period = "300" // Min15 - 15 Minute time period Min15 Period = "900" // Min30 - 30 Minute time period Min30 Period = "1800" // Min60 - 60 Minute time period Min60 Period = "3600" // Hour2 - 2 hour time period Hour2 Period = "2h" // Hour4 - 4 hour time period Hour4 Period = "4h" // Hour6 - 6 hour time period Hour6 Period = "6h" // Hour8 - 8 hour time period Hour8 Period = "8h" // Hour12 - 12 hour time period Hour12 Period = "12h" // Daily time period Daily Period = "d" // Day3 - 3 day time period Day3 Period = "3d" // Weekly time period Weekly Period = "w" // Monthly time period Monthly Period = "m" )
func ParsePeriod ¶ added in v0.2.0
ParsePeriod converts a user-supplied period string to a Period.
Unlike the CLI helper it replaces, an unrecognized period is an error rather than a silent fallback to Daily - which previously meant asking for "6h" from Coinbase quietly returned daily bars.
type Provider ¶ added in v0.2.0
type Provider interface {
// Name is the source identifier, e.g. "tiingo".
Name() string
// Periods lists the periods this source supports.
Periods() []Period
// Fetch returns the bars for one symbol.
Fetch(ctx context.Context, req Request) (Quote, error)
}
Provider fetches quotes from a single data source.
Adding a source means implementing this and calling RegisterProvider; it no longer requires editing parallel if/else chains in the CLI.
type Quote ¶
type Quote struct {
Symbol string `json:"symbol"`
Precision int64 `json:"-"`
Date []time.Time `json:"date"`
Open []float64 `json:"open"`
High []float64 `json:"high"`
Low []float64 `json:"low"`
Close []float64 `json:"close"`
Volume []float64 `json:"volume"`
}
Quote - structure for historical price data
func NewQuoteFromCSV ¶
NewQuoteFromCSV - parse csv quote string into Quote structure
func NewQuoteFromCSVDateFormat ¶
NewQuoteFromCSVDateFormat - parse csv quote string into Quote structure with specified DateTime format
func NewQuoteFromCSVFile ¶
NewQuoteFromCSVFile - parse csv quote file into Quote structure
func NewQuoteFromCSVFileDateFormat ¶
NewQuoteFromCSVFileDateFormat - parse csv quote file into Quote structure with specified DateTime format
func NewQuoteFromCoinbase
deprecated
func NewQuoteFromJSON ¶
NewQuoteFromJSON - parse json quote string into Quote structure
func NewQuoteFromJSONFile ¶
NewQuoteFromJSONFile - parse json quote string into Quote structure
func (Quote) WriteAmibroker
deprecated
func (Quote) WriteFile ¶ added in v0.2.0
WriteFile writes the Quote to filename in the given format. An empty filename derives one from the symbol.
This replaces the per-format WriteCSV/WriteJSON/WriteHighstock/WriteAmibroker methods, each of which repeated the same default-filename-then-WriteFile body.
func (Quote) WriteHighstock
deprecated
type Quotes ¶
type Quotes []Quote
Quotes - an array of historical price data
func NewQuotesFromBinance ¶ added in v0.2.0
func NewQuotesFromBinance(filename string, startDate, endDate string, period Period) (Quotes, error)
NewQuotesFromBinance - create a list of prices from symbols in file
func NewQuotesFromCSV ¶
NewQuotesFromCSV - parse csv quote string into Quotes array
func NewQuotesFromCSVFile ¶
NewQuotesFromCSVFile - parse csv quote file into Quotes array
func NewQuotesFromCoinbase ¶
NewQuotesFromCoinbase - create a list of prices from symbols in file
func NewQuotesFromJSON ¶
NewQuotesFromJSON - parse json quote string into Quote structure
func NewQuotesFromJSONFile ¶
NewQuotesFromJSONFile - parse json quote string into Quote structure
func (Quotes) WriteAmibroker ¶
WriteAmibroker - write Quotes structure to file Deprecated: use Quotes.WriteFile(filename, FormatAmibroker).
func (Quotes) WriteCSV ¶
WriteCSV - write Quotes structure to file Deprecated: use Quotes.WriteFile(filename, FormatCSV).
func (Quotes) WriteFile ¶ added in v0.2.0
WriteFile writes the Quotes to filename in the given format. An empty filename defaults to "quotes" with the format's extension.
func (Quotes) WriteHighstock ¶
WriteHighstock - write Quote struct to json file in Highstock format Deprecated: use Quotes.WriteFile(filename, FormatHighstock).
type Request ¶ added in v0.2.0
type Request struct {
Symbol string
From time.Time
To time.Time
Period Period
// Token overrides Client.Token for sources that need credentials.
Token string
}
Request describes one symbol fetch.
type RetryPolicy ¶ added in v0.2.0
type RetryPolicy struct {
// Max is the number of additional attempts after the first.
Max int
// Backoff is the base delay, doubled after each attempt. Defaults to
// 500ms when Max > 0 and Backoff is zero.
Backoff time.Duration
}
RetryPolicy controls retries for transport errors, 429, and 5xx responses. The zero value performs no retries, which is the historical behavior; opt in by setting Max. A Retry-After header on a 429 takes precedence over Backoff.
type SymbolNotFoundError ¶
type SymbolNotFoundError struct {
Symbol string
}
SymbolNotFoundError indicates that a symbol was not found in the data source. This error type allows callers to distinguish "not found" from other errors.
func (*SymbolNotFoundError) Error ¶
func (e *SymbolNotFoundError) Error() string