Class: YahooFinance::Stock

Inherits:
Object
  • Object
show all
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[:ANALYST_ESTIMATES] + AVL_FIELDS[:COMPANY_PROFILE])
@@insert_variable_delays =
false
@@insert_variable_delay =
2
@@retry_times =
3
@@retry_variable_delay =
30

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbols, fields = nil) ⇒ Stock

Returns a new instance of Stock.



152
153
154
155
156
157
158
159
160
# File 'lib/yahoo_finance/stock.rb', line 152

def initialize(symbols, fields = nil)
  @symbols = symbols
  @fields = []
  if fields
    fields.each do |aField|
      add_field aField
    end
  end
end

Instance Attribute Details

Returns the value of attribute print_progress.



124
125
126
# File 'lib/yahoo_finance/stock.rb', line 124

def print_progress
  @print_progress
end

#results_hashObject (readonly)

Returns the value of attribute results_hash.



124
125
126
# File 'lib/yahoo_finance/stock.rb', line 124

def results_hash
  @results_hash
end

Class Method Details

.insert_variable_delayObject



144
145
146
# File 'lib/yahoo_finance/stock.rb', line 144

def self.insert_variable_delay
  @@insert_variable_delay
end

.insert_variable_delay=(aDelay) ⇒ Object



148
149
150
# File 'lib/yahoo_finance/stock.rb', line 148

def self.insert_variable_delay= aDelay
  @@insert_variable_delay = aDelay
end

.insert_variable_delaysObject



136
137
138
# File 'lib/yahoo_finance/stock.rb', line 136

def self.insert_variable_delays
  @@insert_variable_delays
end

.insert_variable_delays=(trueFalse) ⇒ Object



140
141
142
# File 'lib/yahoo_finance/stock.rb', line 140

def self.insert_variable_delays= trueFalse
  @@insert_variable_delays = trueFalse
end

Instance Method Details

#add_field(aField) ⇒ Object



185
186
187
188
189
# File 'lib/yahoo_finance/stock.rb', line 185

def add_field(aField)
  if @@available_fields.include? aField
    @fields << aField
  end
end

#add_symbol(aSymbol) ⇒ Object



180
181
182
183
# File 'lib/yahoo_finance/stock.rb', line 180

def add_symbol(aSymbol)
  aSymbol = aSymbol.strip || ""
  @symbols << aSymbol if (aSymbol != "" && (@symbols.include?(aSymbol) == false))
end

#allocate_fields_to_connectionsObject



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/yahoo_finance/stock.rb', line 262

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_fieldsObject



170
171
172
# File 'lib/yahoo_finance/stock.rb', line 170

def available_fields
  @@available_fields
end

#available_fields_and_descriptionsObject

NOTE – UPDATE THIS WHEN YOU ADD A NEW PAGE %%



175
176
177
178
# File 'lib/yahoo_finance/stock.rb', line 175

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

#fetchObject



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
260
# File 'lib/yahoo_finance/stock.rb', line 191

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

#fieldsObject



166
167
168
# File 'lib/yahoo_finance/stock.rb', line 166

def fields
  return @fields
end

#symbolsObject



162
163
164
# File 'lib/yahoo_finance/stock.rb', line 162

def symbols
  return @symbols
end