yahoofinance

package module
v0.0.0-...-5f5c102 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2020 License: MIT Imports: 19 Imported by: 1

README

yahoofinance - Yahoo Finance API in Go

GoDoc

Table of Contents

Installation

$ go get github.com/z-Wind/yahoofinance

Examples

Client
client := GetClient()
yfinance, err := New(client)
History
call := yfinance.History.Period("0050.TW", "1mo", "1d")
history, err := call.Do()

Reference

Documentation

Index

Examples

Constants

View Source
const (
	HOST = "https://query1.finance.yahoo.com/"
)

const strings

Variables

This section is empty.

Functions

func CheckResponse

func CheckResponse(res *http.Response) error

CheckResponse returns an error (of type *Error) if the response status code is not 2xx.

func DecodeResponse

func DecodeResponse(target interface{}, res *http.Response) error

DecodeResponse decodes the body of res into target. If there is no body, target is unchanged.

func GetClient

func GetClient() *http.Client

GetClient get client

func JSONReader

func JSONReader(v interface{}) (io.Reader, error)

JSONReader convert struct to reader for json request

func ResolveRelative

func ResolveRelative(basePath string, elem ...string) string

ResolveRelative join path

func SendRequest

func SendRequest(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error)

SendRequest sends a single HTTP request using the given client. If ctx is non-nil, it sends the request with req.WithContext

Types

type Adjclose

type Adjclose struct {
	Value []float64 `json:"adjclose"`
}

Adjclose Adjclose

type BetweenCall

type BetweenCall struct {
	DefaultCall
	// contains filtered or unexported fields
}

BetweenCall call function

func (*BetweenCall) Do

func (c *BetweenCall) Do() (*Infomation, error)

Do send request

func (*BetweenCall) IncludeAdjustedClose

func (c *BetweenCall) IncludeAdjustedClose(s string) *BetweenCall

IncludeAdjustedClose Adjust Close Default is true

func (*BetweenCall) Interval

func (c *BetweenCall) Interval(s string) *BetweenCall

Interval Default is 1d Valid intervals: 1m,2m,5m,15m,30m,60m,90m,1h,1d,5d,1wk,1mo,3mo Intraday data cannot extend last 60 days

type Chart

type Chart struct {
	Result []Result     `json:"result"`
	Error  ErrorHistory `json:"error"`
}

Chart Chart

type CurrentTradingPeriod

type CurrentTradingPeriod struct {
	Pre     TimeInfo `json:"pre"`
	Regular TimeInfo `json:"regular"`
	Post    TimeInfo `json:"post"`
}

CurrentTradingPeriod CurrentTradingPeriod

type DefaultCall

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

DefaultCall DefaultCall function

func (*DefaultCall) Context

func (c *DefaultCall) Context(ctx context.Context) *DefaultCall

Context sets the context to be used in this call's Do method. Any pending HTTP request will be aborted if the provided context is canceled.

func (*DefaultCall) Header

func (c *DefaultCall) Header() http.Header

Header returns an http.Header that can be modified by the caller to add HTTP headers to the request.

type Dividend

type Dividend struct {
	Amount float64 `json:"amount"`
	Date   int64   `json:"date"`
}

Dividend Dividend

type Error

type Error struct {
	// Code is the HTTP response status code and will always be populated.
	Code int `json:"code,omitempty"`
	// Message is the server response message and is only populated when
	// explicitly referenced by the JSON server response.
	Message string `json:"message,omitempty"`
	// Body is the raw response returned by the server.
	// It is often but not always JSON, depending on how the request fails.
	Body string
	// Header contains the response header fields from the server.
	Header http.Header
}

Error contains an error response from the server.

func (*Error) Error

func (e *Error) Error() string

type ErrorHistory

type ErrorHistory struct {
	Code        string `json:"code"`
	Description string `json:"description"`
}

ErrorHistory ErrorHistory

type Events

type Events struct {
	Dividends map[string]Dividend `json:"dividends"`
	Splits    map[string]Split    `json:"splits"`
}

Events Events

type HistoryService

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

HistoryService get history

func NewHistoryService

func NewHistoryService(s *Service) *HistoryService

NewHistoryService get history

func (*HistoryService) Between

func (r *HistoryService) Between(symbol string, start, end time.Time) *BetweenCall

Between get data in period https://query1.finance.yahoo.com/v8/finance/chart/0050.TW?range=7d&interval=1d&includeAdjustedClose=true&events="div,splits" https://query1.finance.yahoo.com/v8/finance/chart/VTI?period1=-2208988800&period2=1607299200&interval=1d&includeAdjustedClose=true&events="div,splits"

:Parameters:
    period : str
        Valid periods: 1d,5d,1mo,3mo,6mo,1y,2y,5y,10y,ytd,max
        Either Use period parameter or use start and end
    interval : str
        Valid intervals: 1m,2m,5m,15m,30m,60m,90m,1h,1d,5d,1wk,1mo,3mo
        Intraday data cannot extend last 60 days
    start: str
        Download start date string (YYYY-MM-DD) or _datetime.
        Default is 1900-01-01
    end: str
        Download end date string (YYYY-MM-DD) or _datetime.
        Default is now

func (*HistoryService) Period

func (r *HistoryService) Period(symbol, period, interval string) *PeriodCall

Period get data in period https://query1.finance.yahoo.com/v8/finance/chart/0050.TW?range=7d&interval=1d&includeAdjustedClose=true&events="div,splits" https://query1.finance.yahoo.com/v8/finance/chart/VTI?period1=-2208988800&period2=1607299200&interval=1d&includeAdjustedClose=true&events="div,splits"

