twelvedata

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddBoolParam

func AddBoolParam(params map[string]string, key string, value *bool)

func AddDateParam

func AddDateParam(params map[string]string, key string, value *time.Time, format string)

func AddIntParam

func AddIntParam(params map[string]string, key string, value *int)

func AddStringParam

func AddStringParam(params map[string]string, key string, value *string)

func GetAPICreditsLeft added in v1.0.4

func GetAPICreditsLeft() int

GetAPICreditsLeft returns the last known API credits remaining from TwelveData Returns -1 if no API call has been made yet

Types

type APIClient

type APIClient struct {
	Logger *zap.Logger
	Debug  bool
	Client *HTTPClient
}

func NewAPIClient

func NewAPIClient(cfg Config) (*APIClient, error)

NewAPIClient creates a new API client

func (*APIClient) GetCryptocurrencies

func (c *APIClient) GetCryptocurrencies() (cryptoResponse *CryptoResponse, err error)
func (c *APIClient) GetLogo(req LogoRequest) (logo *Logo, err error)

func (*APIClient) GetQuote

func (c *APIClient) GetQuote(req QuoteRequest) (quote *Quote, err error)

func (*APIClient) GetStocks

func (c *APIClient) GetStocks() (stocksResponse *StocksResponse, err error)

func (*APIClient) GetTimeSeries

func (c *APIClient) GetTimeSeries(req TimeSeriesRequest) (candles *TimeSeriesResponse, err error)

type APIUrl

type APIUrl string

type Config

type Config struct {
	Logger        *zap.Logger
	RestyClient   *resty.Client
	APIKey        string
	APIUrl        APIUrl
	Debug         bool
	RetryCount    *int
	RetryWaitTime *time.Duration
	Timeout       int
}

type Crypto

type Crypto struct {
	Symbol             string   `json:"symbol"`              // Cryptocurrency symbol (e.g. "BTC/USD", "ETH/EUR")
	AvailableExchanges []string `json:"available_exchanges"` // List of exchanges where the cryptocurrency is available (e.g. ["Binance", "Coinbase"])
	CurrencyBase       string   `json:"currency_base"`       // Base currency of the cryptocurrency (e.g. "BTC", "ETH")
	CurrencyQuote      string   `json:"currency_quote"`      // Quote currency of the cryptocurrency (e.g. "USD", "EUR")
}

type CryptoResponse

type CryptoResponse struct {
	Data   []Crypto `json:"data"`   // List of cryptocurrencies
	Status string   `json:"status"` // Status of the response
}

type HTTPClient

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

func (*HTTPClient) Get

func (h *HTTPClient) Get(endpoint string, data map[string]string) (response *resty.Response, err error)
type Logo struct {
	Meta      LogoMeta `json:"meta"`
	URL       string   `json:"url"`        // URL of the logo image (for stocks only)
	LogoBase  string   `json:"logo_base"`  // URL of the logo of the base currency (for crypto and forex only)
	LogoQuote string   `json:"logo_quote"` // URL of the logo of the quote currency (for crypto and forex only)
}

type LogoMeta

type LogoMeta struct {
	Symbol   string `json:"symbol"`
	Exchange string `json:"exchange"`
}

type LogoRequest

type LogoRequest struct {
	Symbol   *string // Required: Symbol of the asset (e.g. "AAPL", "BTC/USD")
	Exchange *string // Exchange code (e.g. "NASDAQ", "Binance")
	MicCode  *string // Market Identifier Code (e.g. "XNAS" for NASDAQ)
	Country  *string // Country code (e.g. "US" or "United States")
}

func (LogoRequest) ToParams

func (req LogoRequest) ToParams() (map[string]string, error)

type Quote

type Quote struct {
	Symbol                string            `json:"symbol"`
	Name                  string            `json:"name"`
	Exchange              string            `json:"exchange"`
	MicCode               string            `json:"mic_code"`
	Currency              string            `json:"currency"`
	DateTime              TDTime            `json:"datetime"`
	Timestamp             TDTime            `json:"timestamp"`
	LastQuoteAt           TDTime            `json:"last_quote_at"`
	Open                  TDFloat64         `json:"open"`
	High                  TDFloat64         `json:"high"`
	Low                   TDFloat64         `json:"low"`
	Close                 TDFloat64         `json:"close"`
	Volume                TDFloat64         `json:"volume"`
	PreviousClose         TDFloat64         `json:"previous_close"`
	Change                TDFloat64         `json:"change"`
	PercentChange         TDFloat64         `json:"percent_change"`
	AverageVolume         TDFloat64         `json:"average_volume"`
	Rolling1DayChange     TDFloat64         `json:"rolling_1day_change"`
	Rolling7DayChange     TDFloat64         `json:"rolling_7day_change"`
	RollingPeriodChange   TDFloat64         `json:"rolling_period_change"`
	IsMarketOpen          bool              `json:"is_market_open"`
	FiftyTwoWeek          QuoteFiftyTwoWeek `json:"fifty_two_week"`
	ExtendedChange        TDFloat64         `json:"extended_change"`
	ExtendedPercentChange TDFloat64         `json:"extended_percent_change"`
	ExtendedPrice         TDFloat64         `json:"extended_price"`
	ExtendedTimestamp     int64             `json:"extended_timestamp"`
}

