Class: YahooFinance::Stock
- Inherits:
-
Object
- Object
- YahooFinance::Stock
- Defined in:
- lib/yahoo_finance/stock.rb
Constant Summary collapse
- @@available_fields =
(AVL_FIELDS[:YAHOO_STOCK_FIELDS] + AVL_FIELDS[:KEY_STATISTICS] + AVL_FIELDS[:COMPANY_EVENTS] + AVL_FIELDS[:ANALYST_OPINION] + AVL_FIELDS[:COMPANY_PROFILE])
- @@insert_variable_delays =
false
- @@insert_variable_delay =
2
- @@retry_times =
3
- @@retry_variable_delay =
30
Instance Attribute Summary collapse
-
#print_progress ⇒ Object
readonly
Returns the value of attribute print_progress.
-
#results_hash ⇒ Object
readonly
Returns the value of attribute results_hash.
Class Method Summary collapse
- .insert_variable_delay ⇒ Object
- .insert_variable_delay=(aDelay) ⇒ Object
- .insert_variable_delays ⇒ Object
- .insert_variable_delays=(trueFalse) ⇒ Object
Instance Method Summary collapse
- #add_field(aField) ⇒ Object
- #add_symbol(aSymbol) ⇒ Object
- #allocate_fields_to_connections ⇒ Object
- #available_fields ⇒ Object
-
#available_fields_and_descriptions ⇒ Object
NOTE – UPDATE THIS WHEN YOU ADD A NEW PAGE %%.
- #fetch ⇒ Object
- #fields ⇒ Object
-
#initialize(symbols, fields = nil) ⇒ Stock
constructor
A new instance of Stock.
- #symbols ⇒ Object
Constructor Details
#initialize(symbols, fields = nil) ⇒ Stock
Returns a new instance of Stock.
151 152 153 154 155 156 157 158 159 |
# File 'lib/yahoo_finance/stock.rb', line 151 def initialize(symbols, fields = nil) @symbols = symbols @fields = [] if fields fields.each do |aField| add_field aField end end end |
Instance Attribute Details
#print_progress ⇒ Object (readonly)
Returns the value of attribute print_progress.
123 124 125 |
# File 'lib/yahoo_finance/stock.rb', line 123 def print_progress @print_progress end |
#results_hash ⇒ Object (readonly)
Returns the value of attribute results_hash.
123 124 125 |
# File 'lib/yahoo_finance/stock.rb', line 123 def results_hash @results_hash end |
Class Method Details
.insert_variable_delay ⇒ Object
143 144 145 |
# File 'lib/yahoo_finance/stock.rb', line 143 def self.insert_variable_delay @@insert_variable_delay end |
.insert_variable_delay=(aDelay) ⇒ Object
147 148 149 |
# File 'lib/yahoo_finance/stock.rb', line 147 def self.insert_variable_delay= aDelay @@insert_variable_delay = aDelay end |
.insert_variable_delays ⇒ Object
135 136 137 |
# File 'lib/yahoo_finance/stock.rb', line 135 def self.insert_variable_delays @@insert_variable_delays end |
.insert_variable_delays=(trueFalse) ⇒ Object
139 140 141 |
# File 'lib/yahoo_finance/stock.rb', line 139 def self.insert_variable_delays= trueFalse @@insert_variable_delays = trueFalse end |
Instance Method Details
#add_field(aField) ⇒ Object
184 185 186 187 188 |
# File 'lib/yahoo_finance/stock.rb', line 184 def add_field(aField) if @@available_fields.include? aField @fields << aField end end |
#add_symbol(aSymbol) ⇒ Object
179 180 181 182 |
# File 'lib/yahoo_finance/stock.rb', line 179 def add_symbol(aSymbol) aSymbol = aSymbol.strip || "" @symbols << aSymbol if (aSymbol != "" && (@symbols.include?(aSymbol) == false)) end |
#allocate_fields_to_connections ⇒ Object
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/yahoo_finance/stock.rb', line 261 def allocate_fields_to_connections @fields_hash = {} AVL_FIELDS.keys.each do |driver| @fields_hash[driver] = [] end @fields.each do |aField| if AVL_FIELDS[:YAHOO_STOCK_FIELDS].include? aField @fields_hash[:YAHOO_STOCK_FIELDS] << aField else DRIVERS.keys.each do |driver| # puts "CHECKING DRIVER: #{driver} for field #{aField}" if AVL_FIELDS[driver].include? aField @fields_hash[driver] << aField end end end end end |
#available_fields ⇒ Object
169 170 171 |
# File 'lib/yahoo_finance/stock.rb', line 169 def available_fields @@available_fields end |
#available_fields_and_descriptions ⇒ Object
NOTE – UPDATE THIS WHEN YOU ADD A NEW PAGE %%
174 175 176 177 |
# File 'lib/yahoo_finance/stock.rb', line 174 def available_fields_and_descriptions ysh = {}; YahooFinance::AVL_FIELDS[:YAHOO_STOCK_FIELDS].each { |field| ysh[field] = "" } ysh.merge(YahooFinance::KeyStatistics::AVL_KEY_STATS).merge(YahooFinance::CompanyEvents::AVL_KEY_STATS) end |
#fetch ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/yahoo_finance/stock.rb', line 190 def fetch allocate_fields_to_connections # initialize symbol rows @results_hash = {} @symbols.each do |aSymbol| @results_hash[aSymbol.upcase] = {} # we do this to avoid problems with symbols misstyped with mixed upper/lower case end # First we fetch symbols under YAHOO_STOCK_FIELDS if @fields_hash[:YAHOO_STOCK_FIELDS].size > 0 # puts "SYMBOLS ARE: #{@symbols.to_s} AND PARAMETERS: #{@fields_hash[:YAHOO_STOCK_FIELDS].to_s}" # Yahoo Stock API has some limits, therefore, we need to chunk the symbols @symbols.each_slice(50).to_a.each do |symbol_slice| qt = YahooStock::Quote.new(:stock_symbols => symbol_slice) qt.add_parameters(:symbol) @fields_hash[:YAHOO_STOCK_FIELDS].each do |aField| qt.add_parameters(aField) end yshs = qt.results(:to_hash).output @fields_hash[:YAHOO_STOCK_FIELDS].each do |aField| yshs.each do |aSymbolRow| symbol = aSymbolRow[:symbol] value = aSymbolRow[aField] begin @results_hash[symbol][aField] = YahooFinance.parse_yahoo_field(value) rescue Exception => e puts "Failed in symbol #{symbol.to_s} field #{aField.to_s} value #{(value || "NULL").to_s}" puts "RESULT ROW: #{aSymbolRow.to_s}" end end end sleep(Random.new(Random.new_seed).rand*@@insert_variable_delay) if @@insert_variable_delays end end # Then we fetch fields from KEY STATISTICS DRIVERS.keys.each do |driver| if @fields_hash[driver].size > 0 puts "FETCHING FIELDS FOR #{driver.to_s}" if @print_progress stp = DRIVERS[driver].new # will overwrite symbol below @symbols.each do |aSymbol| tries = 0 now = Time.now stp.symbol = aSymbol puts "#{now.hour}:#{now.min}:#{now.sec}\tWorking on symbol #{aSymbol}" if @print_progress begin # stp = DRIVERS[driver].new(aSymbol) # will overwrite symbol below stp.fetch @fields_hash[driver].each do |aField| value = stp.value_for(aField) # this already parses the numbers @results_hash[aSymbol][aField] = value end sleep(Random.new(Random.new_seed).rand*@@insert_variable_delay) if @@insert_variable_delays rescue tries += 1 puts "Exception: #{$!.inspect} -- Retrying symbol #{aSymbol} time: #{tries.to_s}" if tries <= @@retry_times sleep(@@retry_variable_delay) retry end end end end puts "DONE witn #{driver.to_s}" if @print_progress end # puts "RIGHT BEFORE I RETURN THE BIG HASH: #{@results_hash}" @results_hash end |
#fields ⇒ Object
165 166 167 |
# File 'lib/yahoo_finance/stock.rb', line 165 def fields return @fields end |
#symbols ⇒ Object
161 162 163 |
# File 'lib/yahoo_finance/stock.rb', line 161 def symbols return @symbols end |