:Parameters:
    period : str
        Valid periods: 1d,5d,1mo,3mo,6mo,1y,2y,5y,10y,ytd,max
        Either Use period parameter or use start and end
    interval : str
        Valid intervals: 1m,2m,5m,15m,30m,60m,90m,1h,1d,5d,1wk,1mo,3mo
        Intraday data cannot extend last 60 days
    start: str
        Download start date string (YYYY-MM-DD) or _datetime.
        Default is 1900-01-01
    end: str
        Download end date string (YYYY-MM-DD) or _datetime.
        Default is now
Example
client := GetClient()
yfinance, err := New(client)
if err != nil {
	panic(err)
}

call := yfinance.History.Period("0050.TW", "1mo", "1d")
history, err := call.Do()
if err != nil {
	panic(err)
}

fmt.Printf("%+v", history)
Output:

type Indicators

type Indicators struct {
	Quote    []Quote    `json:"quote"`
	Adjclose []Adjclose `json:"adjclose"`
}

Indicators Indicators

type Infomation

type Infomation struct {
	// ServerResponse contains the HTTP response code and headers from the
	// server.
	ServerResponse `json:"-"`
	Chart          Chart `json:"chart"`
}

Infomation Infomation

type Meta

type Meta struct {
	Currency             string               `json:"currency"`
	Symbol               string               `json:"symbol"`
	ExchangeName         string               `json:"exchangeName"`
	InstrumentType       string               `json:"instrumentType"`
	FirstTradeDate       int64                `json:"firstTradeDate"`
	RegularMarketTime    int64                `json:"regularMarketTime"`
	Gmtoffset            int64                `json:"gmtoffset"`
	Timezone             string               `json:"timezone"`
	ExchangeTimezoneName string               `json:"exchangeTimezoneName"`
	RegularMarketPrice   float64              `json:"regularMarketPrice"`
	ChartPreviousClose   float64              `json:"chartPreviousClose"`
	PriceHint            int                  `json:"priceHint"`
	CurrentTradingPeriod CurrentTradingPeriod `json:"currentTradingPeriod"`
	DataGranularity      string               `json:"dataGranularity"`
	Range                string               `json:"range"`
	ValidRanges          []string             `json:"validRanges"`
}

Meta Meta

type PeriodCall

type PeriodCall struct {
	DefaultCall
	// contains filtered or unexported fields
}

PeriodCall call function

func (*PeriodCall) Do

func (c *PeriodCall) Do() (*Infomation, error)

Do send request

func (*PeriodCall) IncludeAdjustedClose

func (c *PeriodCall) IncludeAdjustedClose(s string) *PeriodCall

IncludeAdjustedClose Adjust Close Default is true

type Quote

type Quote struct {
	Volume []float64 `json:"volume"`
	Close  []float64 `json:"close"`
	Open   []float64 `json:"open"`
	High   []float64 `json:"high"`
	Low    []float64 `json:"low"`
}

Quote Quote

type QuoteService

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

QuoteService get history

func NewQuoteService

func NewQuoteService(s *Service) *QuoteService

NewQuoteService get history

func (*QuoteService) RegularMarketPrice

func (r *QuoteService) RegularMarketPrice(symbol string) *RegularMarketPriceCall

RegularMarketPrice get last price https://query1.finance.yahoo.com/v8/finance/chart/0050.TW?range=7d&interval=1d&includeAdjustedClose=true&events="div,splits" https://query1.finance.yahoo.com/v8/finance/chart/VTI?period1=-2208988800&period2=1607299200&interval=1d&includeAdjustedClose=true&events="div,splits"

:Parameters:
    period : str
        Valid periods: 1d,5d,1mo,3mo,6mo,1y,2y,5y,10y,ytd,max
        Either Use period parameter or use start and end
    interval : str
        Valid intervals: 1m,2m,5m,15m,30m,60m,90m,1h,1d,5d,1wk,1mo,3mo
        Intraday data cannot extend last 60 days
    start: str
        Download start date string (YYYY-MM-DD) or _datetime.
        Default is 1900-01-01
    end: str
        Download end date string (YYYY-MM-DD) or _datetime.
        Default is now

type RegularMarketPriceCall

type RegularMarketPriceCall struct {
	DefaultCall
	// contains filtered or unexported fields
}

RegularMarketPriceCall call function

func (*RegularMarketPriceCall) Do

Do send request

type Result

type Result struct {
	Meta       Meta       `json:"meta"`
	Timestamp  []int64    `json:"timestamp"`
	Events     Events     `json:"events"`
	Indicators Indicators `json:"indicators"`
}

Result Result

type ServerResponse

type ServerResponse struct {
	// HTTPStatusCode is the server's response status code. When using a
	// resource method's Do call, this will always be in the 2xx range.
	HTTPStatusCode int
	// Header contains the response header fields from the server.
	Header http.Header
}

ServerResponse is embedded in each Do response and provides the HTTP status code and header sent by the server.

type Service

type Service struct {
	History *HistoryService
	Quote   *QuoteService
	// contains filtered or unexported fields
}

Service Yahoo Finance api

func New

func New(client *http.Client) (*Service, error)

New Yahoo Finance API server

type Split

type Split struct {
	Date        int64  `json:"date"`
	Numerator   int    `json:"numerator"`
	Denominator int    `json:"denominator"`
	SplitRatio  string `json:"splitRatio"`
}

Split Split

type TimeInfo

type TimeInfo struct {
	Timezone  string `json:"timezone"`
	Start     int64  `json:"start"`
	End       int64  `json:"end"`
	Gmtoffset int64  `json:"gmtoffset"`
}

TimeInfo TimeInfo

Jump to

Keyboard shortcuts

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