Code Monkey home page Code Monkey logo

google-finance-ruby-client's Introduction

Google Finance Ruby Client

Gem Version Build Status

A Ruby client for the undocumented Google Finance web API. Supports stock quotes and historical prices. Attempts to make sense of, coerce and structure the data.

IANAL, but do note that if your application is for public consumption, using the Google Finance API seems to be against Google's terms of service.

Installation

Add to Gemfile.

gem 'google-finance-ruby-client'

Run bundle install.

Usage

Get a Quote

Fetches stock quotes via https://finance.google.com/finance.

quote = GoogleFinance::Quote.get('MSFT')

quote.last_trade_price # 84.26
quote.change # 0.09
quote.change_in_percent # 0.11
quote.change_in_percent_s # "+0.11%"

See quote.rb for returned fields.

If a symbol cannot be found a GoogleFinance::Errors::SymbolNotFound is raised.

Get Multiple Quotes

Searches for a ticker or tickers, then fetches each quote.

quotes = GoogleFinance::Quotes.search('MSFT', 'AB')

quotes.size # 2

quotes[0] # GoogleFinance::Quote.get('MSFT')
quotes[1] # GoogleFinance::Quote.get('AB')

If one of the symbols cannot be found a GoogleFinance::Errors::SymbolsNotFound is raised.

Get Price History

Daily Price History

Fetches price history for a ticker via https://finance.google.com/finance/historical.

prices = GoogleFinance::History.get('MSFT')

# prices for the last year of open markets
prices.count # 251

# prices appear in reverse chronological order
prices.first # #<GoogleFinance::Price close=85.54 date=#<Date: 2017-12-29> high=86.05 low=85.5 open=85.63 volume=18717406>
prices[1] # #<GoogleFinance::Price close=85.72 date=#<Date: 2017-12-28> high=85.93 low=85.55 open=85.9 volume=10594344>

If a symbol cannot be found a GoogleFinance::Errors::SymbolNotFound is raised.

The following options are supported.

  • start_date: date to start retrieving from
  • end_date: date to retrieve data up to

Retrieve prices in the first trading week of 2016. No trading on the week-end or during holidays.

prices = GoogleFinance::History.get('MSFT', start_date: Date.parse('2016-01-03'), end_date: Date.parse('2016-01-10'))

prices.count # 5
prices.first # #<GoogleFinance::Price close=52.33 date=#<Date: 2016-01-08> high=53.28 low=52.15 open=52.37 volume=48753969>

Intraday Price History

Fetches price history, including at intraday intervals, for a ticker via https://finance.google.com/finance/getprices.

prices = GoogleFinance::Prices.get('MSFT')

prices.exchange # NASDAQ

# prices for the last month of open markets
prices.count # 21

# prices appear in reverse chronological order
prices.last #<GoogleFinance::Price close=85.71 date=2017-12-27 16:00:00 -0500 high=85.98 low=85.215 open=85.65 volume=14678025>
prices[-2] #<GoogleFinance::Price close=85.4 date=2017-12-26 16:00:00 -0500 high=85.5346 low=85.03 open=85.31 volume=9891237>

See price.rb for returned fields.

If a symbol cannot be found a GoogleFinance::Errors::SymbolNotFound is raised.

The following options are supported.

  • exchange: stock exchange symbol on which stock is traded, eg. NASDAQ
  • interval: interval size in seconds
  • period: period, a number followed by d (days) or Y (years)
  • fields: array of data to return
    • date: timestamp
    • open: price at market open
    • close: price at market close
    • volume: volume
    • low: low price
    • high: high price

Retrieve intraday prices in 1 hour intervals.

prices = GoogleFinance::Prices.get('GOOG', interval: 60 * 60, period: '1d')

prices.count # 7

prices # array of GoogleFinance::Price, date=2017-12-29 10:00AM, 11:00AM, etc.

Retrieve only prices at market close.

prices = GoogleFinance::Prices.get('GOOG', fields: [:days, :close])

prices.first # #<GoogleFinance::Price close=1047.41 date=2017-11-28 16:00:00 -0500>

Contributing

See CONTRIBUTING.

Copyright and License

Copyright (c) 2017, Daniel Doubrovkine and Contributors.

This project is licensed under the MIT License.

google-finance-ruby-client's People

Contributors

dblock avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

google-finance-ruby-client's Issues

Support start time in get_prices

Passing ts seems to have no effect, but according to http://www.networkerror.org/component/content/article/1-technical-wootness/44-googles-undocumented-finance-api.html should.

        when :start, :ts then
          query[:ts] = v.to_time.to_i
  context 'with different start', vcr: { cassette_name: 'get_prices_goog_start' } do
    subject do
      GoogleFinance::Prices.get('GOOG', start: Date.parse('2016-1-1 12:00'), fields: %i[date close])
    end
    it 'retrieves prices from 2012' do
      p subject.first
      p subject.last
    end
  end

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.