yahoo-finance 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e4d05824e6ec9455f7431eb7450405a4f187502c
4
- data.tar.gz: 53b32872affff1bd7a1ba6153b73f62aac280852
3
+ metadata.gz: 9c9c50a4db7239780947f98ccac1239b206f6d3f
4
+ data.tar.gz: dcaa2a3413b9047165882ac7b485a7d6ba10b6f4
5
5
  SHA512:
6
- metadata.gz: 69bbfa32a532c46ee3360b58a48492b74638e4816513359d7b7c212c6909fc317d92e9100cd63d1302aaa52ac94dc759ee740215fc4cff1af349afbdaf215c7b
7
- data.tar.gz: 5dfa9bbe070a5bcce32df9ffedb1257d9e2880c8dcbc052bef866a47a4d557aae49f1d97a3fefa36c042873c69d2bdbce287c04509514c86ee68f3c0dd759918
6
+ metadata.gz: 975822b34c359f5dd724dc1672d336d49017256316ee215364bfd5d88909a47f7c272f5b8ab37a356543ab886ee723fc1ea9bf2dd57024f5cc615df8725adbfb
7
+ data.tar.gz: 345fe8dd44e08e1acfd7c57f1bfd26a1ddb913e4cfaa4a6b120a47c3b48d01bd804a8061faed913819ad49d820e6cee659cabeb7cc068ecfab24f55422da4509
data/HISTORY CHANGED
@@ -1,3 +1,9 @@
1
+ 1.2.0 - Feb 04, 2017
2
+ ====================
3
+ * Fixed support for { raw: false }
4
+ * Symbols that are not found will now not return (instead of returning a struct with an empty :symbol field and a bunch of "N/A"s)
5
+ * The returned quotes now will always include the :symbol name
6
+
1
7
  0.3.0 - Aug 16, 2015
2
8
  ====================
3
9
  * Refactorings & support for US companies data (via NASDAQ's Apis) - by Eric D. Santos Sosa
@@ -20,4 +26,4 @@
20
26
 
21
27
  0.0.1 - Aug 04, 2007
22
28
  ===============================
23
- * Initial implementation - no bells, no whristles :)
29
+ * Initial implementation - no bells, no whistles :)
data/README.md CHANGED
@@ -30,7 +30,21 @@ Passing `raw: false` will return numerical values
30
30
  ```ruby
31
31
  yahoo_client = YahooFinance::Client.new
32
32
  data = yahoo_client.quotes(["BVSP", "NATU3.SA", "USDJPY=X"], [:ask, :bid, :last_trade_date], { raw: false } )
33
- data[0].ask # This is now a float
33
+ data[0].ask # This is now a BigDecimal
34
+ ```
35
+
36
+ Passing `na_as_nil: true` will convert "N/A" responses to `nil`
37
+
38
+ ```ruby
39
+ yahoo_client = YahooFinance::Client.new
40
+
41
+ data = yahoo_client.quotes(["BVSP"], [:ask] )
42
+ data[0].ask
43
+ > "N/A"
44
+
45
+ data = yahoo_client.quotes(["BVSP"], [:ask], { na_as_nil: true } )
46
+ data[0].ask
47
+ > nil
34
48
  ```
35
49
 
36
50
  The full list of fields follows:
data/lib/yahoo-finance.rb CHANGED
@@ -4,6 +4,7 @@ require "json"
4
4
  require "yahoo-finance/version"
5
5
  require "yahoo-finance/finance-utils"
6
6
  require "csv"
7
+ require 'bigdecimal'
7
8
 
8
9
  module YahooFinance
9
10
  # Client for Yahoo Finance Queries
@@ -11,92 +12,93 @@ module YahooFinance
11
12
  include YahooFinance::FinanceUtils
12
13
 
