forked from dulearnaux/NetfondsData
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Netfonds_Ticker_List.py
230 lines (210 loc) · 9.92 KB
/
Netfonds_Ticker_List.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# -*- coding: utf-8 -*-
def get_netfonds_tickers(toget=['SPX','ETF']):
"""
Returns pandas.DataFrame(columns=[['ticker','folder']])
NYSE returns ~1800 NYSE tickers
AMEX returns ~700 AMEX tickers
NASDAW returns ~2000 NASDAQ tickers
SPX return S&P500 tickers sourced from the above exchanges
ETF returns a number of ETF tickers sourced from the above exchanges
also appends any additional tickers passe in the list
"""
'''
need to debug/test the following
if etf in toget loop, for spx as well
todelete and etf.drop, for spx as well
does temp.append work as expeccted, are all arguments dataframes?
'''
import pandas as pd
#from urllib import urlretrieve as ul
#import numpy as np
import urllib2
import io
filepath = 'D:\\Financial Data\\Netfonds\\TickerList\\'
temp = pd.DataFrame(columns=['ticker'])
folder = dict()
date = pd.datetime.today() - pd.offsets.BDay(1)
date=date.date()
datestr = date.strftime('%Y%m%d')
daystr=str(date.day)
monthstr=str(date.month)
yearstr=str(date.year)
names = ['ticker','b','c','d','e','f','g','h']
AMEX = pd.read_csv(filepath+'AMEX.A.txt', sep=';', names =names)
NASDAQ = pd.read_csv(filepath+'NASDAQ.O.txt', sep=';',names =names)
NYSE = pd.read_csv(filepath+'NYSE.N.txt', sep=';',names =names)
if 'ETF' in toget:
todelete = []
ETF = pd.read_csv(filepath+'ETF.txt',sep=',',header=0) #the file should alrady append the exchange letter
for i in ETF.index:
if ETF['ticker'][i] in AMEX['ticker'].values+'.A':
folder[ETF['ticker'][i]]='ETF'
elif ETF['ticker'][i] in NASDAQ['ticker'].values+'.O':
folder[ETF['ticker'][i]]='ETF'
elif ETF['ticker'][i] in NYSE['ticker'].values+'.N':
folder[ETF['ticker'][i]]='ETF'
else:
print "Error: Can't idendify ETF ticker exchange for "+ETF['ticker'][i]
todelete.append(i)
ETF = ETF.drop(todelete) #delete unidentified tickers
todelete=[]
ETF2 = pd.read_csv(filepath+'ETFvix.txt',sep=',',header=0) #need to ID the exchange the SPX ticker is on
for i in ETF2.index:
if ETF2['ticker'][i] in AMEX['ticker'].values:
ETF2['ticker'][i] = ETF2['ticker'][i]+'.A'
folder[ETF2['ticker'][i]]='ETF'
elif ETF2['ticker'][i] in NASDAQ['ticker'].values:
ETF2['ticker'][i]= ETF2['ticker'][i]+'.O'
folder[ETF2['ticker'][i]]='ETF'
elif ETF2['ticker'][i] in NYSE['ticker'].values:
ETF2['ticker'][i]= ETF2['ticker'][i]+'.N'
folder[ETF2['ticker'][i]]='ETF'
else:
print "Error: Can't idendify ETFvix ticker exchange for "+ETF2['ticker'][i]
todelete.append(i)
ETF2 = ETF2.drop(todelete) #delete unidentified tickers
temp = pd.concat([temp,ETF])
temp = pd.concat([temp,ETF2])
toget.remove('ETF')
# SPX components are sourced from a website which extracts them from S&P
if 'SPX' in toget:
todelete=[]
url='https://raw.github.com/datasets/s-and-p-500-companies/master/data/constituents.csv'
#(filename,headers)= ul(url)
urlread=0
while urlread==0:
try:
buff= urllib2.urlopen(url)
cvsstring = buff.read()
SPX = pd.read_csv(io.BytesIO(cvsstring),header=0)
flname='SPX.'+datestr+'.txt'
SPX.to_csv(filepath+flname, sep=';', header=True, index=False)
urlread=1
except urllib2.URLError,e:
print 'OOPS: URLError'
print e
#SPX = pd.read_csv(filename, header=0)
SPX.rename(columns={'Symbol':'ticker'},inplace=True)
#need to ID the exchange the SPX ticker is on
for i in SPX.index:
if SPX['ticker'][i] in AMEX['ticker'].values:
SPX['ticker'][i] = SPX['ticker'][i]+'.A'
folder[SPX['ticker'][i]]='SPX'
elif SPX['ticker'][i] in NASDAQ['ticker'].values:
SPX['ticker'][i]= SPX['ticker'][i]+'.O'
folder[SPX['ticker'][i]]='SPX'
elif SPX['ticker'][i] in NYSE['ticker'].values:
SPX['ticker'][i]= SPX['ticker'][i]+'.N'
folder[SPX['ticker'][i]]='SPX'
else:
print "Error: Can't idendify SPX ticker exchange for "+SPX['ticker'][i]
todelete.append(i)
SPX = SPX.drop(todelete) #delete unidentified tickers
temp = pd.concat([temp,SPX])
toget.remove('SPX')
url='http://www.netfonds.no/quotes/exchange.php?'
url=url+'exchange=%s'
url=url+'&at_day=' + daystr
url=url+'&at_month=' +monthstr
url=url+'&at_year=' +yearstr
url=url+'&format=csv'
if 'AMEX' in toget:
urlread=0
#get ticker names from netfonds website
while urlread==0:
try:
buff = urllib2.urlopen(url %'A')
csvstring = buff.read()
if len(csvstring)==0:
print 'No Amex data for this date'
print 'Using AMEX tickers from the .csv file on disk'
#AMEX = pd.read_csv(filepath+'AMEX.A.txt', sep=';', names =names)
urlread=1
continue
else:
df = pd.read_csv(io.BytesIO(csvstring), sep=';', names=names)
df.to_csv(filepath+'AMEX.A.txt', sep=';', header=False, index=False)
flname = 'AMEX.A.'+datestr+'.txt'
df.to_csv(filepath+flname, sep=';', header=False, index=False)
urlread=1
except urllib2.URLError,e:
print 'OOPS: URLError on AMEX ticker list pull'
print e
AMEX['ticker'] = AMEX['ticker']+'.A' #append the .A for netfonds exchange ID
temp = pd.concat([temp,AMEX])
toget.remove('AMEX')
if 'NASDAQ' in toget:
urlread=0
#get ticker names from netfonds website
while urlread==0:
try:
buff = urllib2.urlopen(url %'O')
csvstring = buff.read()
if len(csvstring)==0:
print 'No Nasdaq data for this date'
print 'Using Nasdaq tickers from the .csv file on disk'
#NASDAQ = pd.read_csv(filepath+'NASDAQ.O.txt', sep=';', names =names)
urlread=1
continue
else:
df = pd.read_csv(io.BytesIO(csvstring), sep=';', names=names)
df.to_csv(filepath+'NASDAQ.O.txt', sep=';',header=False, index=False)
flname = 'NASDAQ.O.'+datestr+'.txt'
df.to_csv(filepath+flname, sep=';', header=False, index=False)
urlread=1
except urllib2.URLError,e:
print 'OOPS: URLError on NASDAQ ticker list pull'
print e
NASDAQ['ticker'] = NASDAQ['ticker']+'.O' #append the .O for netfonds exchange ID
#temp = temp.append(pd.DataFrame(NASDAQ['ticker'], columns='ticker'))
temp = pd.concat([temp,NASDAQ])
toget.remove('NASDAQ')
if 'NYSE' in toget:
urlread=0
#get ticker names from netfonds website
while urlread==0:
try:
buff = urllib2.urlopen(url %'N')
csvstring = buff.read()
if len(csvstring)==0:
print 'No NYSE data for this date'
print 'Using NYSE tickers from the .csv file on disk'
#NASDAQ = pd.read_csv(filepath+'NYSE.N.txt', sep=';', names =names)
urlread=1
continue
else:
df = pd.read_csv(io.BytesIO(csvstring), sep=';', names=names)
df.to_csv(filepath+'NYSE.N.txt', sep=';', header=False, index=False)
flname = 'NYSE.N.'+datestr+'.txt'
df.to_csv(filepath+flname, sep=';', header=False, index=False)
urlread=1
except urllib2.URLError,e:
print 'OOPS: URLError on NYSE ticker list pull'
print e
except socket.timeout, e:
print TCKR + ' OOPS: socket timeout on NYSE ticker list pull'
print e
except socket.error, e:
print TCKR + ' OOPS: socket error on NYSE ticker list pull'
print e
except httplib.IncompleteRead, e:
print TCKR + ' OOPS: httplib Imcomplete error on NYSE ticker list pull'
print e
NYSE['ticker'] = NYSE['ticker']+'.N' #append the .N for netfonds exchange ID
#temp = temp.append(pd.DataFrame(NYSE['ticker'], columns='ticker'))
temp = pd.concat([temp,NYSE])
toget.remove('NYSE')
if (len(toget) != 0): #add remaing tickers to the list. This enables adding tickers not on the usual exchanges
srs = pd.Series(toget)
data = pd.DataFrame(srs,columns=['ticker']) #append remaining tickers passed in 'toget'
temp = temp.append(data)
#temp = pd.concat([NYSE, AMEX, NASDAQ], ignore_index=True)
temp = temp.drop_duplicates(cols='ticker') #remove duplicates
temp['folder']=''
for key_,val_ in folder.iteritems():
temp['folder'][temp.ticker==key_]=val_ +'\\'
temp.index = range(len(temp.index))
return temp[['ticker','folder']]
if __name__=='__main__':
tickers = get_netfonds_tickers(toget=['SPX','ETF','AMEX','NYSE','NASDAQ'])
print 'hey'