forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinance.py
86 lines (58 loc) · 1.86 KB
/
finance.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
"""
Some examples playing around with yahoo finance data
"""
from datetime import datetime
from pandas.compat import zip
import matplotlib.finance as fin
import numpy as np
from pylab import show
from pandas import Index, DataFrame
from pandas.core.datetools import BMonthEnd
from pandas import ols
startDate = datetime(2008, 1, 1)
endDate = datetime(2009, 9, 1)
def getQuotes(symbol, start, end):
quotes = fin.quotes_historical_yahoo(symbol, start, end)
dates, open, close, high, low, volume = zip(*quotes)
data = {
'open': open,
'close': close,
'high': high,
'low': low,
'volume': volume
}
dates = Index([datetime.fromordinal(int(d)) for d in dates])
return DataFrame(data, index=dates)
msft = getQuotes('MSFT', startDate, endDate)
aapl = getQuotes('AAPL', startDate, endDate)
goog = getQuotes('GOOG', startDate, endDate)
ibm = getQuotes('IBM', startDate, endDate)
px = DataFrame({'MSFT': msft['close'],
'IBM': ibm['close'],
'GOOG': goog['close'],
'AAPL': aapl['close']})
returns = px / px.shift(1) - 1
# Select dates
subIndex = ibm.index[(ibm['close'] > 95) & (ibm['close'] < 100)]
msftOnSameDates = msft.reindex(subIndex)
# Insert columns
msft['hi-lo spread'] = msft['high'] - msft['low']
ibm['hi-lo spread'] = ibm['high'] - ibm['low']
# Aggregate monthly
def toMonthly(frame, how):
offset = BMonthEnd()
return frame.groupby(offset.rollforward).aggregate(how)
msftMonthly = toMonthly(msft, np.mean)
ibmMonthly = toMonthly(ibm, np.mean)
# Statistics
stdev = DataFrame({
'MSFT': msft.std(),
'IBM': ibm.std()
})
# Arithmetic
ratios = ibm / msft
# Works with different indices
ratio = ibm / ibmMonthly
monthlyRatio = ratio.reindex(ibmMonthly.index)
# Ratio relative to past month average
filledRatio = ibm / ibmMonthly.reindex(ibm.index, method='pad')