yfinance_wrapper 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1d85bd7392805b47e38c34a148124b7ecd68822b597001c9051aeacd30791516
4
- data.tar.gz: 12e22cf4668680cd6a6a3d10934575a77987459483cbd7aecc5a136a7b963aac
3
+ metadata.gz: 8325c8f773f83d22f9b8e6523922041dcd37181b953e8ce08f4d36c23366cc68
4
+ data.tar.gz: 946cd16e59051dd76ae22ab922f697485f528961f07b5e763497ce48988a470f
5
5
  SHA512:
6
- metadata.gz: be045351689c7bd513a98ec75817812946f447bdb1956ec8a88703c33c12219a86870739b803cb9f10593dd54f3a6b443e5f1f4005d717c3e1f1905644109b3f
7
- data.tar.gz: 6595f89d4e915571c134734b32206b7fcbb6aab8bcdd8f946762743647692a22ddc2973c0ee9e489f41edb645132082523fcb296291291fb42eb4feae3d278e7
6
+ metadata.gz: ea438134ac530c37700708590522387428400b99845b5ea8823f1932aaecac2de65d1a62e1811ae4597149a1e12add54e1852ad2238b4652c689c40e4889d4e4
7
+ data.tar.gz: 2ea2265b144ab91c8c10ee55fb25ed44b62889ed8bd26344dbc65eef29c60a8012262084a33b96782faae3058ebdac89d40831c72eb654969a1731205a43a8e0
data/README.md CHANGED
@@ -24,3 +24,20 @@ This library supports the development of financial data applications and analysi
24
24
  ```bash
25
25
  pip3 install yfinance
26
26
  ```
27
+
28
+ # Example
29
+
30
+ ```ruby
31
+ ticker = YfinanceWrapper::Ticker.new('APPL')
32
+ ticker.info
33
+ # {"quoteType"=>"MUTUALFUND",
34
+ # "symbol"=>"APPL",
35
+ # "language"=>"en-US"
36
+ # ...
37
+ # }
38
+
39
+ ticker.history(period: "max", interval: "1d")
40
+ # {1980-12-12 00:00:00 -0500=>
41
+ # ...
42
+ # }
43
+ ```
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "pycall/import"
4
+ require "date"
5
+ require "time"
4
6
 
5
7
  module YfinanceWrapper
6
8
  class Ticker
@@ -26,9 +28,148 @@ module YfinanceWrapper
26
28
  res
27
29
  end
28
30
 
