Need to generate UUIDs?

YahooFinance - Ruby Module

YahooFinance is a Ruby programming language module for retrieving stock quote information from the finance.yahoo.com web site. It can retrieve current stock quote data or historical quote data.

This module can be "required" and used as a library. Or it can be run from the command-line as a script.

Programatic Usage

The following is example code using the YahooFinance Ruby module to retrieve the most recent stock quote data.

require 'yahoofinance'
# Set the type of quote we want to retrieve.
# Available type are:
# - YahooFinance::StandardQuote
# - YahooFinance::ExtendedQuote
# - YahooFinance::RealTimeQuote
quote_type = YahooFinance::StandardQuote
# Set the symbols for which we want to retrieve quotes.
# You can include more than one symbol by separating
# them with a ',' (comma).
quote_symbols = "yhoo,goog"
# Get the quotes from Yahoo! Finance. The get_quotes method call
# returns a Hash containing one quote object of type "quote_type" for
# each symbol in "quote_symbols". If a block is given, it will be
# called with the quote object (as in the example below).
YahooFinance::get_quotes( quote_type, quote_symbols ) do |qt|
puts "QUOTING: #{qt.symbol}"
puts qt.to_s
end
# You can get the same effect using the quote specific method.
quotes = YahooFinance::get_standard_quotes( quote_symbols )
quotes.each do |symbol, qt|
puts "QUOTING: #{symbol}"
puts qt.to_s
end

The following is an example of using the YahooFinance Ruby module to download historical quote data.

require 'yahoofinance'
# You can get the historical quote data in 2 formats:
# 1. As an array of raw data.
# 2. As a YahooFinance::HistoricalQuote object.
# Getting the historical quote data as a raw array.
# The elements of the array are:
# [0] - Date
# [1] - Open
# [2] - High
# [3] - Low
# [4] - Close
# [5] - Volume
# [6] - Adjusted Close
# The following example will download 30 days worth of qutoes.
# See the note below on the meaning of the number of days.
YahooFinance::get_historical_quotes_days( 'YHOO', 30 ) do |row|
puts "YHOO,#{row.join(',')}"
end
# The API also supportes retrieving quotes based on specific dates.
YahooFinance::get_historical_quotes( 'YHOO',
Date.parse( '2005-09-09' ),
Date.today() ) do |row|
puts "YHOO,#{row.join(',')}"
end
# Getting the data as YahooFinance::HistoricalQuote objects using the
# days API.
YahooFinance::get_HistoricalQuotes_days( 'YHOO', 30 ) do |hq|
puts "#{hq.symbol},#{hq.date},#{hq.open},#{hq.high},#{hq.low},"
+ "#{hq.close},#{hq.volume},#{hq.adjClose}"
end
# Getting the data as YahooFinance::HistoricalQuote objects based on
# specific dates.
YahooFinance::get_HistoricalQuotes( 'YHOO',
Date.parse( '2005-09-09' ),
Date.today() ) do |hq|
puts "#{hq.symbol},#{hq.date},#{hq.open},#{hq.high},#{hq.low},"
+ "#{hq.close},#{hq.volume},#{hq.adjClose}"
end

Note On Historical Quote Retrieval

Requests for historical quotes of stocks on some 'international' exchanges are limited by Yahoo! Finance to 200 data points per request. In order to retrieve all of the data points in one method call, the YahooFinance Ruby module will internally make multiple requests if the 200 data point limit is detected. Thus, users of the YahooFinance Ruby module need not be concerned with this limitation.

The API for historical quotes supports selecting the quotes based on dates or a number of days. When using the number of days, it is important to understand that the number of days given does not correspond to actual business days. It corresponds to calendar days and thus the number of data points returned will almost always be less than the number of days requested.