The code below is a good representation of my coding skills. I've been at it for about 6 months. What I am interested in is ways to make the following program faster. I currently have the urlrequests threaded to pull information for 500 stocks, but it is still too slow (around 30 seconds). What python techniques can I use to make this program more efficient?
Thanks!
from urllib.request import urlopen
from csv import reader
from threading import Thread
class StockQuote():
"""gets stock data from Yahoo Finance"""
def __init__(self, quote):
self.quote = quote
def lastPrice(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=l1'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def volume(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=v0'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def yearrange(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=w0'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def PEratio(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=r0'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def bookValue(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=b4'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def EBITDA(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=j4'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def PEGRatio(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=r5'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
def ticker(self):
url = 'http://finance.yahoo.com/d/quotes.csv?s={ticker}&f=s0'.format(ticker=self.quote)
return bytes.decode((urlopen(url).read().strip()))
class StockData(Thread):
def __init__(self, stk, fileobj):
Thread.__init__(self)
self.stk = stk
self.fileobj = fileobj
def run(self):
try:
stkObj = StockQuote(self.stk)
stkdata= {}
stkdata['Ticker'] = stkObj.ticker()
stkdata['Price'] = stkObj.lastPrice()
stkdata['PE Ratio'] = stkObj.PEratio()
stkdata['Volume'] = stkObj.volume()
stkdata['Year Range'] = stkObj.yearrange()
stkdata['Book Value per Share'] = stkObj.bookValue()
stkdata['EBITDA'] = stkObj.EBITDA()
stkdata['PEG Ratio'] = stkObj.PEGRatio()
except Exception:
pass
self.fileobj.write(str(stkdata))
def openSP500file():
SP500 = reader(open(r'C:\Users\test\Desktop\SP500.csv', 'r'), delimiter=',')
printlist = open(r'C:\Users\test\Desktop\SP500prices.txt', 'w')
for x in SP500:
indStk = x[0]
t1 = StockData(indStk, printlist)
t1.start()
def main():
openSP500file()
if __name__ == '__main__':
main()