TransparenTech

An ISV and Software Consultancy

Want to track lots of stock ticker symbols?

Try our stock tracking software now and track 10s, 100s or even 1000s of stocks easily.

The Ultimate Stock Tracking Software



Need to generate UUIDs?

Try our new, free, UUID Generator service.

Quickly and easily generate UUIDs individually or in bulk.

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

Command-Line Usage

The module includes a simple command-line interface.

% yahoofinance.rb -h
Usage: yahoofinance.rb [options] <symbol>

    -s                               Retrieve standard quotes (default).
    -x                               Retrieve extended quotes.
    -r                               Retrieve real-time quotes.
    -z                               Retrieve historical quotes.
    -d, --days N                     Number of days of historical
                                     quotes to retrieve. Default is 90.
    -h, --help                       Show this message

% yahoofinance.rb -x yhoo
Retrieving quotes for: yhoo
QUOTING: YHOO
YahooFinance::ExtendedQuote
movingAve50daysChangePercentFrom = -10.64%
weeks52ChangeFromLow = 2.23
annualizedGain = -
pricePaid = -
weeks52ChangePercentFromLow = +8.95%
weeks52Range = 24.91 - 43.66
stockExchange = NasdaqNM
holdingsGain = -
tradeDate = -
pegRatio = 2.12
dividendYield = N/A
name = YAHOO INC
dayValueChange = - - -1.20%
pricePerEPSEstimateCurrentYear = 56.06
oneYearTargetPrice = 38.30
dividendPerShare = 0.00
shortRatio = 6.00
holdingsValue = -
commission = -
pricePerSales = 6.48
pricePerEPSEstimateNextYear = 41.00
earningsPerShare = 0.848
notes = -
highLimit = -
movingAve50days = 30.372
pricePerBook = 4.47
exDividendDate = 12-May-04
lowLimit = -
movingAve200days = 31.911
bookValue = 6.149
epsEstimateCurrentYear = 0.49
marketCap = 38.235B
peRatio = 32.39
sharesOwned = -
movingAve200daysChangeFrom = -4.771
epsEstimateNextYear = 0.67
symbol = YHOO
movingAve200daysChangePercentFrom = -14.95%
epsEstimateNextQuarter = 0.16
dividendPayDate = N/A
weeks52ChangeFromHigh = -16.52
holdingsGainPercent = - - -
movingAve50daysChangeFrom = -3.232
ebitda = 1.849B
weeks52ChangePercentFromHigh = -37.84%

% yahoofinance.rb -z -d 10 yhoo
Retrieving quotes for: yhoo
yhoo,31-Jul-06,27.46,27.55,26.99,27.14,16492600,27.14
yhoo,28-Jul-06,26.90,27.50,26.33,27.47,21584800,27.47
yhoo,27-Jul-06,27.35,27.50,26.64,26.70,25153000,26.70
yhoo,26-Jul-06,26.78,27.51,26.57,27.08,20073800,27.08
yhoo,25-Jul-06,26.75,27.19,26.57,26.95,21382900,26.95
yhoo,24-Jul-06,26.24,27.23,25.89,26.94,42631300,26.94

Note On Historical Quote Retrieval

Download

Current Version

YahooFinance RubyForge Page

Installation Instructions

RubyGem Installation

# gem install yahoofinance

or

# gem install yahoofinance-<version>.gem

Tarball Installation

% tar xvzf yahoofinance-<version>.tar.gz
% cd yahoofinance-<version>
% ruby setup.rb

You may also be interested in the on-line documentation for setup.rb .

Credits/Complaints/Suggestions/Bugs

License

The YahooFinance Ruby module is released under the GNU General Public License (GPL).