Python for Finance: Analyze Big Financial Data

(Elle) #1
2014-02-21,37.90,37.96,37.22,37.29,12351900,37.29

2014-02-20,37.83,38.04,37.30,37.79,11155900,37.79

2014-02-19,38.06,38.33,37.68,37.81,15851900,37.81

2014-02-18,38.31,38.59,38.09,38.31,12096400,38.31

2014-02-14,38.43,38.45,38.11,38.23,9975800,38.23

2014-02-13,37.92,38.69,37.79,38.52,12088100,38.52

2014-02-12,38.60,38.91,38.03,38.11,14088500,38.11

2014-02-11,38.15,38.86,38.09,38.50,18348000,38.50

2014-02-10,38.00,38.13,37.25,37.76,17642900,37.76

2014-02-07,36.65,37.27,36.24,37.23,16178500,37.23

2014-02-06,35.65,36.75,35.61,36.24,14250000,36.24

2014-02-05,35.60,35.94,34.99,35.49,14022900,35.49

2014-02-04,35.11,35.86,34.86,35.66,21082500,35.66

2014-02-03,35.94,36.01,34.66,34.90,22195200,34.90

The library also provides convenience functions to customize URL strings. For example,


you might want to be able to parameterize the symbol to look up and the starting date. To


this end, define a new URL string with a string replacement part where you can insert the


parameters:


In  [ 34 ]: url =   ‘http://ichart.finance.yahoo.com/table.csv?g=d&ignore=.csv’
url += ‘&%s’ # for replacement with parameters
url += ‘&d=06&e=30&f=2014’

The function urlencode takes as an argument a Python dictionary with the parameter


names and the values to associate:


In  [ 35 ]: params  =   urllib.urlencode({‘s’:  ‘MSFT’, ‘a’:    ‘05’,   ‘b’:     1 ,    ‘c’:     2014 })

As result, there is a string object that can be inserted into the preceding URL string to


complete it:


In  [ 36 ]: params
Out[36]: ‘a=05&s=MSFT&b=1&c=2014’
In [ 37 ]: url % params
Out[37]: ‘http://ichart.finance.yahoo.com/table.csv?g=d&ignore=.csv&a=05&s=MSFT&
b=1&c=2014&d=06&e=30&f=2014’

Equipped with this new URL string, establish a connection and read the data from the


connection:


In  [ 38 ]: connect =   urllib.urlopen(url  %   params)
In [ 39 ]: data = connect.read()

The result again is stock price data, this time for more dates and for Microsoft:


In  [ 40 ]: print data
Out[40]: Date,Open,High,Low,Close,Volume,Adj Close
2014-07-30,44.07,44.10,43.29,43.58,31921400,43.31
2014-07-29,43.91,44.09,43.64,43.89,27763100,43.62
2014-07-28,44.36,44.51,43.93,43.97,29684200,43.70
2014-07-25,44.30,44.66,44.30,44.50,26737700,44.22
2014-07-24,44.93,45.00,44.32,44.40,30725300,44.12
2014-07-23,45.45,45.45,44.62,44.87,52362900,44.59
2014-07-22,45.00,45.15,44.59,44.83,43095800,44.55
2014-07-21,44.56,45.16,44.22,44.84,37604400,44.56
2014-07-18,44.65,44.84,44.25,44.69,43407500,44.41
2014-07-17,45.45,45.71,44.25,44.53,82180300,44.25
2014-07-16,42.51,44.31,42.48,44.08,63318000,43.81
2014-07-15,42.33,42.47,42.03,42.45,28748700,42.19
2014-07-14,42.22,42.45,42.04,42.14,21881100,41.88
2014-07-11,41.70,42.09,41.48,42.09,24083000,41.83
2014-07-10,41.37,42.00,41.05,41.69,21854700,41.43
2014-07-09,41.98,41.99,41.53,41.67,18445900,41.41
2014-07-08,41.87,42.00,41.61,41.78,31218200,41.52
2014-07-07,41.75,42.12,41.71,41.99,21952400,41.73
2014-07-03,41.91,41.99,41.56,41.80,15969300,41.54
2014-07-02,41.73,41.90,41.53,41.90,20208100,41.64
2014-07-01,41.86,42.15,41.69,41.87,26917000,41.61
2014-06-30,42.17,42.21,41.70,41.70,30805500,41.44
Free download pdf