-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathportfolio_getyahoodata.py
103 lines (90 loc) · 3.7 KB
/
portfolio_getyahoodata.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# A Python module for retrieving stock data using the
# yahoo finance API
# requires the following installations:
#pip install cvxopt
#pip install numpy
#pip install scipy
#pip install pandas
#pip install patsy
#pip install statsmodels
import csv
import urllib2
from collections import defaultdict
import numpy
from statsmodels.stats.correlation_tools import cov_nearest
# Retrieves the stock quote for the given symbol
# from Yahoo Finance as a float.
# Input: symbol - stock symbol as a string
# Output: price - latest trade price from yahoo finance
def get_stock_quote(symbol):
BASE_URL = 'http://download.finance.yahoo.com/d/quotes.csv?s='
ID = symbol
close_prop = '&f=l1'
SUFFIX = '&e=.csv'
url = "%s%s%s%s" % (BASE_URL, ID, close_prop, SUFFIX)
price = float(urllib2.urlopen(url).read().strip())
return price
# Downloads the stock history for the given symbol,
# for the given date range, as a csv file.
# Input: symbol - stock symbol as a string
# start - start date in the form 'mm/dd/yyyy'
# end - end date in the form 'mm/dd/yyyy'
# outfile - output filename, e.g. 'out.csv'
# interval - trading interval; either d, w, m (daily, weekly, monthl7)
def csv_quote_history(symbol, start, end, outfile, interval='d'):
response = _quote_history(symbol, start, end, interval)
with open(outfile, 'wb') as f:
csv_reader = csv.reader(response)
csv_writer = csv.writer(f)
for row in csv_reader:
csv_writer.writerow(row)
# Gives the stock history for the given symbol,
# for the given date range, as a dictionary.
# Output: keys: ['High', 'Adj Close', 'Volume', 'Low', 'Date', 'Close', 'Open']
# values: list
def quote_history_dict(symbol, start, end, interval='m'):
history = defaultdict(lambda: [])
response = _quote_history(symbol, start, end, interval)
dreader = csv.DictReader(response)
for row in dreader:
for key in row.iterkeys():
history[key].insert(0, row[key])
return history
def _quote_history(symbol, start, end, interval):
BASE_URL = 'http://ichart.yahoo.com/table.csv?s='
ID = symbol
sm, sd, sy = start.split('/')
em, ed, ey = end.split('/')
url = "%s%s&a=%d&b=%d&c=%d&d=%d&e=%d&f=%d&g=%s" % (BASE_URL, ID, (int(sm)-1), int(sd), int(sy), (int(em)-1), int(ed), int(ey), interval)
response = urllib2.urlopen(url)
return response
def get_prices(symbol, start, end, interval='m'):
history = quote_history_dict(symbol, start, end, interval)
prices = map(lambda x: round(float(x),2), history['Close'])
prices[0] = round(float(history['Open'][0]),2)
return prices
def get_returns(symbol, start, end, interval='m'):
history = quote_history_dict(symbol, start, end, interval)
prices = map(lambda x: round(float(x),2), history['Close'])
prices[0] = round(float(history['Open'][0]),2)
returns = map(lambda (x, y): (y/x)-1, zip(prices[0:-1], prices[1:]))
return returns
def get_yr_returns(symbol, start, end):
history = quote_history_dict(symbol, start, end, 'm')
prices = map(lambda x: round(float(x),2), history['Close'])
prices[0] = round(float(history['Open'][0]),2)
prices.insert(0, prices[0])
returns = map(lambda (x, y): (y/x)-1, zip(prices[0::12][:-1], prices[12::12]))
return returns
def avg_return(symbol, start, end, interval='m'):
if interval=='y':
return numpy.mean(get_yr_returns(symbol, start, end))
else:
return numpy.mean(get_returns(symbol, start, end, interval))
def cov_matrix(symbols, start, end, interval='m'):
if interval=='y':
data = [numpy.array(get_yr_returns(s, start, end)) for s in symbols]
else:
data = [numpy.array(get_returns(s, start, end, interval)) for s in symbols]
x = numpy.array(data)
return cov_nearest(numpy.cov(x))