Pulling and Plotting Stock Data with Python
Sonce a long time I am interested in retrieving stock price data from the web and to play a little bit with the statistics. One first step is of course to pull the price data from an internet source.
For this undertaking I followed mainly the excellent Youtube videos of sentdex, wich you can find under this link.
The source of the free stock price data is Yahoo! Finance with the Yahoo! Finance API. Further ingredients are a working Python installation and the Python module urllib2.
Below is the code for pulling the data and saving it in a text file on the example of Royal Dutch Shell (RDSA.AS).
import urllib2
import time
stockToPull = 'BG.L'
timeRange = '3y'
def pullData(stock):
try:
fileLine = stock+'_'+timeRange+'.txt'
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range='+timeRange+'/csv'
sourceCode = urllib2.urlopen(urlToVisit).read()
splitSource = sourceCode.split('\n')
for eachLine in splitSource:
splitLine = eachLine.split(',')
if len(splitLine)==6:
if 'values' not in eachLine:
saveFile = open(fileLine,'a')
lineToWrite = eachLine+'\n'
saveFile.write(lineToWrite)
print 'Pulled',stock
print 'sleeping'
time.sleep(1)
except Exception,e:
print 'main loop', str(e)
pullData(stockToPull)
I take no credit for this code, since it is really just copied and a little modified from the Youtube videos mentioned ealier. So far I have followed the series up to video number 12 and this includes the plotting of the data. Since the code is a bit too long to post here, I will just show the plot of Royal Dutch Shell on a scale of the last 3 years.
In addition, the next plot is showing the price of BG group plc. in GBP. BG group plc. has recently been acquired by by Royal Dutch Shell. The announcement was on the 8th of April 2015.
Feb. 25, 2021, 5:55 p.m. by Janette