class Yfinance
Constants
- COLUMNS
- HISTORICAL_MODES
- SAVE_OPTIONS
these options do not mess up the CSV parser by including ',' in the data fields
Attributes
error_message[R]
Public Class Methods
new(max_concurrency: 20, memoize: false)
click to toggle source
# File lib/yfinance.rb, line 115 def initialize(max_concurrency: 20, memoize: false) @hydra = Typhoeus::Hydra.new(max_concurrency: max_concurrency) Typhoeus::Config.memoize = memoize @error_message = "Symbol not found" end
Public Instance Methods
add_historical_data_query(symbols_array, start_date, end_date, options={}, &callback)
click to toggle source
# File lib/yfinance.rb, line 125 def add_historical_data_query(symbols_array, start_date, end_date, options={}, &callback) start_date = Date.parse(start_date) unless start_date.is_a?(Date) end_date = Date.parse(end_date) unless end_date.is_a?(Date) options = {} options[:raw] ||= true options[:period] ||= :daily symbols_array.each do |symbol| symbol = symbol.rstrip.upcase 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" @hydra.queue(make_request(url, symbol, options, callback)) end end
daily_historical_data(symbols_array, start_date, end_date)
click to toggle source
returns daily historical data in one hash
# File lib/yfinance.rb, line 139 def daily_historical_data(symbols_array, start_date, end_date) result = {} callb = Proc.new do |resp| result.merge!(resp) if result.length == symbols_array.length return result end end add_historical_data_query(symbols_array, start_date, end_date, {period: :daily}, &callb) run end
daily_historical_data_callback(symbols_array, start_date, end_date, &callback)
click to toggle source
for each symbol, the callback is executed
# File lib/yfinance.rb, line 152 def daily_historical_data_callback(symbols_array, start_date, end_date, &callback) add_historical_data_query(symbols_array, start_date, end_date, {period: :daily}, &callback) run end
quotes(symbols_array, columns_array = [:symbol, :last_trade_price, :last_trade_date, :change, :previous_close], options = {})
click to toggle source
# File lib/yfinance.rb, line 170 def quotes(symbols_array, columns_array = [:symbol, :last_trade_price, :last_trade_date, :change, :previous_close], options = {}) options[:raw] ||= true symbols_array = symbols_array.map(&:upcase) ret = Hash[symbols_array.each_slice(1).to_a] symb_str = symbols_array.join("+") columns_array.unshift(:symbol) columns = "#{columns_array.map {|col| COLUMNS[col] }.join('')}" url = "http://download.finance.yahoo.com/d/quotes.csv?s=#{URI.escape(symb_str)}&f=#{columns}" request = Typhoeus::Request.new(url, method: :get) request.on_complete do |response| begin result = CSV.parse(response.body, headers: columns_array) result.each do |row| h = row.to_hash if h[:name] == h[:symbol] ret[h[:symbol]] = nil else ret[h[:symbol]] = h end end return ret rescue return {error: @error_message} end end @hydra.queue(request) @hydra.run end
quotes_all_info(symbols_array)
click to toggle source
# File lib/yfinance.rb, line 199 def quotes_all_info(symbols_array) quotes(symbols_array, SAVE_OPTIONS) end
read_symbols(query, &callback)
click to toggle source
# File lib/yfinance.rb, line 157 def read_symbols(query, &callback) url = "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=#{query}&callback=YAHOO.Finance.SymbolSuggest.ssCallback" request = Typhoeus::Request.new(url, method: :get) request.on_complete do |response| body = response.body body.sub!('YAHOO.Finance.SymbolSuggest.ssCallback(', '').chomp!(')') json_result = JSON.parse(body) callback.call(json_result["ResultSet"]["Result"]) end @hydra.queue(request) @hydra.run end
run()
click to toggle source
# File lib/yfinance.rb, line 121 def run @hydra.run end
Private Instance Methods
make_request(url, symbol, options, callback)
click to toggle source
# File lib/yfinance.rb, line 205 def make_request(url, symbol, options, callback) request = Typhoeus::Request.new(url, method: :get) cols = if options[:period] == :dividends_only [:dividend_pay_date, :dividend_yield] else [:trade_date, :open, :high, :low, :close, :volume, :adjusted_close] end request.on_complete do |response| if response.code == 200 if response.body[0..40] != "Date,Open,High,Low,Close,Volume,Adj Close" raise YahooFinanceException.new(" * Error: Unknown response body from Yahoo - #{ response.body[0..40] } ...") else result = CSV.parse(response.body, headers: cols).to_a result.delete_at(0) result.delete_at(0) result_hash = { symbol => result } callback.call(result_hash) end elsif response.code == 404 result_hash = { symbol => "#{symbol} not found at Yahoo" } callback.call(result_hash) # raise SymbolNotFoundException.new("#{symbol} not found at Yahoo") else raise YahooFinanceException.new("Error communicating with Yahoo. Response code #{ response.code }. URL: " + "#{ url }. Response: #{ response.inspect }") end end request end