Class: SQA::DataFrame::YahooFinance

Inherits:
Object
  • Object
show all
Defined in:
lib/sqa/data_frame/yahoo_finance.rb

Constant Summary collapse

CONNECTION =
Faraday.new(url: 'https://finance.yahoo.com')
HEADERS =
[
  :timestamp,       # 0
  :open_price,      # 1
  :high_price,      # 2
  :low_price,       # 3
  :close_price,     # 4
  :adj_close_price, # 5
  :volume,          # 6
]
HEADER_MAPPING =

The Yahoo Finance Headers are being remapped so that the header can be used as a method name to access the vector.

{
  "Date"      => HEADERS[0],
  "Open"      => HEADERS[1],
  "High"      => HEADERS[2],
  "Low"       => HEADERS[3],
  "Close"     => HEADERS[4],
  "Adj Close" => HEADERS[5],
  "Volume"    => HEADERS[6]
}

Class Method Summary collapse

Class Method Details

.recent(ticker) ⇒ Object

Scrape the Yahoo Finance website to get recent historical prices for a specific ticker

ticker String the security to retrieve returns a DataFrame



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/sqa/data_frame/yahoo_finance.rb', line 47

def self.recent(ticker)
  response  = CONNECTION.get("/quote/#{ticker.upcase}/history")
  doc       = Nokogiri::HTML(response.body)
  table     = doc.css('table').first

  raise "NoDataError" if table.nil?

  rows      = table.css('tbody tr')

  aofh = []

  rows.each do |row|
    cols = row.css('td').map{|c| c.children[0].text}

    next unless 7 == cols.size
    next if cols[1]&.include?("Dividend")

    if cols.any?(nil)
      debug_me('== ERROR =='){[
        :cols
      ]}
      next
    end

    cols[0] = Date.parse(cols[0]).to_s
    cols[6] = cols[6].tr(',','').to_i
    (1..5).each {|x| cols[x] = cols[x].to_f}
    aofh << HEADERS.zip(cols).to_h
  end

  aofh
end