type QuoteFiftyTwoWeek

type QuoteFiftyTwoWeek struct {
	Low               TDFloat64 `json:"low"`
	High              TDFloat64 `json:"high"`
	LowChange         TDFloat64 `json:"low_change"`
	HighChange        TDFloat64 `json:"high_change"`
	LowChangePercent  TDFloat64 `json:"low_change_percent"`
	HighChangePercent TDFloat64 `json:"high_change_percent"`
	Range             string    `json:"range"`
}

type QuoteInterval

type QuoteInterval string
const (
	QuoteInterval1Min   QuoteInterval = "1min"
	QuoteInterval5Min   QuoteInterval = "5min"
	QuoteInterval15Min  QuoteInterval = "15min"
	QuoteInterval30Min  QuoteInterval = "30min"
	QuoteInterval45Min  QuoteInterval = "45min"
	QuoteInterval1Hour  QuoteInterval = "1h"
	QuoteInterval2Hour  QuoteInterval = "2h"
	QuoteInterval4Hour  QuoteInterval = "4h"
	QuoteInterval1Day   QuoteInterval = "1day"
	QuoteInterval1Week  QuoteInterval = "1week"
	QuoteInterval1Month QuoteInterval = "1month"
)

type QuoteRequest

type QuoteRequest struct {
	Symbol           *string        // Required: Symbol of the asset (e.g. "AAPL", "BTC/USD")
	FIGI             *string        // Financial Instrument Global Identifier
	ISIN             *string        // International Securities Identification Number
	CUSIP            *string        // Committee on Uniform Securities Identification Procedures
	Interval         *QuoteInterval // Time interval for the quotes (e.g. "1min", "1week") defaults to "1day"
	Exchange         *string        // Exchange code (e.g. "NASDAQ", "Binance")
	MicCode          *string        // Market Identifier Code (e.g. "XNAS" for NASDAQ)
	Country          *string        // Country code (e.g. "US" or "United States")
	VolumeTimePeriod *int           // Number of periods for Average Volume
	Type             *string        // Type of asset (e.g. "Digital currency", "Common stock")
	PrePost          *bool          // Include pre/post market data (default is false)
	EOD              *bool          // If true, then return data for closed day
	RollingPeriod    *int           // Number of hours for calculate rolling change at period
	DP               *int           // Number of decimal places for float values. Supports 0-11, default is 5
	TimeZone         *string        // Timezone for the response (e.g. "America/New_York", "UTC"). Defaults to "Exchange"
}

QuoteRequest is the available parameters for a quote request

func (QuoteRequest) ToParams

func (req QuoteRequest) ToParams() (map[string]string, error)

type Stocks

type Stocks struct {
	Symbol   string `json:"symbol"`    // Stock symbol (e.g. "AAPL")
	Name     string `json:"name"`      // Full name of the stock (e.g. "Apple Inc.")
	Currency string `json:"currency"`  // Currency code (e.g. "USD")
	Exchange string `json:"exchange"`  // Exchange code (e.g. "NASDAQ")
	MicCode  string `json:"mic_code"`  // Market Identifier Code (e.g. "XNAS" for NASDAQ)
	Country  string `json:"country"`   // Country code (e.g. "US")
	Type     string `json:"type"`      // Type of the stock (e.g. "Common Stock", "ETF")
	FigiCode string `json:"figi_code"` // Financial Instrument Global Identifier
	CfiCode  string `json:"cfi_code"`  // Classification of Financial Instruments code
	ISIN     string `json:"isin"`      // International Securities Identification Number
	CUSIP    string `json:"cusip"`     // Committee on Uniform Securities Identification Procedures
}

type StocksResponse

type StocksResponse struct {
	Data   []Stocks `json:"data"`  // List of stocks
	Count  int      `json:"count"` // Total number of stocks
	Status string   `json:"status"`
}

type TDFloat64 added in v1.0.6

type TDFloat64 float64

TDFloat64 handles float values from TwelveData API that may contain malformed strings. The API sometimes returns bare signs ("+", "-") or empty strings instead of valid numbers. TDFloat64 defaults to 0 for any unparseable value rather than failing the entire unmarshal.

