Class: Yfinance

Inherits:
Object
  • Object
show all
Defined in:
lib/yfinance.rb

Defined Under Namespace

Classes: SymbolNotFoundException, YahooFinanceException

Constant Summary collapse

COLUMNS =
{
   :ask => "a",
   :average_daily_volume => "a2",
   # :ask_size => "a5", # sometimes includes comma which isn't parsed correctly
   :bid => "b",
   :ask_real_time => "b2",
   :bid_real_time => "b3",
   :book_value => "b4",
   # :bid_size => "b6", # sometimes includes comma which isn't parsed correctly
   :change_and_percent_change => "c",
   :change => "c1",
   :comission => "c3",
   :change_real_time => "c6",
   :after_hours_change_real_time => "c8",
   :dividend_per_share => "d",
   :last_trade_date => "d1",
   :trade_date => "d2",
   :earnings_per_share => "e",
   :error_indicator => "e1",
   :eps_estimate_current_year => "e7",
   :eps_estimate_next_year => "e8",
   :eps_estimate_next_quarter => "e9", 
   #:float_shares => "f6",  # sometimes includes comma which isn't parsed correctly
   :low => "g",
   :high => "h",
   :low_52_weeks => "j",
   :high_52_weeks => "k",
   :holdings_gain_percent => "g1",
   :annualized_gain => "g3",
   :holdings_gain => "g4",
   :holdings_gain_percent_realtime => "g5",
   :holdings_gain_realtime => "g6",
   :more_info => "i",
   :order_book => "i5", 
   :market_capitalization => "j1",
   :market_cap_realtime => "j3", 
   :ebitda => "j4",
   :change_From_52_week_low => "j5",
   :percent_change_from_52_week_low => "j6",
   :last_trade_realtime_withtime => "k1",
   :change_percent_realtime => "k2",
   :last_trade_size => "k3",
   :change_from_52_week_high => "k4",
   :percent_change_from_52_week_high => "k5",
   :last_trade_with_time => "l",
   :last_trade_price => "l1",
   :close => "l1",
   :high_limit => "l2",
   :low_limit => "l3",
   :days_range => "m",
   :days_range_realtime => "m2",
   :moving_average_50_day => "m3",
   :moving_average_200_day => "m4",
   :change_from_200_day_moving_average => "m5",
   :percent_change_from_200_day_moving_average => "m6",
   :change_from_50_day_moving_average => "m7",
   :percent_change_from_50_day_moving_average => "m8",
   :name => "n",
   :notes => "n4",
   :open => "o",
   :previous_close => "p",
   :price_paid => "p1",
   :change_in_percent => "p2",
   :price_per_sales => "p5",
   :price_per_book => "p6",
   :ex_dividend_date => "q",
   :pe_ratio => "r",
   :dividend_pay_date => "r1",
   :pe_ratio_realtime => "r2",
   :peg_ratio => "r5",
   :price_eps_estimate_current_year => "r6",
   :price_eps_Estimate_next_year => "r7",
   :symbol => "s",
   :shares_owned => "s1",
   :short_ratio => "s7",
   :last_trade_time => "t1",
   #:trade_links => "t6", # doesn't work anymore - asks for brokerage and is not parse correctly
   :ticker_trend => "t7",
   :one_year_target_price => "t8",
   :volume => "v",
   :holdings_value => "v1",
   :holdings_value_realtime => "v7",
   :weeks_range_52 => "w",
   :day_value_change => "w1",
   :day_value_change_realtime => "w4",
   :stock_exchange => "x",
   :dividend_yield => "y"
}
HISTORICAL_MODES =
{
  :daily => "d",
  :weekly => "w",
  :monthly => "m",
  :dividends_only => "v"
}

Instance Method Summary collapse

Constructor Details

#initialize(max_concurrency: 20, memoize: false) ⇒ Yfinance

Returns a new instance of Yfinance.



110
111
112
113
# File 'lib/yfinance.rb', line 110

def initialize(max_concurrency: 20, memoize: false)
  @hydra = Typhoeus::Hydra.new(max_concurrency: max_concurrency)
  Typhoeus::Config.memoize = memoize
end

Instance Method Details

#add_historical_data_query(symbols_array, start_date, end_date, options = {}, &callback) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/yfinance.rb', line 119

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
     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

#quotes(symbols_array, columns_array = [:symbol, :last_trade_price, :last_trade_date, :change, :previous_close], options = {}) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/yfinance.rb', line 145

def quotes(symbols_array, columns_array = [:symbol, :last_trade_price, :last_trade_date, :change, :previous_close], options = {})
  options[:raw] ||= true
  ret = []
  symb_str = symbols_array.join("+")
  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|
        ret << row.to_hash#OpenStruct.new(row.to_hash) 
      end
    rescue
      ret = {name: "unknown symbol"}#OpenStruct.new(name: "unknown symbol")
    end
    return ret
  end
  @hydra.queue(request)
  @hydra.run
end

#quotes_all_info(symbols_array) ⇒ Object



167
168
169
170
# File 'lib/yfinance.rb', line 167

def quotes_all_info(symbols_array)
  columns_array = COLUMNS.keys
  quotes(symbols_array, columns_array)
end

#read_symbols(query, &callback) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/yfinance.rb', line 132

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

#runObject



115
116
117
# File 'lib/yfinance.rb', line 115

def run
  @hydra.run
end