Reputation: 3927
I'm reading in stock data from Yahoo associated with the "tickers" (stock codes) provided to me in a CSV file.However, some of the stock codes are actually not available on Yahoo, so I added the Exception Handling portion below. However, now I get the Traceback Error t1=t2=t3=sum[0][0] #just finding the three min distances
IndexError: list index out of range
and I'm unable to decipher how to solve it.
The first code snippet is what my code looked like without Exception Handling and worked appropriately. The second is the one with Exception Handling.
First Snippet
import pandas
import pandas.io.data as web
import datetime
import csv
f1=open('C:\Users\Username\Documents\Programming\Financialdata.csv') #Enter the location of the file
c1= csv.reader(f1)
tickers =[]
for row in c1: #reading tickers from the csv file
tickers.append(row)
start=datetime.datetime(2012,1,1)
end=datetime.datetime(2013,1,1)
l=[]; m=[]; tickernew=[]
i=0;j=0; k=0; z=[]
for tick in tickers[0]:
f=web.DataReader(tick,'yahoo', start,end)
if len(f)==250: #checking if the stock was traded for 250 days
tickernew.append(tick) #new ticker list to keep track of the new index number of tickers
k = k + 1 #k keeps track of the number of new tickers
for i in range(0,len(f)-1):
m.append(f['Adj Close'][i+1]/f['Adj Close'][i]) #calculating daily return and storing in list m
if i==0:
z.append(m[i]) #calculating time series of return list z
elif i>0:
z.append(z[i-1]*m[i])
l.append(z) #storing the lists z for all the stocks in list l (list of list)
m=[]
z=[]
sum=[[0 for x in range (k)]for x in range (k)] #2d matrix for the squared distance
for i in range (k):
for j in range (i+1,k): # it will be a upper triangular matrix
for p in range (len(f)-1):
sum[i][j]= sum[i][j] + (l[i][p] - l[j][p])**2 #calculating distance
for i in range (k): #setting the lower half of the matrix 1 (if we see 1 in the answer we will set a higher limit but typically the distance squared is less than 1)
for j in range (i+1):
sum[i][j]=1
t1=t2=t3=sum[0][0] #just finding the three min distances
Second Snippet (with Exception Handling and results in an Error)
import pandas
import pandas.io.data as web
import datetime
import csv
f1=open('C:\Users\Username\Documents\Programming\Financialdata.csv') #Enter the location of the file
c1= csv.reader(f1)
tickers =[]
for row in c1: #reading tickers from the csv file
tickers.append(row)
start=datetime.datetime(2012,1,1)
end=datetime.datetime(2013,1,1)
l=[]; m=[]; tickernew=[]
i=0;j=0; k=0; z=[]
for tick in tickers[0]:
try: #Exception Handling
f=web.DataReader(tick,'yahoo', start,end)
except Exception, e:
print "This ticker does not belong to Yahoo. Skip to next ticker: ",
print e
break
if len(f)==250: #checking if the stock was traded for 250 days
tickernew.append(tick) #new ticker list to keep track of the new index number of tickers
k = k + 1 #k keeps track of the number of new tickers
for i in range(0,len(f)-1):
m.append(f['Adj Close'][i+1]/f['Adj Close'][i]) #calculating daily return and storing in list m
if i==0:
z.append(m[i]) #calculating time series of return list z
elif i>0:
z.append(z[i-1]*m[i])
l.append(z) #storing the lists z for all the stocks in list l (list of list)
m=[]
z=[]
sum=[[0 for x in range (k)]for x in range (k)] #2d matrix for the squared distance
for i in range (k):
for j in range (i+1,k): # it will be a upper triangular matrix
for p in range (len(f)-1):
sum[i][j]= sum[i][j] + (l[i][p] - l[j][p])**2 #calculating distance
for i in range (k): #setting the lower half of the matrix 1 (if we see 1 in the answer we will set a higher limit but typically the distance squared is less than 1)
for j in range (i+1):
sum[i][j]=1
t1=t2=t3=sum[0][0] #just finding the three min distances
Upvotes: 2
Views: 149
Reputation: 5281
change break
with continue
, if you want to skip to next ticket.
change:
except Exception, e:
print "This ticker does not belong to Yahoo. Skip to next ticker: ",
print e
break
to
except Exception, e:
print "This ticker does not belong to Yahoo. Skip to next ticker: ",
print e
continue
Upvotes: 2
Reputation: 6451
Indentation..
except Exception, e:
print "This ticker does not belong to Yahoo. Skip to next ticker: ",
print e
break
if len(f)==250:
Upvotes: 1