Class: Market

Inherits:
Object
  • Object
show all
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",
  "Accept-Encoding" => "gzip,deflate",
  "Cache-Control" => "max-age=0",
  "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

Class Method Details

.chain(ticker, expiry) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/market.rb', line 121

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(get(url))
  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



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/market.rb', line 195

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(get(url))
  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

Returns:

  • (Boolean)


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/market.rb', line 155

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(get(url))
  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



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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/market.rb', line 66

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(get(url))
  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

.get(url) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/market.rb', line 47

def get(url)
  uri = URI.parse(url)

  request = Net::HTTP::Get.new uri.request_uri

  HEADERS.each do |name, value|
    request.add_field name, value
  end

  response = @http.request(uri, request)
  data = response.body

  case response["content-encoding"]
    when /gzip/    then Zlib::GzipReader.new(StringIO.new(data)).read
    when /deflate/ then Zlib::Inflate.inflate(data)
    else data
  end
end

.historical_prices(ticker, from = Date.today - 365) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/market.rb', line 179

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
    get(url)
  end

  # [newest, ..., oldest]
  csv.scan(/\d+\.\d+$/).map { |p| p.to_f }
end

.member_of?(ticker, index_ticker) ⇒ Boolean

Returns:

  • (Boolean)


217
218
219
220
221
# File 'lib/market.rb', line 217

def member_of?(ticker, index_ticker)
  index_members = INDEXES[index_ticker] ||= constituents(index_ticker)

  index_members.include?(ticker)
end

.with_retry(&block) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/market.rb', line 223

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