PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

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