Skip to content

Commit

Permalink
Merge pull request herval#17 from fengb/optional-historical-dates
Browse files Browse the repository at this point in the history
Optional historical dates
  • Loading branch information
herval committed Nov 5, 2014
2 parents fa9c801 + 98ca83a commit 389edfc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 26 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,19 @@ The last parameter (options) can include, besides the "raw" option, a "period" o
The period can be specified as :daily, :monthly, :weekly or :dividends_only

```ruby
data = YahooFinance.historical_quotes("BVSP", Time::now-(24*60*60*10), Time::now) # 10 days worth of data
data = YahooFinance.historical_quotes("AAPL") # entire historical data
```

or

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

or

``` ruby
data = YahooFinance.historical_quotes("BVSP", Time::now-(24*60*60*10), Time::now, { raw: false, period: :monthly })
data = YahooFinance.historical_quotes("AAPL", { raw: false, period: :monthly })
```

### Getting splits
Expand Down
26 changes: 21 additions & 5 deletions lib/yahoo_finance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ def self.quotes(symbols_array, columns_array = [:symbol, :last_trade_price, :las
end
ret
end
def self.historical_quotes(symbol, start_date, end_date, options = {})

def self.historical_quotes(symbol, options = {})
options[:raw] ||= true
options[:period] ||= :daily
read_historical(symbol, start_date, end_date, options).map do |row|
read_historical(symbol, options).map do |row|
OpenStruct.new(row.to_hash.merge(:symbol => symbol))
end
end
Expand Down Expand Up @@ -163,8 +163,24 @@ def self.read_quotes(symb_str, cols)
CSV.parse(conn.read, :headers => cols)
end

def self.read_historical(symbol, start_date, end_date, options)
url = "http://ichart.finance.yahoo.com/table.csv?s=#{URI.escape(symbol)}&d=#{end_date.month-1}&e=#{end_date.day}&f=#{end_date.year}&g=#{HISTORICAL_MODES[options[:period]]}&a=#{start_date.month-1}&b=#{start_date.day}&c=#{start_date.year}&ignore=.csv"
def self.read_historical(symbol, options)
params = {
:s => URI.escape(symbol),
:g => HISTORICAL_MODES[options[:period]],
:ignore => '.csv',
}
if options[:start_date]
params[:a] = options[:start_date].month-1
params[:b] = options[:start_date].day
params[:c] = options[:start_date].year
end
if options[:end_date]
params[:d] = options[:end_date].month-1
params[:e] = options[:end_date].day
params[:f] = options[:end_date].year
end

url = "http://ichart.finance.yahoo.com/table.csv?#{params.map{|k, v| "#{k}=#{v}"}.join('&')}"
conn = open(url)
cols =
if options[:period] == :dividends_only
Expand Down
42 changes: 23 additions & 19 deletions test/yahoo_finance_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@
require File.join(File.dirname(__FILE__),'../lib/yahoo_finance')

class TestYahoo_finance_test < Test::Unit::TestCase
def test_quotes
def days_ago(days)
Time::now-(24*60*60*days)
end

def test_simple_quotes
quotes = YahooFinance.quotes(["BVSP", "AAPL"])
assert_equal(2, quotes.size)

assert_nothing_raised do
q = YahooFinance.historical_quotes("MSFT", Time::now-(24*60*60*40), Time::now, { :raw => false, :period => :daily })
[:trade_date, :open, :high, :low, :close, :volume, :adjusted_close].each do |col|
assert q.first.send(col)
end
end
end

assert_nothing_raised do
q = YahooFinance.historical_quotes("MSFT", Time::now-(24*60*60*400), Time::now, { :raw => false, :period => :dividends_only })
assert q.first.dividend_pay_date
assert q.first.dividend_yield
end
assert_nothing_raised do
YahooFinance.quotes(["AAPL", "MSFT", "BVSP", "JPYUSD" ],
YahooFinance::COLUMNS.take(20).collect { |k, v| v },
{ raw: false })
def test_custom_columns
YahooFinance.quotes(["AAPL", "MSFT", "BVSP", "JPYUSD" ],
YahooFinance::COLUMNS.take(20).collect { |k, v| v },
{ raw: false })
end

def test_historical_quotes
q = YahooFinance.historical_quotes("MSFT", { :raw => false, :period => :daily, :start_date => days_ago(40) })
[:trade_date, :open, :high, :low, :close, :volume, :adjusted_close].each do |col|
assert q.first.send(col)
end
end

Expand All @@ -34,12 +32,18 @@ def test_splits
assert s.first.after
end

def test_dividends
q = YahooFinance.historical_quotes("MSFT", { :raw => false, :period => :dividends_only, :start_date => days_ago(400) })
assert q.first.dividend_pay_date
assert q.first.dividend_yield
end

def test_escapes_symbol_for_url
assert_nothing_raised do
YahooFinance.quotes(["^AXJO"])
end
assert_nothing_raised do
YahooFinance.historical_quotes("^AXJO", Time::now-(24*60*60*400), Time::now)
YahooFinance.historical_quotes("^AXJO", :start_date => days_ago(400))
end
end

Expand Down

0 comments on commit 389edfc

Please sign in to comment.