A dead simple wrapper for yahoo finance quotes end-point.
Switch branches/tags
Latest commit 34723b2 Apr 19, 2017 @herval committed on GitHub Merge pull request #41 from wealthsimple/add-currency
Add currency to result


Ruby's Yahoo Finance Wrapper

A dead simple wrapper for yahoo finance quotes end-point.


Just add it to your Gemfile:

gem 'yahoo-finance'


Getting latest quotes for a set of symbols

Pass an array of valid symbols (stock names, indexes, exchange rates) and a list of fields you want:

yahoo_client = YahooFinance::Client.new
data = yahoo_client.quotes(["BVSP", "NATU3.SA", "USDJPY=X"], [:ask, :bid, :last_trade_date])

Data is now an array of results. You now have accessor methods to retrieve the data, with the return results being strings:

puts data[0].symbol + " value is: " + data[0].ask 

Passing raw: false will return numerical values

yahoo_client = YahooFinance::Client.new
data = yahoo_client.quotes(["BVSP", "NATU3.SA", "USDJPY=X"], [:ask, :bid, :last_trade_date], { raw: false } )
data[0].ask # This is now a BigDecimal

Passing na_as_nil: true will convert "N/A" responses to nil

yahoo_client = YahooFinance::Client.new

data = yahoo_client.quotes(["BVSP"], [:ask] )
> "N/A"

data = yahoo_client.quotes(["BVSP"], [:ask], { na_as_nil: true } )
> nil

The full list of fields follows:


Getting symbols by stock market (beta)

Create a YahooFinance::Client instance

yahoo_client = YahooFinance::Client.new

Calling symbols_by_market method (symbols_by_market(country, stock_market))

Note: Can only be called with US Stock Markets for now.

Important: This data comes directly from NASDAQ's CSV endpoints, NOT Yahoo Finance. It might be extracted into a different Gem in the future.

yahoo_client.symbols_by_market('us', 'nyse') # Only US Stock Markets For Now

This method returns an array of symbols that can be used with the quotes method

data = yahoo_client.quotes(yahoo_client.symbols_by_market('us', 'nyse'), [:ask, :bid, :last_trade_date])

Getting historical quotes

Here you can specify a date range and a symbol, and retrieve historical data for it. The last parameter (options) can include, besides the "raw" option, a "period" option. The period can be specified as :daily, :monthly, :weekly or :dividends_only

yahoo_client = YahooFinance::Client.new
data = yahoo_client.historical_quotes("AAPL") # entire historical data


yahoo_client = YahooFinance::Client.new
data = yahoo_client.historical_quotes("AAPL", { start_date: Time::now-(24*60*60*10), end_date: Time::now }) # 10 days worth of data


yahoo_client = YahooFinance::Client.new
data = yahoo_client.historical_quotes("AAPL", { raw: false, period: :monthly })

Getting splits

You can also retrieve split data.

yahoo_client = YahooFinance::Client.new
data = yahoo_client.splits('AAPL', :start_date => Date.today - 10*365)
data[0].date   # Date<2014-06-09>
data[0].before # 1
data[0].after  # 7

Enjoy! :-)