alphavantage

package module
v0.0.0-...-b284339 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2023 License: MIT Imports: 10 Imported by: 0

README

go-alphavantage

A Go wrapper for stock data and stock indicators from the Alpha Vantage API.

Please Note: This project is a work in progress.

Introduction

Alpha Vantage provides a free API for real time financial data and financial indicators. go-alphavantage provides a wrapper to the Alpha Vantage API(http://www.alphavantage.co/). All methods require and API key which can be requested at http://www.alphavantage.co/support/#api-key. Some API calls will require a premium API key. Full Alpha Vantage API documentation is available at http://www.alphavantage.co/documentation.

Implemented Methods

Time Series Stock Data APIs (WIP)
  • Intraday
  • Daily
  • Daily Adjusted
  • Weekly
  • Weekly Adjusted
  • Monthly
  • Monthly Adjusted
  • Quote
  • Ticker Search
Alpha Intelligence (Not Implemented)
Fundamental Data (Not Implemented)
Forex(FX) (Not Implemented)
Cryptocurrencies (Not Implemented)
Economic Indicators (Not Implemented)
Technical Indicators (Not Implemented)

Usage

go get github.com/cwilsn28/go-alphavantage

Add your API token to a .env file in the root of your project directory

ALPHAVANTAGE_TOKEN=<your_api_token>
token, err := alphavantage.GetAPIToken()
if err != nil {
    panic(err)
}

stockAPI := alphavantage.NewStockAPI(token)

/* Test building of query using parameters from Alphavantage API documentation. */
queryParams := alphavantage.QueryParams{
    Symbol:   "IBM",
    Interval: "5min",
}

/* Fetch daily timeseries */
results, err := stockAPI.IntraDay(queryParams)
if err != nil {
    panic(err)
}

...

Documentation

Index

Constants

View Source
const AVBaseURL = "https://alphavantage.co/query"
View Source
const DefaultHTTPTimeout = 80 * time.Second

Variables

This section is empty.

Functions

func GetAPIToken

func GetAPIToken() (string, error)

func TimeSeriesFromJSON

func TimeSeriesFromJSON(filename string, v interface{}) error
---------------------------------------------------------------------

* Read timeseries data from a local .json file. * This function assumes the filename includes the full path to the * input file. * --------------------------------------------------------------------

func TimeSeriesToCSV

func TimeSeriesToCSV(timeseries map[string]OHLCV, filename string) error
---------------------------------------------------------------------

* Write timeseries data to a local .csv file. * This function assumes the filename includes the full path to the * output file. * -------------------------------------------------------------------

Types

type Client

type Client struct {
	HTTPClient *http.Client
	Request    *http.Request
	Query      *url.Values
	// contains filtered or unexported fields
}

func NewClient

func NewClient(apiKey, method string) *Client

Return an instance of an API client object to the caller

func (*Client) ExecuteQuery

func (c *Client) ExecuteQuery(v interface{}) error

Execute an existing API query.

func (*Client) NewQuery

func (c *Client) NewQuery(queryParams QueryParams)

Assemble a new API query.

func (*Client) NewRequest

func (c *Client) NewRequest(reqMethod string) error

Initialize a new HTTP request

type DailyAdjustedTimeSeries

type DailyAdjustedTimeSeries struct {
	MetaData   MetaData         `json:"Meta Data"`
	TimeSeries map[string]OHLCV `json:"Time Series (Daily),omitempty"`
}

type DailyTimeSeries

type DailyTimeSeries struct {
	MetaData   MetaData         `json:"Meta Data"`
	TimeSeries map[string]OHLCV `json:"Time Series (Daily),omitempty"`
}

type GlobalQuote

type GlobalQuote struct {
	Quote Quote `json:"Global Quote"`
}

type IntraDayTimeSeries

type IntraDayTimeSeries struct {
	MetaData        MetaData         `json:"Meta Data"`
	TimeSeries1Min  map[string]OHLCV `json:"Time Series (1min),omitempty"`
	TimeSeries5Min  map[string]OHLCV `json:"Time Series (5min),omitempty"`
	TimeSeries15Min map[string]OHLCV `json:"Time Series (15min),omitempty"`
	TimeSeries30Min map[string]OHLCV `json:"Time Series (30min),omitempty"`
	TimeSeries60Min map[string]OHLCV `json:"Time Series (60min),omitempty"`
}

type MetaData

type MetaData struct {
	Information   string `json:"1. Information"`
	Symbol        string `json:"2. Symbol"`
	LastRefreshed string `json:"3. Last Refreshed"`
	Interval      string `json:"4. Interval,omitempty"`
	OutputSize    string `json:"4. Output Size,5. Output Size,omitempty"`
	TimeZone      string `json:"4. Time Zone,5. Time Zone,6. Time Zone,omitempty"`
}

Metadata fields in the AV response can vary depending on the query. Overload the interval, outputsize, and timezone struct tags to capture the variations in the json response.

type MonthlyAdjustedTimeSeries

type MonthlyAdjustedTimeSeries struct {
	MetaData   MetaData         `json:"Meta Data"`
	TimeSeries map[string]OHLCV `json:"Monthly Adjusted Time Series,omitempty"`
}

type MonthlyTimeSeries

type MonthlyTimeSeries struct {
	MetaData   MetaData         `json:"Meta Data"`
	TimeSeries map[string]OHLCV `json:"Monthly Time Series,omitempty"`
}

type OHLCV

type OHLCV struct {
	Open             string `json:"1. open"`
	High             string `json:"2. high"`
	Low              string `json:"3. low"`
	Close            string `json:"4. close"`
	AdjustedClose    string `json:"5. adjusted close,omitempty"`
	Volume           string `json:"5. volume,6. volume"`
	DividendAmt      string `json:"7. dividend amount,omitempty"`
	SplitCoefficient string `json:"8. split coefficient,omitempty"`
}

Overload the struct tags to account for presence of adjusted close, dividend amount, and split coefficient.

type QueryParams

type QueryParams struct {
	// Required by all timeseries calls.
	Function string
	// Required by all timeseries calls EXCEPT search endpoint.
	Symbol string
	// Required by search endpoint.
	Keywords string
	// Required by intraday endpoint.
	Interval string
	// Optional for all daily timeseries calls.
	OutputSize string
	// Optional for all timeseries calls
	Datatype string
}

func (QueryParams) HasDatatype

func (q QueryParams) HasDatatype() bool

func (QueryParams) HasInterval

func (q QueryParams) HasInterval() bool

func (QueryParams) HasKeywords

func (q QueryParams) HasKeywords() bool

func (QueryParams) HasOutputSize

func (q QueryParams) HasOutputSize() bool

func (QueryParams) HasSymbol

func (q QueryParams) HasSymbol() bool

type Quote

type Quote struct {
	Symbol         string `json:"01. symbol"`
	Open           string `json:"02. open"`
	High           string `json:"03. high"`
	Low            string `json:"04. low"`
	Price          string `json:"05. price"`
	Volume         string `json:"06. volume"`
	LatestTradeDay string `json:"07. latest trading day"`
	PreviousClose  string `json:"08. previous close"`
	Change         string `json:"09. change"`
	ChangePercent  string `json:"10. change percent"`
}

* Models for stock quotes.

type StockAPI

type StockAPI struct {
	Client *Client
}

func NewStockAPI

func NewStockAPI(apiKey string) *StockAPI

Create a new CoreStock API

func (*StockAPI) Daily

func (api *StockAPI) Daily(queryParams QueryParams) (*DailyTimeSeries, error)

func (*StockAPI) DailyAdjusted

func (api *StockAPI) DailyAdjusted(queryParams QueryParams) (*DailyAdjustedTimeSeries, error)

func (*StockAPI) GlobalQuote

func (api *StockAPI) GlobalQuote(queryParams QueryParams) (*GlobalQuote, error)

func (*StockAPI) IntraDay

func (api *StockAPI) IntraDay(queryParams QueryParams) (*IntraDayTimeSeries, error)

TimeSeries methods

func (*StockAPI) Monthly

func (api *StockAPI) Monthly(queryParams QueryParams) (*MonthlyTimeSeries, error)

func (*StockAPI) MonthlyAdjusted

func (api *StockAPI) MonthlyAdjusted(queryParams QueryParams) (*MonthlyAdjustedTimeSeries, error)

func (*StockAPI) TickerSearch

func (api *StockAPI) TickerSearch(queryParams QueryParams) (*TickerSearchList, error)

func (*StockAPI) Weekly

func (api *StockAPI) Weekly(queryParams QueryParams) (*WeeklyTimeSeries, error)

func (*StockAPI) WeeklyAdjusted

func (api *StockAPI) WeeklyAdjusted(queryParams QueryParams) (*WeeklyAdjustedTimeSeries, error)

type TickerSearchList

type TickerSearchList struct {
	BestMatches []TickerSymbol `json:"bestMatches`
}

* Models for ticker search

type TickerSymbol

type TickerSymbol struct {
	Symbol      string `json:"1. symbol"`
	Name        string `json:"2. name"`
	Equity      string `json:"3. type"`
	Region      string `json:"4. region"`
	MarketOpen  string `json:"5. marketOpen"`
	MarketClose string `json:"6. marketClose"`
	TimeZone    string `json:"7. timezone"`
	Currency    string `json:"8. currency"`
	MatchScore  string `json:"9. matchScore"`
}

type WeeklyAdjustedTimeSeries

type WeeklyAdjustedTimeSeries struct {
	MetaData   MetaData         `json:"Meta Data"`
	TimeSeries map[string]OHLCV `json:"Weekly Adjusted Time Series,omitempty"`
}

type WeeklyTimeSeries

type WeeklyTimeSeries struct {
	MetaData   MetaData         `json:"Meta Data"`
	TimeSeries map[string]OHLCV `json:"Weekly Time Series,omitempty"`
}

Jump to

Keyboard shortcuts

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