13
14
  COLUMNS = {
14
- ask: "a",
15
- average_daily_volume: "a2",
16
- ask_size: "a5",
17
- bid: "b",
18
- ask_real_time: "b2",
19
- bid_real_time: "b3",
20
- book_value: "b4",
21
- bid_size: "b6",
22
- change_and_percent_change: "c",
23
- change: "c1",
24
- comission: "c3",
25
- change_real_time: "c6",
26
- after_hours_change_real_time: "c8",
27
- dividend_per_share: "d",
28
- last_trade_date: "d1",
29
- trade_date: "d2",
30
- earnings_per_share: "e",
31
- error_indicator: "e1",
32
- eps_estimate_current_year: "e7",
33
- eps_estimate_next_year: "e8",
34
- eps_estimate_next_quarter: "e9",
35
- float_shares: "f6",
36
- low: "g", high: "h",
37
- low_52_weeks: "j",
38
- high_52_weeks: "k",
39
- holdings_gain_percent: "g1",
40
- annualized_gain: "g3",
41
- holdings_gain: "g4",
42
- holdings_gain_percent_realtime: "g5",
43
- holdings_gain_realtime: "g6",
44
- more_info: "i",
45
- order_book: "i5",
46
- market_capitalization: "j1",
47
- shares_outstanding: "j2",
48
- market_cap_realtime: "j3",
49
- ebitda: "j4",
50
- change_From_52_week_low: "j5",
51
- percent_change_from_52_week_low: "j6",
52
- last_trade_realtime_withtime: "k1",
53
- change_percent_realtime: "k2",
54
- last_trade_size: "k3",
55
- change_from_52_week_high: "k4",
56
- percent_change_from_52_week_high: "k5",
57
- last_trade_with_time: "l",
58
- last_trade_price: "l1",
59
- close: "l1",
60
- high_limit: "l2",
61
- low_limit: "l3",
62
- days_range: "m",
63
- days_range_realtime: "m2",
64
- moving_average_50_day: "m3",
65
- moving_average_200_day: "m4",
66
- change_from_200_day_moving_average: "m5",
67
- percent_change_from_200_day_moving_average: "m6",
68
- change_from_50_day_moving_average: "m7",
69
- percent_change_from_50_day_moving_average: "m8",
70
- name: "n",
71
- notes: "n4",
72
- open: "o", previous_close: "p",
73
- price_paid: "p1",
74
- change_in_percent: "p2",
75
- price_per_sales: "p5",
76
- price_per_book: "p6",
77
- ex_dividend_date: "q",
78
- pe_ratio: "r",
79
- dividend_pay_date: "r1",
80
- pe_ratio_realtime: "r2",
81
- peg_ratio: "r5",
82
- price_eps_estimate_current_year: "r6",
83
- price_eps_Estimate_next_year: "r7",
84
- symbol: "s",
85
- shares_owned: "s1",
86
- revenue: "s6",
87
- short_ratio: "s7", last_trade_time: "t1",
88
- trade_links: "t6",
89
- ticker_trend: "t7",
90
- one_year_target_price: "t8", volume: "v",
91
- holdings_value: "v1",
92
- holdings_value_realtime: "v7",
93
- weeks_range_52: "w",
94
- day_value_change: "w1",
95
- day_value_change_realtime: "w4",
96
- stock_exchange: "x",
97
- dividend_yield: "y",
98
- adjusted_close: nil
99
- # only in historical quotes ^
15
+ :ask => ["a", BigDecimal],
16
+ :average_daily_volume => ["a2", BigDecimal],
17
+ :ask_size => ["a5", BigDecimal],
18
+ :bid => ["b", BigDecimal],
19
+ :ask_real_time => ["b2", Time],
20
+ :bid_real_time => ["b3", BigDecimal],
21
+ :book_value => ["b4", BigDecimal],
22
+ :bid_size => ["b6", BigDecimal],
23
+ :chance_and_percent_change => ["c", String],
24
+ :change => ["c1", BigDecimal],
25
+ :comission => ["c3", String],
26
+ :change_real_time => ["c6", BigDecimal],
27
+ :after_hours_change_real_time => ["c8", String],
28
+ :dividend_per_share => ["d", BigDecimal],
29
+ :last_trade_date => ["d1", DateTime],
30
+ :trade_date => ["d2", String],
31
+ :earnings_per_share => ["e", BigDecimal],
32
+ :error_indicator => ["e1", String],
33
+ :eps_estimate_current_year => ["e7", BigDecimal],
34
+ :eps_estimate_next_year => ["e8", BigDecimal],
35
+ :eps_estimate_next_quarter => ["e9", BigDecimal],
36
+ :float_shares => ["f6", BigDecimal],
37
+ :low => ["g", BigDecimal],
38
+ :high => ["h", BigDecimal],
39
+ :low_52_weeks => ["j", BigDecimal],
40
+ :high_52_weeks => ["k", BigDecimal],
41
+ :holdings_gain_percent => ["g1", BigDecimal],
42
+ :annualized_gain => ["g3", BigDecimal],
43
+ :holdings_gain => ["g4", BigDecimal],
44
+ :holdings_gain_percent_realtime => ["g5", BigDecimal],
45
+ :holdings_gain_realtime => ["g6", BigDecimal],
46
+ :more_info => ["i", String],
47
+ :order_book => ["i5", BigDecimal],
48
+ :market_capitalization => ["j1", BigDecimal],
49
+ :market_cap_realtime => ["j3", BigDecimal],
50
+ :ebitda => ["j4", BigDecimal],
51
+ :change_From_52_week_low => ["j5", BigDecimal],
52
+ :percent_change_from_52_week_low => ["j6", BigDecimal],
53
+ :last_trade_realtime_withtime => ["k1", String],
54
+ :change_percent_realtime => ["k2", String],
55
+ :last_trade_size => ["k3", BigDecimal],
56
+ :change_from_52_week_high => ["k4", BigDecimal],
57
+ :percent_change_from_52_week_high => ["k5", BigDecimal],
58
+ :last_trade_with_time => ["l", String],
59
+ :last_trade_price => ["l1", BigDecimal],
60
+ :close => ["l1", BigDecimal], # same as :last_trade_price
61
+ :high_limit => ["l2", BigDecimal],
62
+ :low_limit => ["l3", BigDecimal],
63
+ :days_range => ["m", BigDecimal],
64
+ :days_range_realtime => ["m2", BigDecimal],
65
+ :moving_average_50_day => ["m3", BigDecimal],
66
+ :moving_average_200_day => ["m4", BigDecimal],
67
+ :change_from_200_day_moving_average => ["m5", BigDecimal],
68
+ :percent_change_from_200_day_moving_average => ["m6", BigDecimal],
69
+ :change_from_50_day_moving_average => ["m7", BigDecimal],
70
+ :percent_change_from_50_day_moving_average => ["m8", BigDecimal],
71
+ :name => ["n", String],
72
+ :notes => ["n4", String],
73
+ :open => ["o", BigDecimal],
74
+ :previous_close => ["p", BigDecimal],
75
+ :price_paid => ["p1", BigDecimal],
76
+ :change_in_percent => ["p2", BigDecimal],
77
+ :price_per_sales => ["p5", BigDecimal],
78
+ :price_per_book => ["p6", BigDecimal],
79
+ :ex_dividend_date => ["q", DateTime],
80
+ :pe_ratio => ["p5", BigDecimal],
81
+ :dividend_pay_date => ["r1", String],
82
+ :pe_ratio_realtime => ["r2", BigDecimal],
83
+ :peg_ratio => ["r5", BigDecimal],
84
+ :price_eps_estimate_current_year => ["r6", BigDecimal],
85
+ :price_eps_Estimate_next_year => ["r7", BigDecimal],
86
+ :symbol => ["s", String],
87
+ :shares_owned => ["s1", BigDecimal],
88
+ :short_ratio => ["s7", BigDecimal],
89
+ :last_trade_time => ["t1", Time],
90
+ :trade_links => ["t6", String],
91
+ :ticker_trend => ["t7", String],
92
+ :one_year_target_price => ["t8", BigDecimal],
93
+ :volume => ["v", BigDecimal],
94
+ :holdings_value => ["v1", String],
95
+ :holdings_value_realtime => ["v7", String],
96
+ :weeks_range_52 => ["w", BigDecimal],
97
+ :day_value_change => ["w1", BigDecimal],
98
+ :day_value_change_realtime => ["w4", String],
99
+ :stock_exchange => ["x", String],
100
+ :dividend_yield => ["y", BigDecimal],
101
+ :adjusted_close => [nil, BigDecimal] # this one only comes in historical quotes
100
102
  }