func (*TDFloat64) UnmarshalJSON added in v1.0.6

func (f *TDFloat64) UnmarshalJSON(data []byte) error

type TDTime

type TDTime struct {
	time.Time
}

TDTime handles the datetime format used by TwelveData API

func (*TDTime) UnmarshalJSON

func (t *TDTime) UnmarshalJSON(data []byte) error

type TDZonedTime

type TDZonedTime struct {
	time.Time
}

type TimeSeriesCandle

type TimeSeriesCandle struct {
	DateTime TDZonedTime `json:"datetime"`
	Open     float64     `json:"open,string"`
	Close    float64     `json:"close,string"`
	High     float64     `json:"high,string"`
	Low      float64     `json:"low,string"`
	Volume   float64     `json:"volume,string"`
}

type TimeSeriesInterval

type TimeSeriesInterval string
const (
	TimeSeriesInterval1Min   TimeSeriesInterval = "1min"
	TimeSeriesInterval5Min   TimeSeriesInterval = "5min"
	TimeSeriesInterval15Min  TimeSeriesInterval = "15min"
	TimeSeriesInterval30Min  TimeSeriesInterval = "30min"
	TimeSeriesInterval45Min  TimeSeriesInterval = "45min"
	TimeSeriesInterval1Hour  TimeSeriesInterval = "1h"
	TimeSeriesInterval2Hour  TimeSeriesInterval = "2h"
	TimeSeriesInterval4Hour  TimeSeriesInterval = "4h"
	TimeSeriesInterval5Hour  TimeSeriesInterval = "5h"
	TimeSeriesInterval1Day   TimeSeriesInterval = "1day"
	TimeSeriesInterval1Week  TimeSeriesInterval = "1week"
	TimeSeriesInterval1Month TimeSeriesInterval = "1month"
)

type TimeSeriesRequest

type TimeSeriesRequest struct {
	Symbol        *string             // Required: Symbol of the asset (e.g. "AAPL", "BTC/USD")
	FIGI          *string             // Financial Instrument Global Identifier
	ISIN          *string             // International Securities Identification Number
	CUSIP         *string             // Committee on Uniform Securities Identification Procedures
	Interval      *TimeSeriesInterval // Required: Time interval for the candles (e.g. "1min", "1day")
	Exchange      *string             // Exchange code (e.g. "NASDAQ", "Binance")
	MicCode       *string             // Market Identifier Code (e.g. "XNAS" for NASDAQ)
	Country       *string             // Country code (e.g. "US" or "United States")
	Type          *string             // Type of asset (e.g. "Digital currency", "Common stock")
	OutputSize    *int                // Number of candles to return (default is 30, max is 5000)
	PrePost       *bool               // Include pre/post market data (default is false)
	DP            *int                // Number of decimal places for float values. Supports 0-11, default is -1 (API automatically determines precision)
	Order         *string             // Sorting order for the results "asc" and "desc" (default is "desc")
	TimeZone      *string             // Timezone for the response (e.g. "America/New_York", "UTC"). Defaults to "Exchange"
	Date          *time.Time          // Specific day to fetch data for (time is ignored)
	StartDate     *time.Time          // Time when the series starts
	EndDate       *time.Time          // Time when the series ends
	PreviousClose *bool               // Include previous close price in the response (default is false)
	Adjust        *string             // Adjusting mode for prices ("none", "dividends", "splits", "all"). Default is "none"
}

TimeSeriesRequest is the available parameters for a time series request

func (TimeSeriesRequest) ToParams

func (req TimeSeriesRequest) ToParams() (map[string]string, error)

type TimeSeriesResponse

type TimeSeriesResponse struct {
	Meta    TimeSeriesResponseMeta `json:"meta"`
	Candles []TimeSeriesCandle     `json:"values"`
}

func (*TimeSeriesResponse) UnmarshalJSON

func (r *TimeSeriesResponse) UnmarshalJSON(data []byte) error

UnmarshalJSON Parses JSON response, first taking the exchange timezone from the meta field (if present) and then parsing each candle's datetime in the correct timezone.

type TimeSeriesResponseMeta

type TimeSeriesResponseMeta struct {
	Symbol           string `json:"symbol"`
	Interval         string `json:"interval"`
	Currency         string `json:"currency"`
	CurrencyBase     string `json:"currency_base"`
	CurrencyQuote    string `json:"currency_quote"`
	ExchangeTimezone string `json:"exchange_timezone"`
	Exchange         string `json:"exchange"`
	MicCode          string `json:"mic_code"`
	Type             string `json:"type"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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