1 '''
2 Created on Apr 8, 2011
3
4 @author: sjoshi42
5 @summary: This module get stock data from yahoo finance
6 '''
7
8 import qstkutil.DataAccess as da
9 import qstkutil.utils as utils
10 import urllib2
11 import urllib
12 import datetime
13 import sys
14 import os
15
17 _list= list()
18
19
20
21
22 _date= _str.partition(",")[0]
23 rest= _str.partition(",")[2]
24
25
26 _list.append(rest.partition(",")[0])
27 rest= rest.partition (",")[2]
28
29
30 _list.append(rest.partition(",")[0])
31 rest= rest.partition (",")[2]
32
33
34 _list.append (rest.partition(",")[0])
35 rest= rest.partition(",")[2]
36
37
38 _list.append (rest.partition(",")[0])
39 rest= rest.partition (",")[2]
40
41
42
43 vol= rest.partition(",")[0]
44
45
46 adj_close= float (rest.partition (",")[2])
47
48
49 _list = map (lambda x: (float(x)), _list)
50
51 try:
52 ratio= adj_close/ _list[-1]
53 _list= map (lambda x: (x * ratio), _list)
54 _list= map (lambda x: (str(x)), _list)
55 _list_concat= ",".join(_list)
56 _str= _date + "," + _list_concat + "," + vol + "," + str (adj_close) + "\n"
57 except ZeroDivisionError:
58 print ("Warning: Close value is Zero.")
59
60
61 return _str
62
63
65
66 path= os.environ['QSDATA']
67
68 get_data_for_exchange("NASDAQ", path)
69 get_data_for_exchange("NYSE", path)
70 get_data_for_exchange("AMEX", path)
71
72
74
75
76
77 data_path= data_path + "/Raw/Yahoo/US/" + str (exchange) + "/"
78
79
80 if not (os.access(data_path, os.F_OK)):
81 os.makedirs(data_path)
82
83 utils.clean_paths(data_path)
84
85 symbol_list= list()
86
87 print "Getting list of stocks.."
88
89 try:
90 nasdaq_params= urllib.urlencode ({'exchange':str(exchange), 'render':'download'})
91 nasdaq_get= urllib2.urlopen ('http://www.nasdaq.com/screening/companies-by-name.aspx?%s' % nasdaq_params)
92 symbol_list.append (nasdaq_get.readline())
93 while (len (symbol_list[-1]) > 0):
94 symbol_list.append (nasdaq_get.readline())
95
96 symbol_list.pop(0)
97 symbol_list.pop(-1)
98
99
100
101 symbol_list = map(lambda x:(x.partition(str("\""))[2]),symbol_list)
102 symbol_list = map(lambda x:(x.partition(str("\""))[0]),symbol_list)
103
104 except urllib2.HTTPError:
105 print "Unable to get list of stocks from server. Please check your internet connection and retry."
106 except:
107 print"Unknown error occoured when getting list of stocks from server."
108
109 print "Got " + str (len(symbol_list)) + " symbols. Now getting symbol data..."
110
111 _now =datetime.datetime.now();
112 miss_ctr=0;
113
114 for symbol in symbol_list:
115 symbol_data=list()
116 print "Getting " + str (symbol)
117
118 try:
119 params= urllib.urlencode ({'a':03, 'b':12, 'c':2000, 'd':_now.month, 'e':_now.day, 'f':_now.year, 's': str(symbol)})
120 url_get= urllib2.urlopen("http://ichart.finance.yahoo.com/table.csv?%s" % params)
121
122 header= url_get.readline()
123 symbol_data.append (url_get.readline())
124 while (len(symbol_data[-1]) > 0):
125 symbol_data.append(url_get.readline())
126
127
128 symbol_data.pop(-1)
129
130
131 symbol_data= map (adjust, symbol_data)
132
133
134 symbol_data = map(lambda x:(x.replace("-", "")),symbol_data)
135 symbol_data = map(lambda x:(str(symbol) + "," + str(x)) ,symbol_data)
136
137
138 f= open (data_path + symbol + ".csv", 'w')
139
140
141 f.write (header)
142
143 while (len(symbol_data) > 0):
144 f.write (symbol_data.pop())
145
146 f.close();
147
148
149
150
151
152 except urllib2.HTTPError:
153 miss_ctr= miss_ctr+1
154 print "Unable to fetch data for stock: " + str (symbol)
155 except urllib2.URLError:
156 print "URL Error for stock: " + str (symbol)
157
158
159
160
161
162
163
164 print "All done. Got " + str (len(symbol_list) - miss_ctr) + " stocks. Could not get " + str (miss_ctr) + " stocks."
165
166
167
168 if __name__ == '__main__':
169 main()
170