Showing posts with label google finance. Show all posts
Showing posts with label google finance. Show all posts

Wednesday, August 15, 2012

Historical Intraday Stock Price Data with Python

By popular request, this post will present how to acquire intraday stock data from google finance using python. The general structure of the code is pretty simple to understand. First a request url is created and sent. The response is then read by python to create an array or matrix of the financial data and a vector of time data. This array is created with the help of the popular Numpy package that can be downloaded from here. Then in one if-statement, the time data is then restructured into a proper unix time format and translated to a more familiar date string for each financial data point. The translated time vector is then joined with the financial array to produce a single easy to work with financial time series array. Since Numpy has been ported to Python 3, the code I wrote should be compatibile with both Python 2.X and 3.X. Here it is: import urllib2 import urllib import numpy as np from datetime import datetime urldata = {} urldata['q'] = ticker = 'JPM' # stock symbol urldata['x'] = 'NYSE' # exchange symbol urldata['i'] = '60' # interval urldata['p'] = '1d' # number of past trading days (max has been 15d) urldata['f'] = 'd,o,h,l,c,v' # requested data d is time, o is open, c is closing, h is high, l is low, v is volume url_values = urllib.urlencode(urldata) url = 'http://www.google.com/finance/getprices' full_url = url + '?' + url_values req = urllib2.Request(full_url) response = urllib2.urlopen(req).readlines() getdata = response del getdata[0:7] numberoflines = len(getdata) returnMat = np.zeros((numberoflines, 5)) timeVector = [] index = 0 for line in getdata: line = line.strip('a') listFromLine = line.split(',') returnMat[index,:] = listFromLine[1:6] timeVector.append(int(listFromLine[0])) index += 1 # convert Unix or epoch time to something more familiar for x in timeVector: if x > 500: z = x timeVector[timeVector.index(x)] = datetime.fromtimestamp(x) else: y = z+x*60 # multiply by interval timeVector[timeVector.index(x)] = datetime.fromtimestamp(y) tdata = np.array(timeVector) time = tdata.reshape((len(tdata),1)) intradata = np.concatenate((time, returnMat), axis=1) # array of all data with the properly formated times

Wednesday, July 11, 2012

Historical Minute-by-Minute Stock Prices in MATLAB

This is an extension of my first post on this blog. My first post went over how to gather the minute to minute stock price data from Google Finance using Mathematica. Using the same general programming structure, I've created a function to do this in MATLAB. 

The IntraDayStockData function can be obtained here. This function takes necessary inputs of a stock symbol and the name of the exchange the stock is traded in. This function also takes optional inputs of quote frequency (in seconds) and number of previous trading days. By default, the optional inputs are set for 1 minute quote intervals and 15 previous trading days. The output of this function are the time, the time in string format, trade volume, highest prices, lowest prices, and closing prices. Here is a quick example for getting the minute by minute quotes of the latest trading day (today) for JP Morgan Chase:

jpm = IntraDayStockData('JPM','NYSE','60','1d'); plot(jpm.date,jpm.close,'b-'); hold on; plot(jpm.date,jpm.high,'r-'); hold on; plot(jpm.date,jpm.low,'g-'); datetick('x',16);

Monday, April 18, 2011

Google Finance Backfill

During a 3am google search, I stumbled upon this page:

http://www.marketcalls.in/database/google-realtime-intraday-backfill-data.html

It seems that google finance keeps a 15 day minute by minute backfill of all stock quotes.  This was a great opportunity to use Mathematica to do some overly-complicated plotting at 3am.

The parameters to get the data is detailed on the site above.  For the minute by minute quote of JP Morgan for the last 15 days, the Market Calls website suggested using the following URL:

http://www.google.com/finance/getprices?q=JPM&x=NYSE&i=60&p=15d&f=d,c,h,l

The first task was to create a way to download the data for any stock of interest, which was easy enough with Mathematica.

Now the second task was a bit more complicated, since the backfill data kept time in a Unix format (counting the seconds since the birth of Unix).  The following snippet of code can convert this Unix time to a more familiar format.

Then all that is left to do, is to extract and format the price data.

Now it is time to plot!


When insomnia hits again, I may try my hand on some econometrics styled analysis with this data.  For those interested in playing with the code, you can download it here