yahoo_finance_client 0.3.1 → 0.4.1
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 +4 -4
- data/.rubocop.yml +1 -1
- data/CHANGELOG.md +26 -0
- data/README.md +36 -1
- data/lib/yahoo_finance_client/stock.rb +62 -0
- data/lib/yahoo_finance_client/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ec1a70f9b1a9927d6b36ea559dbaf4204f8c82ad37d70b2c2feab47864cd01ac
|
|
4
|
+
data.tar.gz: 3a91d7848dffe1d5ca1532e31c513f4465ad9147f93c8a6984dfabf320c65337
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b489b0a5689876b9f13e8876263ce54c2878a7aea9e4ad421114d9ee4434c50764c89d9cb6a19a0605bc0e4bc777f14ff641edbef314e57bdf4ddc365c9428ac
|
|
7
|
+
data.tar.gz: daffd51bf44af895b9741b94278ef5d81729f7bdc0b3250b91b0897009cfe30b069beedcd14c3d02a7f22bc40e01907eb8fab80655a5ea02c14e90e002d1a87c
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## [0.4.1] - 2026-02-13
|
|
4
|
+
|
|
5
|
+
- Add `fifty_two_week_high` and `fifty_two_week_low` fields to quote data
|
|
6
|
+
|
|
7
|
+
## [0.4.0] - 2026-02-12
|
|
8
|
+
|
|
9
|
+
- Add dividend history fetching via chart API (`get_dividend_history`)
|
|
10
|
+
- Update README with bulk quotes, dividend history, and caching docs
|
|
11
|
+
|
|
12
|
+
## [0.3.1] - 2026-02-12
|
|
13
|
+
|
|
14
|
+
- Add `ex_dividend_date` and `dividend_date` fields to quote data
|
|
15
|
+
|
|
16
|
+
## [0.3.0] - 2026-02-10
|
|
17
|
+
|
|
18
|
+
- Add bulk quotes support with `get_quotes` method
|
|
19
|
+
|
|
20
|
+
## [0.2.1] - 2026-01-29
|
|
21
|
+
|
|
22
|
+
- Add extended stock information fields (EPS, PE ratio, dividend data, moving averages)
|
|
23
|
+
|
|
24
|
+
## [0.2.0] - 2026-01-27
|
|
25
|
+
|
|
26
|
+
- Add multi-strategy authentication with retry logic
|
|
27
|
+
- Add CLAUDE.md with project instructions
|
|
28
|
+
|
|
3
29
|
## [0.1.6] - 2025-02-18
|
|
4
30
|
|
|
5
31
|
- Adding a simple cache
|
data/README.md
CHANGED
|
@@ -32,11 +32,46 @@ gem install yahoo_finance_client
|
|
|
32
32
|
|
|
33
33
|
## Usage
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
### Single Quote
|
|
36
|
+
|
|
37
|
+
Fetch stock data by passing a ticker symbol:
|
|
36
38
|
```ruby
|
|
37
39
|
YahooFinanceClient::Stock.get_quote("AAPL")
|
|
40
|
+
# => {
|
|
41
|
+
# symbol: "AAPL", name: "Apple Inc.", price: 182.52,
|
|
42
|
+
# change: 1.25, percent_change: 0.69, volume: 48123456,
|
|
43
|
+
# pe_ratio: 28.5, eps: 6.40,
|
|
44
|
+
# dividend: 0.96, dividend_yield: 0.53, payout_ratio: 15.0,
|
|
45
|
+
# ma50: 178.30, ma200: 172.15,
|
|
46
|
+
# ex_dividend_date: #<Date: 2025-02-07>, dividend_date: #<Date: 2025-02-15>
|
|
47
|
+
# }
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Bulk Quotes
|
|
51
|
+
|
|
52
|
+
Fetch multiple quotes at once (batched in groups of 50):
|
|
53
|
+
```ruby
|
|
54
|
+
YahooFinanceClient::Stock.get_quotes(["AAPL", "MSFT", "GOOG"])
|
|
55
|
+
# => { "AAPL" => { symbol: "AAPL", ... }, "MSFT" => { ... }, "GOOG" => { ... } }
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Dividend History
|
|
59
|
+
|
|
60
|
+
Fetch historical dividend payments via the chart API:
|
|
61
|
+
```ruby
|
|
62
|
+
YahooFinanceClient::Stock.get_dividend_history("AAPL")
|
|
63
|
+
# => [{ date: #<Date: 2024-02-09>, amount: 0.24 }, ...]
|
|
38
64
|
```
|
|
39
65
|
|
|
66
|
+
The default range is `"2y"`. You can pass a different range:
|
|
67
|
+
```ruby
|
|
68
|
+
YahooFinanceClient::Stock.get_dividend_history("AAPL", range: "5y")
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Caching
|
|
72
|
+
|
|
73
|
+
All responses are cached for 5 minutes (300 seconds). The cache is shared across `get_quote`, `get_quotes`, and `get_dividend_history`.
|
|
74
|
+
|
|
40
75
|
## Development
|
|
41
76
|
|
|
42
77
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
@@ -7,6 +7,7 @@ module YahooFinanceClient
|
|
|
7
7
|
# This class provides methods to interact with Yahoo Finance API for stock data.
|
|
8
8
|
class Stock
|
|
9
9
|
QUOTE_PATH = "/v7/finance/quote"
|
|
10
|
+
CHART_PATH = "/v8/finance/chart"
|
|
10
11
|
CACHE_TTL = 300
|
|
11
12
|
MAX_RETRIES = 2
|
|
12
13
|
BATCH_SIZE = 50
|
|
@@ -28,6 +29,11 @@ module YahooFinanceClient
|
|
|
28
29
|
results
|
|
29
30
|
end
|
|
30
31
|
|
|
32
|
+
def get_dividend_history(symbol, range: "2y")
|
|
33
|
+
cache_key = "div_history_#{symbol}_#{range}"
|
|
34
|
+
fetch_from_cache(cache_key) || fetch_and_cache_dividend_history(cache_key, symbol, range)
|
|
35
|
+
end
|
|
36
|
+
|
|
31
37
|
private
|
|
32
38
|
|
|
33
39
|
def partition_cached(symbols)
|
|
@@ -146,6 +152,7 @@ module YahooFinanceClient
|
|
|
146
152
|
dividend: dividend, dividend_yield: calculate_yield(dividend, price),
|
|
147
153
|
payout_ratio: calculate_payout(dividend, eps),
|
|
148
154
|
ma50: quote["fiftyDayAverage"], ma200: quote["twoHundredDayAverage"],
|
|
155
|
+
fifty_two_week_high: quote["fiftyTwoWeekHigh"], fifty_two_week_low: quote["fiftyTwoWeekLow"],
|
|
149
156
|
ex_dividend_date: parse_unix_date(quote["exDividendDate"]),
|
|
150
157
|
dividend_date: parse_unix_date(quote["dividendDate"])
|
|
151
158
|
}
|
|
@@ -169,6 +176,61 @@ module YahooFinanceClient
|
|
|
169
176
|
Time.at(value).utc.to_date
|
|
170
177
|
end
|
|
171
178
|
|
|
179
|
+
def fetch_and_cache_dividend_history(cache_key, symbol, range)
|
|
180
|
+
data = fetch_dividend_history_data(symbol, range)
|
|
181
|
+
store_in_cache(cache_key, data) unless data.empty?
|
|
182
|
+
data
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def fetch_dividend_history_data(symbol, range)
|
|
186
|
+
retries = 0
|
|
187
|
+
begin
|
|
188
|
+
response = make_chart_request(symbol, range)
|
|
189
|
+
parse_dividend_history(response)
|
|
190
|
+
rescue AuthenticationError
|
|
191
|
+
retries += 1
|
|
192
|
+
retry if retries <= MAX_RETRIES
|
|
193
|
+
[]
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
def make_chart_request(symbol, range)
|
|
198
|
+
session = Session.instance
|
|
199
|
+
session.ensure_authenticated
|
|
200
|
+
url = "#{session.base_url}#{CHART_PATH}/#{symbol}?range=#{range}&interval=1mo&events=div&crumb=#{session.crumb}"
|
|
201
|
+
HTTParty.get(url, headers: { "User-Agent" => Session::USER_AGENT, "Cookie" => session.cookie })
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def parse_dividend_history(response)
|
|
205
|
+
raise_if_auth_error(response)
|
|
206
|
+
return [] unless response.success?
|
|
207
|
+
|
|
208
|
+
dividends = JSON.parse(response.body).dig("chart", "result", 0, "events", "dividends")
|
|
209
|
+
return [] unless dividends
|
|
210
|
+
|
|
211
|
+
build_dividend_entries(dividends)
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def raise_if_auth_error(response)
|
|
215
|
+
return unless auth_error?(response)
|
|
216
|
+
|
|
217
|
+
Session.instance.invalidate!
|
|
218
|
+
raise AuthenticationError, "Authentication failed"
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def build_dividend_entries(dividends)
|
|
222
|
+
entries = dividends.values.filter_map { |entry| parse_dividend_entry(entry) }
|
|
223
|
+
entries.sort_by { |d| d[:date] }
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
def parse_dividend_entry(entry)
|
|
227
|
+
date = parse_unix_date(entry["date"])
|
|
228
|
+
amount = entry["amount"]
|
|
229
|
+
return unless date && amount&.positive?
|
|
230
|
+
|
|
231
|
+
{ date: date, amount: amount.round(4) }
|
|
232
|
+
end
|
|
233
|
+
|
|
172
234
|
def fetch_from_cache(key)
|
|
173
235
|
cached_entry = @cache[key]
|
|
174
236
|
return unless cached_entry && Time.now - cached_entry[:timestamp] < CACHE_TTL
|