Skip to content
Yahoo Stock Quotes in Go
Go
Find file
Pull request Compare This branch is 2 commits behind doneland:master.
Latest commit fa05909 @doneland doneland Readme update
Failed to load latest commit information.
README.md Readme update
yquotes.go Historical prices added
yquotes_test.go Historical prices added

README.md

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"`
  }
Something went wrong with that request. Please try again.