101
103
 
102
104
  HISTORICAL_MODES = {
@@ -111,27 +113,31 @@ module YahooFinance
111
113
  # retrieve the quote data (an OpenStruct per quote)
112
114
  # the options param can be used to specify the following attributes:
113
115
  # :raw - if true, each column will be converted (to numbers, dates, etc)
114
- def quotes(symbols_array, columns_array = [
115
- :symbol, :last_trade_price, :last_trade_date,
116
- :change, :previous_close], options = {})
116
+ def quotes(symbols_array, columns_array = [:symbol, :last_trade_price, :last_trade_date, :change, :previous_close], options = {})
117
+ # remove invalid keys
118
+ columns_array.reject! { |c| !COLUMNS.key?(c) }
119
+ columns_array << :symbol if columns_array.index(:symbol).nil?
120
+
121
+ # "N/A" is never present if { raw = false }
122
+ options[:na_as_nil] = true if options[:raw] == false
117
123
 
118
- options[:raw] ||= true
119
124
  ret = []
120
125
  symbols_array.each_slice(SYMBOLS_PER_REQUEST) do |symbols|
121
126
  read_quotes(symbols.join("+"), columns_array).map do |row|
127
+ data = row.to_hash
122
128
  if options[:na_as_nil]
123
- row.each{ |key, value| row[key] = nil if value == 'N/A' }
129
+ data.each { |key, value| data[key] = nil if value == 'N/A' }
124
130
  end
125
- ret << OpenStruct.new(row.to_hash)
131
+ if options[:raw] == false
132
+ data.each { |key, value| data[key] = format(value, COLUMNS[key][1]) }
133
+ end
134
+ ret << OpenStruct.new(data)
126
135
  end
127
136
  end
128
137
  ret
129
138
  end
130
139
 
131
- def quote(symbol, columns_array = [
132
- :symbol, :last_trade_price, :last_trade_date,
133
- :change, :previous_close], options = {})
134
-
140
+ def quote(symbol, columns_array = [:symbol, :last_trade_price, :last_trade_date, :change, :previous_close], options = {})
135
141
  options[:raw] ||= true
136
142
  quotes([symbol], columns_array, options).first
137
143
  end
@@ -154,27 +160,36 @@ module YahooFinance
154
160
 
155
161
  def splits(symbol, options = {})
156
162
  rows = read_splits(symbol, options).select { |row| row[0] == "SPLIT" }
157
- rows.map do |row|
158
- type, date, value = row
163
+ rows.map do |type, date, value|
159
164
  after, before = value.split(":")
160
- OpenStruct.new(
161
- symbol: symbol, date: Date.strptime(date.strip, "%Y%m%d"),
162
- before: before.to_i, after: after.to_i)
165
+ OpenStruct.new(symbol: symbol, date: Date.strptime(date.strip, "%Y%m%d"), before: before.to_i, after: after.to_i)
166
+ end
167
+ end
168
+
169
+ def format(str, type)
170
+ if str.nil?
171
+ str
172
+ elsif type == BigDecimal
173
+ BigDecimal.new(str)
174
+ elsif type == DateTime
175
+ DateTime.parse(str)
176
+ elsif type == Time
177
+ Time.parse(str)
178
+ else
179
+ str
163
180
  end
164
181
  end
165
182
 
166
183
  private
167
184
 
168
185
  def read_quotes(symb_str, cols)
169
- columns = "#{cols.map { |col| COLUMNS[col] }.join("")}"
186
+ columns = "#{cols.map { |col| COLUMNS[col][0] }.join("")}"
170
187
  conn = open("https://download.finance.yahoo.com/d/quotes.csv?s=#{URI.escape(symb_str)}&f=#{columns}")
171
188
  CSV.parse(conn.read, headers: cols)
172
189
  end
173
190
 
174
191
  def read_historical(symbol, options)
175
- params = {
176
- s: URI.escape(symbol), g: HISTORICAL_MODES[options[:period]],
177
- ignore: ".csv" }
192
+ params = { s: URI.escape(symbol), g: HISTORICAL_MODES[options[:period]], ignore: ".csv" }
178
193
  if options[:start_date]
179
194
  params[:a] = options[:start_date].month-1
180
195
  params[:b] = options[:start_date].day
@@ -191,8 +206,7 @@ module YahooFinance
191
206
  cols = if options[:period] == :dividends_only
192
207
  [:dividend_pay_date, :dividend_yield]
193
208
  else
194
- [:trade_date, :open, :high, :low,
195
- :close, :volume, :adjusted_close]
209
+ [:trade_date, :open, :high, :low, :close, :volume, :adjusted_close]
196
210
  end
197
211
  result = CSV.parse(conn.read, headers: cols)
198
212
  #:first_row, :header_converters => :symbol)
@@ -201,8 +215,7 @@ module YahooFinance
201
215
  end
202
216
 
203
217
  def read_splits(symbol, options)
204
- params = {
205
- s: URI.escape(symbol), g: "v" }
218
+ params = { s: URI.escape(symbol), g: "v" }
206
219
  if options[:start_date]
207
220
  params[:a] = options[:start_date].month-1
208
221
  params[:b] = options[:start_date].day
@@ -1,4 +1,4 @@
1
1
  # Version Variable for Yahoo Finance Gem
2
2
  module YahooFinance
3
- VERSION = '1.1.0'
3
+ VERSION = '1.2.0'
4
4
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yahoo-finance
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Herval Freire
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-16 00:00:00.000000000 Z
11
+ date: 2017-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.8'
19
+ version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.8'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement