I created a program that takes stock tickers, crawls the web to find a CSV of each ticker's historical prices, and plots them using matplotlib. Almost everything is working fine, but I've run into a problem parsing the CSV to separate out each price.
The error I get is:
prices = [float(row[4]) for row in csv_rows]
IndexError: list index out of range
I get what the problem is here, I'm just not really sure how I should fix it.
(The issue is in the parseCSV()
method)
# Loop to chart multiple stocks
def chartStocks(*tickers):
for ticker in tickers:
chartStock(ticker)
# Single chart stock method
def chartStock(ticker):
url = "http://finance.yahoo.com/q/hp?s=" + str(ticker) + "+Historical+Prices"
sourceCode = requests.get(url)
plainText = sourceCode.text
soup = BeautifulSoup(plainText, "html.parser")
csv = findCSV(soup)
parseCSV(csv)
# Find the CSV URL
def findCSV(soupPage):
CSV_URL_PREFIX = 'http://real-chart.finance.yahoo.com/table.csv?s='
links = soupPage.findAll('a')
for link in links:
href = link.get('href', '')
if href.startswith(CSV_URL_PREFIX):
return href
# Parse CSV for daily prices
def parseCSV(csv_text):
csv_rows = csv.reader(csv_text.split('\n'))
prices = [float(row[4]) for row in csv_rows]
days = list(range(len(prices)))
point = collections.namedtuple('Point', ['x', 'y'])
for price in prices:
i = 0
p = point(days[i], prices[i])
points = []
points.append(p)
print(points)
plotStock(points)
# Plot the data
def plotStock(points):
plt.plot(points)
plt.show()