1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
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# This file retrieves the current stock information for a given
# set of stocks.
# Eli Hart
# 7/1/13
require 'open-uri'
require 'csv'
# This is the base uri for getting a quote
BASE = "http://download.finance.yahoo.com/d/quotes.csv"
# List of property codes - http://code.google.com/p/yahoo-finance-managed/wiki/enumQuoteProperty
PROPERTIES = %w[s0 o0 a0 b0 v0]
# Yahoo only permits you to retrieve 200 stocks at once
BATCH_SIZE = 200
# open list of stocks and create array with ticker symbols
stockList = Array.new
File.open('stock_list.txt') do |file|
while line = file.gets
stockList.push(line.split()[0])
end
end
# We are limited in how many stocks we can get at once. Do them in batches
count = 0
stockList.each_slice(BATCH_SIZE) do |batch|
# Append list of stocks to get to the 's' parameter, with ticker symbols
# separated by commas
query = BASE + '?s='
# add each stock to the request, separate with commas
batch.each do |stock|
query += stock + ','
end
# Remove last comma
query = query[0..-2]
# Add property paramters to customize quote details
query += '&f='
PROPERTIES.each {|p| query += p}
# End file with static csv parameter
query += '&e=.csv'
# Retrieve and store results
File.open("data/temp#{count}.csv", "wb") do |saved_file|
# the following "open" is provided by open-uri
open(query, 'rb') do |read_file|
saved_file.write(read_file.read)
end
end
count += 1
end
#combine results into one file
CSV.open("data/#{DateTime.now.to_s.gsub(':', '#')}.csv", "wb") do |result|
# Open each result file and copy results
count.times do |i|
path = "data/temp#{i}.csv"
CSV.foreach(path) do |row|
result << row
end
File.delete(path)
end
end