#### /statsmodels/sandbox/examples/thirdparty/findow_1.py

http://github.com/statsmodels/statsmodels
Python | 93 lines | 46 code | 21 blank | 26 comment | 6 complexity | 311b6bdccc5887251e5f95e9e8bf8214 MD5 | raw file
``` 1# -*- coding: utf-8 -*-
2"""A quick look at volatility of stock returns for 2009
3
4Just an exercise to find my way around the pandas methods.
5Shows the daily rate of return, the square of it (volatility) and
6a 5 day moving average of the volatility.
7No guarantee for correctness.
8Assumes no missing values.
9colors of lines in graphs are not great
10
11uses DataFrame and WidePanel to hold data downloaded from yahoo using matplotlib.
12I have not figured out storage, so the download happens at each run
13of the script.
14
15Created on Sat Jan 30 16:30:18 2010
16Author: josef-pktd
17"""
18import os
19
20from statsmodels.compat.python import lzip
21import numpy as np
22import matplotlib.finance as fin
23import matplotlib.pyplot as plt
24import datetime as dt
25
26import pandas as pd
27
28
29def getquotes(symbol, start, end):
30    # Taken from the no-longer-existent pandas.examples.finance
31    quotes = fin.quotes_historical_yahoo(symbol, start, end)
32    dates, open, close, high, low, volume = lzip(*quotes)
33
34    data = {
35        'open' : open,
36        'close' : close,
37        'high' : high,
38        'low' : low,
39        'volume' : volume
40    }
41
42    dates = pd.Index([dt.datetime.fromordinal(int(d)) for d in dates])
43    return pd.DataFrame(data, index=dates)
44
45
46start_date = dt.datetime(2007, 1, 1)
47end_date = dt.datetime(2009, 12, 31)
48
49dj30 = ['MMM', 'AA', 'AXP', 'T', 'BAC', 'BA', 'CAT', 'CVX', 'CSCO',
50       'KO', 'DD', 'XOM', 'GE', 'HPQ', 'HD', 'INTC', 'IBM', 'JNJ',
51       'JPM', 'KFT', 'MCD', 'MRK', 'MSFT', 'PFE', 'PG', 'TRV',
52       'UTX', 'VZ', 'WMT', 'DIS']
53mysym = ['msft', 'ibm', 'goog']
54indexsym = ['gspc', 'dji']
55
56
58dmall = {}
59for sy in dj30:
60    dmall[sy]  = getquotes(sy, start_date, end_date)
61
62# combine into WidePanel
63pawp = pd.WidePanel.fromDict(dmall)
64print(pawp.values.shape)
65
66# select closing prices
67paclose = pawp.getMinorXS('close')
68
69# take log and first difference over time
70paclose_ratereturn = paclose.apply(np.log).diff()
71
72if not os.path.exists('dj30rr'):
73    #if pandas is updated, then sometimes unpickling fails, and need to save again
74    paclose_ratereturn.save('dj30rr')
75
76plt.figure()
77paclose_ratereturn.plot()
78plt.title('daily rate of return')
79
80# square the returns
81paclose_ratereturn_vol = paclose_ratereturn.apply(lambda x:np.power(x,2))
82plt.figure()
83plt.title('volatility (with 5 day moving average')
84paclose_ratereturn_vol.plot()
85
86# use convolution to get moving average
87paclose_ratereturn_vol_mov = paclose_ratereturn_vol.apply(
88                        lambda x:np.convolve(x,np.ones(5)/5.,'same'))
89paclose_ratereturn_vol_mov.plot()
90
91
92
93#plt.show()
```