Class: Market
- Inherits:
-
Object
- Object
- Market
- Defined in:
- lib/market.rb
Defined Under Namespace
Classes: Quote
Constant Summary collapse
- HEADERS =
{ "Accept" => "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", "Accept-Charset" => "ISO-8859-1,utf-8;q=0.7,*;q=0.3", "Accept-Language" => "en-US,en;q=0.8", "Cache-Control" => "max-age=0", "Connection" => "keep-alive", "Host" => "finance.yahoo.com", "Referer" => "http://finance.yahoo.com/", "User-Agent" => "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.215 Safari/534.10" }
- INDEXES =
{}
Class Method Summary collapse
- .chain(ticker, expiry) ⇒ Object
- .constituents(ticker, offset = 0, traverse = true) ⇒ Object
- .event?(ticker, on_or_before_date) ⇒ Boolean
- .fetch(ticker, opts = {}) ⇒ Object
- .gchain(ticker, expiry) ⇒ Object
- .historical_prices(ticker, from = Date.today - 365) ⇒ Object
- .member_of?(ticker, index_ticker) ⇒ Boolean
- .with_retry(&block) ⇒ Object
Class Method Details
.chain(ticker, expiry) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/market.rb', line 133 def chain(ticker, expiry) url = "http://finance.yahoo.com/q/op?s=%s&m=%s" % [ticker, expiry.strftime("%Y-%m")] puts "Fetching #{url}..." if $VERBOSE doc = with_retry do Nokogiri::HTML.parse(open(url, HEADERS).read) end itm_call_data = doc. search("//table[@class='yfnc_datamodoutline1'][1]//td[@class='yfnc_h']"). map { |e| e.text } rows = itm_call_data.in_groups_of(8) rows.map do |row| strike = row[0].to_f * 100 symbol = row[1] last = row[2].to_f * 100 bid = row[4].to_f * 100 ask = row[5].to_f * 100 raise "Expected symbol, got #{symbol.inspect}" unless symbol =~ /^\w+\d+[CP]\d+$/ # Only pick symbols that have the correct expiry # Occurs when multiple series appear for the same month (i.e. weeklys) next unless symbol =~ /#{expiry.strftime("%y%m%d")}/ quote = Quote.new( :symbol => symbol, :last => last, :bid => bid, :ask => ask) [strike, quote] end.compact end |
.constituents(ticker, offset = 0, traverse = true) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/market.rb', line 207 def constituents(ticker, offset = 0, traverse = true) ticker = "^#{ticker}" unless ticker =~ /^\^/ url = "http://finance.yahoo.com/q/cp?s=%s&c=%s" % [CGI.escape(ticker), offset] puts "Fetching #{url}..." if $VERBOSE doc = with_retry do Nokogiri::HTML.parse(open(url, HEADERS).read) end symbols = doc.at("#yfncsumtab").search("tr td:first-child.yfnc_tabledata1").map{ |td| td.text } next_link = doc.at("#yfncsumtab").at("//a[text()='Next']") if next_link && traverse return symbols + constituents(ticker, offset + 1) end return symbols end |
.event?(ticker, on_or_before_date) ⇒ Boolean
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/market.rb', line 167 def event?(ticker, on_or_before_date) url = "http://finance.yahoo.com/q/ce?s=%s" % ticker puts "Fetching #{url}..." if $VERBOSE doc = with_retry do Nokogiri::HTML.parse(open(url, HEADERS).read) end return false if doc.text =~ /There is no Company Events data/ fragment = doc. search("//table[@class='yfnc_datamodoutline1'][1]//td[@class='yfnc_tabledata1']") return false if fragment.text =~ /No Upcoming Events/i events = fragment.map{|e|e.text}.in_groups_of(3) events.any? do |date, event, _| event_date = Date.strptime date, "%d-%b-%y" event_date <= on_or_before_date end end |
.fetch(ticker, opts = {}) ⇒ Object
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/market.rb', line 41 def fetch(ticker, opts = {}) url = "http://finance.yahoo.com/q?s=%s" % ticker puts "Fetching #{url}..." if $VERBOSE doc = with_retry do Nokogiri::HTML.parse(open(url, HEADERS).read) end # Realtime last is at yfs_l90_sym, use if exists if opts[:try_rt] last = (doc.at_css("#yfs_l90_#{ticker.downcase}").text.to_f * 100) rescue nil end last ||= (doc.at_css("#yfs_l10_#{ticker.downcase}").text.to_f * 100) bid = (doc.at_css("#yfs_b00_#{ticker.downcase}").text.to_f * 100) rescue 0 ask = (doc.at_css("#yfs_a00_#{ticker.downcase}").text.to_f * 100) rescue 0 quote = Quote.new( :symbol => ticker, :last => last, :bid => bid, :ask => ask) if opts[:extra] name = doc.at('h1').text.scan(/(.*) \([\w-]+\)$/).to_s # strip trailing (SYMBOL) mktcap = doc.at_css("#yfs_j10_#{ticker.downcase}").text rescue nil divyield = doc.at("#table2 .end td").text.scan(/\((.*)\)/).to_s.to_f rescue nil begin pe_row = doc.at("#table2 tr:nth-child(6)") pe_label, pe_data = pe_row.search("th,td").map{|e|e.text} unless pe_label =~ %r(P/E) puts "P/E label mismatch" pe_data = nil end rescue # nothing end sector, industry = doc.search("#company_details a").map{|e|e.text} rescue nil quote.extra = { :name => name, :mktcap => mktcap, :divyield => divyield, :pe => pe_data.to_f, :sector => sector, :industry => industry } end puts quote.inspect if $VERBOSE quote end |
.gchain(ticker, expiry) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/market.rb', line 97 def gchain(ticker, expiry) url = "http://www.google.com/finance/option_chain?q=%s&expd=%s&expm=%s&expy=%s&output=json" url = url % [ticker.gsub(/-/, "."), expiry.day, expiry.month, expiry.year] puts "Fetching #{url}..." if $VERBOSE doc = with_retry do JSON.parse( open(url).read. gsub(/(\:"\w+):/, '\1'). # Remove extra colon that appears inside strings gsub('\\', ""). # Remove slashes gsub(/(['"])?(\w+)(['"])?:/m, '"\2":'), # Quote each key :symbolize_names => true) end doc[:calls].map do |call| quote = Quote.new( :symbol => call[:s], :last => call[:p].to_f * 100, :bid => call[:b].to_f * 100, :ask => call[:a].to_f * 100 ) unless quote.symbol =~ /^\w+\d+[CP]\d+$/ puts "Dropping bad option #{quote.symbol.inspect}" next end unless quote.bid > 0 and quote.ask > 0 puts "Option #{quote.symbol.inspect} has no bid or ask" next end [call[:strike].to_f * 100, quote] end.compact end |
.historical_prices(ticker, from = Date.today - 365) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/market.rb', line 191 def historical_prices(ticker, from = Date.today - 365) to = Date.today url = "http://ichart.finance.yahoo.com/table.csv?s=%s&a=%s&b=%s&c=%s&d=%se=%s&f=%sg=d&ignore=.csv" url = url % [ticker, from.month - 1, from.day, from.year, to.month - 1, to.year, to.day] puts "Fetching #{url}..." if $VERBOSE csv = with_retry do open(url, HEADERS).read end # [newest, ..., oldest] csv.scan(/\d+\.\d+$/).map { |p| p.to_f } end |
.member_of?(ticker, index_ticker) ⇒ Boolean
229 230 231 232 233 |
# File 'lib/market.rb', line 229 def member_of?(ticker, index_ticker) index_members = INDEXES[index_ticker] ||= constituents(index_ticker) index_members.include?(ticker) end |
.with_retry(&block) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/market.rb', line 235 def with_retry(&block) retries = 20 begin timeout(5) do block.call end rescue Exception retries -= 1 unless retries.zero? puts "Got error, retrying" puts $! retry end raise end end |