yquotes

package module
v0.0.0-...-617aca0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2020 License: MIT Imports: 5 Imported by: 3

README

Not supported any more

YQuotes

Simple way to get stock quotes from Yahoo Finance.

  • Get stock information (price, name and etc.)
  • Get historical price data

Install

go get github.com/doneland/yquotes

How to use

Get price information of single stock

Client can get price information about stock from Finance Yahoo by calling NewStock(symbol string, history bool) method. It retruns Stock type with recent price information. history property directs wether historical data should be loaded or not.

  // Get stock information without historical data. If you want to load historical
  // data, second argument to TRUE.
  stock, err := yquotes.NewStock("AAPL", false)
  if err != nil {
    // handle error
  }

  symbol := stock.Symbol // AAPL
  name := stock.Name // Apple Inc.
  
  // Price information
  price     := stock.Price // Price struct 
  bid       := price.Bid
  ask       := price.Ask
  open      := price.Open
  prevClose := price.PreviousClose
  last      := price.Last
  date      := price.Date 

Get historical information

History for selected number of years

Function HistoryForYears accepts three parameters: symbol, number of years and frequency (daily, monthly). Frequency is defined by static variables yquotes.[.Daily, .Weekly, .Monthly, .Yearly]

  // Get historical prices for the last 3 years.
  prices, err := yquotes.HistoryForYears("AAPL", 3, yquotes.Daily)
  if err != nil {
    // handle error
  }
}

Get historical prices between two dates

Function GetDailyHistory accepts three arguments: symbol, date1 (from) date2 (to). Function returns list hisptorical prices []PriceH. Dates are of time.Time type.

  // Define layout of date. 
  layout := "2006-01-02"
  from := time.Parse(layout, "2012-01-01")
  to   := time.Now()

  prices, err := yquotes.GetDailyHistory("AAPL", from, to)
  if err != nil {

  }

Data types

Stock type

Notice that properies Price and History have different types of price data. This is because historical data row has different data columns.

  type Stock struct {
    // Symbol of stock that should meet requirements of Yahoo. Otherwise,
    // there will be no possibility to find stock.
    Symbol string `json:"symbol,omitempty"`

    // Name of the company will be filled from request of stock data.
    Name string `json:"name,omitempty"`

    // Information about last price of stock.
    Price *Price `json:"price,omitempty"`

    // Contains historical price information. If client asks information
    // for recent price, this field will be omited.
    History []PriceH `json:"history,omitempty"`
  }

Price type

Price struct represents price in single point in time.

  type Price struct {
    Bid           float64   `json:"bid,omitempty"`
    Ask           float64   `json:"ask,omitempty"`
    Open          float64   `json:"open,omitempty"`
    PreviousClose float64   `json:"previousClose,omitempty"`
    Last          float64   `json:"last,omitempty"`
    Date          time.Time `json:"date,omitempty"`
  }

Historical price type

This type represents row of historical price data.

  type PriceH struct {
    Date     time.Time `json:"date,omitempty"`
    Open     float64   `json:"open,omitempty"`
    High     float64   `json:"high,omitempty"`
    Low      float64   `json:"low,omitempty"`
    Close    float64   `json:"close,omitempty"`
    Volume   float64   `json:"volume,omitempty"`
    AdjClose float64   `json:"adjClose,omitempty"`
  }

Documentation

Index

Constants

View Source
const (
	// URL of Yahoo quotes for stock quotes.
	YURL = "http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s"

	// Base data formating.
	// s - symbol
	// n - name
	// b - bid
	// a - ask
	// o - open
	// p - previous price
	// l1 - last price without time
	// d1 - last trade date
	BaseFormat = "snbaopl1d1"

	// Historical data URL with params:
	// s - symbol
	// a - from month (zero based)
	// b - from day
	// c - from year
	// d - to month (zero based)
	// e - to day
	// f - to year
	// g - period frequence (d - daily, w - weekly, m - monthly, y -yearly)
	YURL_H = "http://ichart.yahoo.com/table.csv?s=%s&a=%d&b=%d&c=%d&d=%d&e=%d&f=%d&g=%s&ignore=.csv"
)
View Source
const (
	Symbol   = "s"
	Name     = "n"
	Bid      = "b"
	Ask      = "a"
	Open     = "o"
	Previous = "p"
	Last     = "l1"
	LastDate = "d1"
)

Formatting constants.

View Source
const (
	Daily   = "d"
	Weekly  = "w"
	Monthly = "m"
	Yearly  = "y"
)

Constance of frequesncy for historical data requests.

Variables

This section is empty.

Functions

This section is empty.

Types

type Price

type Price struct {
	Bid           float64   `json:"bid,omitempty"`
	Ask           float64   `json:"ask,omitempty"`
	Open          float64   `json:"open,omitempty"`
	PreviousClose float64   `json:"previousClose,omitempty"`
	Last          float64   `json:"last,omitempty"`
	Date          time.Time `json:"date,omitempty"`
}

Price struct represents price in single point in time.

func GetPrice

func GetPrice(symbol string) (*Price, error)

Get single stock price data.

type PriceH

type PriceH struct {
	Date     time.Time `json:"date,omitempty"`
	Open     float64   `json:"open,omitempty"`
	High     float64   `json:"high,omitempty"`
	Low      float64   `json:"low,omitempty"`
	Close    float64   `json:"close,omitempty"`
	Volume   float64   `json:"volume,omitempty"`
	AdjClose float64   `json:"adjClose,omitempty"`
}

Price type that is used for historical price data.

func GetDailyHistory

func GetDailyHistory(symbol string, from, to time.Time) ([]PriceH, error)

Get historical prices for the stock.

func GetPriceForDate

func GetPriceForDate(symbol string, date time.Time) (PriceH, error)

Get single stock price for certain date.

func HistoryForYears

func HistoryForYears(symbol string, years int, period string) ([]PriceH, error)

Get stock price history for number of years backwards.

type Stock

type Stock struct {
	// Symbol of stock that should meet requirements of Yahoo. Otherwise,
	// there will be no possibility to find stock.
	Symbol string `json:"symbol,omitempty"`

	// Name of the company will be filled from request of stock data.
	Name string `json:"name,omitempty"`

	// Information about last price of stock.
	Price *Price `json:"price,omitempty"`

	// Contains historical price information. If client asks information
	// for recent price, this field will be omited.
	History []PriceH `json:"history,omitempty"`
}

Stock is used as container for stock price data.

func NewStock

func NewStock(symbol string, history bool) (*Stock, error)

Create new stock with recent price data and historical prices. All the prices are represented in daily basis.

symbol - Symbol of company (ticker) history - If true 3 year price history will be loaded. Returns a pointer to value of Stock type.

Jump to

Keyboard shortcuts

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