Class: SQA::DataFrame::YahooFinance
- Inherits:
-
Object
- Object
- SQA::DataFrame::YahooFinance
- 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 =
{ "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
-
.recent(ticker) ⇒ Object
Scrape the Yahoo Finance website to get recent historical prices for a specific ticker returns a Polars DataFrame.
Class Method Details
.recent(ticker) ⇒ Object
Scrape the Yahoo Finance website to get recent historical prices for a specific ticker returns a Polars DataFrame
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/sqa/data_frame/yahoo_finance.rb', line 40 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') data = rows.map do |row| cols = row.css('td').map { |c| c.children[0].text } next unless cols.size == 7 next if cols[1]&.include?("Dividend") next if cols.any?(nil) { "Date" => Date.parse(cols[0]).to_s, "Open" => cols[1].to_f, "High" => cols[2].to_f, "Low" => cols[3].to_f, "Close" => cols[4].to_f, "Adj Close" => cols[5].to_f, "Volume" => cols[6].tr(',', '').to_i } end.compact # Create Polars DataFrame then wrap in SQA::DataFrame polars_df = Polars::DataFrame.new(data) SQA::DataFrame.new(polars_df, mapping: HEADER_MAPPING) end |