29
- private
31
+ def news
32
+ result = @ticker.news
33
+ result.map { |v| v.to_h }
34
+ end
35
+
36
+ def basic_info
37
+ result = @ticker.basic_info
38
+ result.keys.each_with_object({}) do |key, hash|
39
+ hash[key] = to_ruby_type(result[key])
40
+ end
41
+ end
42
+
43
+ def calendar
44
+ result = @ticker.calendar
45
+ result.each_with_object({}) do |pair, hash|
46
+ hash[pair[0]] = to_ruby_type(pair[1])
47
+ end
48
+ end
49
+
50
+ def earnings = nil
51
+
52
+ def isin
53
+ @ticker.isin
54
+ end
55
+
56
+ def options
57
+ @ticker.options.to_a
58
+ end
59
+
60
+ def fast_info
61
+ result = @ticker.fast_info
62
+ result.keys.each_with_object({}) do |key, hash|
63
+ hash[key] = to_ruby_type(result[key])
64
+ end
65
+ end
66
+
67
+ DF_METHODS = %i[actions balance_sheet capital_gains cash_flow cashflow dividends earnings_dates earnings_estimate
68
+ earnings_history eps_revisions eps_trend financials growth_estimates income_stmt incomestmt
69
+ insider_purchases insider_roster_holders insider_transactions institutional_holders major_holders
70
+ mutualfund_holders quarterly_balance_sheet quarterly_balancesheet quarterly_cash_flow quarterly_cashflow
71
+ quarterly_income_stmt quarterly_income_stmt quarterly_incomestmt recommendations recommendations_summary
72
+ splits sustainability ttm_cash_flow ttm_cashflow ttm_financials ttm_income_stmt ttm_incomestmt upgrades_downgrades]
73
+ DICT_METHODS = %i[analyst_price_targets history_metadata quarterly_financials revenue_estimate]
74
+ LIST_METHODS = %i[news sec_filings]
75
+
76
+ UNIMPLEMENTED_METHODS = %i[funds_data quarterly_earnings]
77
+
78
+ DF_METHODS.each do |method|
79
+ define_method(method) do |*args, **kwargs|
80
+ result = @ticker.send(method, *args, **kwargs)
81
+ to_ruby_type(result)
82
+ end
83
+ end
84
+
85
+ DICT_METHODS.each do |method|
86
+ define_method(method) do |*args, **kwargs|
87
+ result = @ticker.send(method, *args, **kwargs)
88
+ to_ruby_type(result)
89
+ end
90
+ end
91
+
92
+ UNIMPLEMENTED_METHODS.each do |method|
93
+ define_method(method) do
94
+ raise NotImplementedError, "Method #{method} is not implemented"
95
+ end
96
+ end
97
+
98
+ LIST_METHODS.each do |method|
99
+ define_method(method) do |*args, **kwargs|
100
+ result = @ticker.send(method, *args, **kwargs)
101
+ list_to_ruby_type(result)
102
+ end
103
+ end
104
+
105
+ # private
106
+ def list_to_ruby_type(list)
107
+ list.map { |v| dict_to_ruby_type(v) }
108
+ end
109
+
110
+ def to_ruby_type(value)
111
+ return dict_to_ruby_type(value) if value.class.to_s == "<class 'dict'>"
112
+
113
+ return df_to_ruby_type(value) if value.respond_to?(:index) && value.respond_to?(:keys)
114
+
115
+ case value.to_s
116
+ when /\A-?\d+\.\d+\Z/
117
+ value.to_f
118
+ when /\A-?\d+\Z/
119
+ value.to_i
120
+ when /\A\[.+?\]\Z/
121
+ if value.respond_to?(:map)
122
+ value.map { |v| to_ruby_type(v) }
123
+ else
124
+ value.size.times.map { |i| to_ruby_type(value[i]) }
125
+ end
126
+ when /\A\d{4}-\d{2}-\d{2}\Z/
127
+ date = value.to_s.scan(/\d+/).map(&:to_i)
128
+ Date.new(date[0], date[1], date[2])
129
+ when /\A\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\Z/
130
+ Time.strptime(value.to_s, "%Y-%m-%d %H:%M:%S")
131
+ when /\Anan\Z/, /\ANaN\Z/
132
+ Float::NAN
133
+ else
134
+ value.to_s
135
+ end
136
+ end
137
+
138
+ def df_to_ruby_type(df)
139
+ indexes = df.index
140
+
141
+ indexes.size.times.each_with_object({}) do |i, hash|
142
+ index = indexes[i]
143
+ ruby_index = to_ruby_type(indexes[i])
144
+ value = df.loc[index]
145
+ ruby_value = if value.respond_to?(:index) && value.respond_to?(:keys)
146
+ df_to_ruby_type(value)
147
+ else
148
+ to_ruby_type(value)
149
+ end
150
+
151
+ hash[ruby_index] = ruby_value
152
+ end
153
+ end
154
+
155
+ def dict_to_ruby_type(dict)
156
+ if dict.respond_to?(:to_h)
157
+ hash = dict.to_h
158
+ hash.each_with_object({}) do |(key, value), hash|
159
+ hash[key] = to_ruby_type(value)
160
+ end
161
+ elsif dict.respond_to?(:each_with_object)
162
+ dict.each_with_object({}) do |(key, value), hash|
163
+ hash[key] = to_ruby_type(value)
164
+ end
165
+ els
166
+ dict.keys.each_with_object({}) do |key, hash|
167
+ value = dict[key]
168
+ hash[key] = to_ruby_type(value)
169
+ end
170
+ end
171
+ end
30
172
 
31
- # Add Candle class when candle needs more mothods.
32
173
  def candle_to_hash(candle, keys)
33
174
  keys.each_with_object({}) do |key, hash|
34
175
  hash[key] = candle[key].to_f
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module YfinanceWrapper
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yfinance_wrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ys7i
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-04-13 00:00:00.000000000 Z
11
+ date: 2025-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pycall