Coder Social home page Coder Social logo

yquotes's Introduction

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"`
  }

yquotes's People

Contributors

edgmic avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.