Menu

[r6030]: / trunk / py4science / examples / stock_demo.py  Maximize  Restore  History

Download this file

42 lines (29 with data), 999 Bytes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""Simple data access and manipulation demo, using Yahoo Finance data."""
# <demo> stop
# Needed libraries
import urllib
import matplotlib.mlab as mlab # contains csv2rec
import pylab as p
# <demo> stop
# Choose a stock, make the URL for it and download it to a local file
ticker = 'CROX' # Boulder-based company Crocs
url = 'http://ichart.finance.yahoo.com/table.csv?' +\
's=%s&d=9&e=20&f=2007&g=d&a=0&b=29&c=1993&ignore=.csv'%ticker
fname = '%s.csv'% ticker
urllib.urlretrieve(url, fname)
# <demo> stop
# Now, make a Record Array out of this dataset:
r = mlab.csv2rec(fname)
# note that the CSV file is sorted most recent date first, so you will probably
# want to sort the record array so most recent date is last
r.sort()
# A quick look at the data structure:
print 'dtype:',r.dtype
print 'shape:',r.shape
# <demo> stop
p.plot(r.date,r.adj_close)
p.show()
# <demo> stop
# Now, make a slightly modified version of the file with cleaner formatting.
# We'll use this later...
#
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.