Class: YfinanceWrapper::Ticker

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

Constant Summary collapse

DF_METHODS =
%i[actions balance_sheet capital_gains cash_flow cashflow dividends earnings_dates earnings_estimate
earnings_history eps_revisions eps_trend financials growth_estimates income_stmt incomestmt
insider_purchases insider_roster_holders insider_transactions institutional_holders major_holders
mutualfund_holders quarterly_balance_sheet quarterly_balancesheet quarterly_cash_flow quarterly_cashflow
quarterly_income_stmt quarterly_income_stmt quarterly_incomestmt recommendations recommendations_summary
splits sustainability ttm_cash_flow ttm_cashflow ttm_financials ttm_income_stmt ttm_incomestmt upgrades_downgrades]
DICT_METHODS =
%i[analyst_price_targets history_metadata quarterly_financials revenue_estimate]
LIST_METHODS =
%i[news sec_filings]
UNIMPLEMENTED_METHODS =
%i[funds_data quarterly_earnings]

Instance Method Summary collapse

Constructor Details

#initialize(symbol) ⇒ Ticker

Returns a new instance of Ticker.



9
10
11
# File 'lib/yfinance_wrapper/ticker.rb', line 9

def initialize(symbol)
  @ticker = YfinanceWrapper.yf.Ticker.new(symbol)
end

Instance Method Details

#basic_infoObject



36
37
38
39
40
41
# File 'lib/yfinance_wrapper/ticker.rb', line 36

def basic_info
  result = @ticker.basic_info
  result.keys.each_with_object({}) do |key, hash|
    hash[key] = to_ruby_type(result[key])
  end
end

#calendarObject



43
44
45
46
47
48
# File 'lib/yfinance_wrapper/ticker.rb', line 43

def calendar
  result = @ticker.calendar
  result.each_with_object({}) do |pair, hash|
    hash[pair[0]] = to_ruby_type(pair[1])
  end
end

#candle_to_hash(candle, keys) ⇒ Object



173
174
175
176
177
# File 'lib/yfinance_wrapper/ticker.rb', line 173

def candle_to_hash(candle, keys)
  keys.each_with_object({}) do |key, hash|
    hash[key] = candle[key].to_f
  end
end

#df_to_ruby_type(df) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/yfinance_wrapper/ticker.rb', line 138

def df_to_ruby_type(df)
  indexes = df.index

  indexes.size.times.each_with_object({}) do |i, hash|
    index = indexes[i]
    ruby_index = to_ruby_type(indexes[i])
    value = df.loc[index]
    ruby_value = if value.respond_to?(:index) && value.respond_to?(:keys)
                   df_to_ruby_type(value)
                 else
                   to_ruby_type(value)
                 end

    hash[ruby_index] = ruby_value
  end
end

#dict_to_ruby_type(dict) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/yfinance_wrapper/ticker.rb', line 155

def dict_to_ruby_type(dict)
  if dict.respond_to?(:to_h)
    hash = dict.to_h
    hash.each_with_object({}) do |(key, value), hash|
      hash[key] = to_ruby_type(value)
    end
  elsif dict.respond_to?(:each_with_object)
    dict.each_with_object({}) do |(key, value), hash|
      hash[key] = to_ruby_type(value)
    end
    els
    dict.keys.each_with_object({}) do |key, hash|
      value = dict[key]
      hash[key] = to_ruby_type(value)
    end
  end
end

#earningsObject



50
# File 'lib/yfinance_wrapper/ticker.rb', line 50

def earnings = nil

#fast_infoObject



60
61
62
63
64
65
# File 'lib/yfinance_wrapper/ticker.rb', line 60

def fast_info
  result = @ticker.fast_info
  result.keys.each_with_object({}) do |key, hash|
    hash[key] = to_ruby_type(result[key])
  end
end

#history(*args, **kwargs) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/yfinance_wrapper/ticker.rb', line 17

def history(*args, **kwargs)
  result = @ticker.history(*args, **kwargs)
  indexes = result.index

  res = {}

  indexes.size.times do |i|
    key = indexes[i]
    res[Time.new(key.to_s)] = candle_to_hash(result.loc[key], %w[Open High Low Close])
  end

  res
end

#infoObject



13
14
15
# File 'lib/yfinance_wrapper/ticker.rb', line 13

def info
  @ticker.info.to_h
end

#isinObject



52
53
54
# File 'lib/yfinance_wrapper/ticker.rb', line 52

def isin
  @ticker.isin
end

#list_to_ruby_type(list) ⇒ Object

private



106
107
108
# File 'lib/yfinance_wrapper/ticker.rb', line 106

def list_to_ruby_type(list)
  list.map { |v| dict_to_ruby_type(v) }
end

#newsObject



31
32
33
34
# File 'lib/yfinance_wrapper/ticker.rb', line 31

def news
  result = @ticker.news
  result.map { |v| v.to_h }
end

#optionsObject



56
57
58
# File 'lib/yfinance_wrapper/ticker.rb', line 56

def options
  @ticker.options.to_a
end

#to_ruby_type(value) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/yfinance_wrapper/ticker.rb', line 110

def to_ruby_type(value)
  return dict_to_ruby_type(value) if value.class.to_s == "<class 'dict'>"

  return df_to_ruby_type(value) if value.respond_to?(:index) && value.respond_to?(:keys)

  case value.to_s
  when /\A-?\d+\.\d+\Z/
    value.to_f
  when /\A-?\d+\Z/
    value.to_i
  when /\A\[.+?\]\Z/
    if value.respond_to?(:map)
      value.map { |v| to_ruby_type(v) }
    else
      value.size.times.map { |i| to_ruby_type(value[i]) }
    end
  when /\A\d{4}-\d{2}-\d{2}\Z/
    date = value.to_s.scan(/\d+/).map(&:to_i)
    Date.new(date[0], date[1], date[2])
  when /\A\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\Z/
    Time.strptime(value.to_s, "%Y-%m-%d %H:%M:%S")
  when /\Anan\Z/, /\ANaN\Z/
    Float::NAN
  else
    value.to_s
  end
end