from datetime import datetime
import pandas as pd
import pandas.io.data
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
pd.set_option('max_columns', 50)
%matplotlib inline
run_console = False
if run_console:
%qtconsole
update_from_web = False
if update_from_web:
today = datetime.now()
# http://research.stlouisfed.org/fred2/series/DJIA/downloaddata
# http://measuringworth.com/DJA/
# http://www.eia.gov/dnav/pet/hist/LeafHandler.ashx?n=PET&s=RWTC&f=D
# http://www.eia.gov/dnav/pet/pet_pri_spt_s1_d.htm
# From http://measuringworth.com/DJA/
dow = pd.read_csv('DJA.csv', skiprows=3, index_col='Date', parse_dates=True, usecols=['Date','DJIA'])
# From http://research.stlouisfed.org/fred2/series/DJIA/downloaddata
# dow1 = pd.read_excel('DJIA.xls', 'DJIA', skiprows=23, index_col='DATE', parse_dates=True, usecols=['DATE','VALUE'])
# dow['5/4/1970':].plot(figsize=(10, 10))
dow[np.isnan(dow['DJIA'])]
DJIA | |
---|---|
Date | |
NaN | NaN |
Citation: Samuel H. Williamson, 'Daily Closing Value of the Dow Jones Average, 1885 to Present,' MeasuringWorth, 2012. | NaN |
2 rows × 1 columns
dow.dropna(inplace=True)
dow.tail()
DJIA | |
---|---|
Date | |
2/28/2014 | 16321.71 |
3/3/2014 | 16168.03 |
3/4/2014 | 16395.88 |
3/5/2014 | 16360.18 |
3/6/2014 | 16421.89 |
5 rows × 1 columns
wti = pd.read_csv('WTI.csv', skiprows=6, index_col='Day', parse_dates=True, usecols=['Day','Cushing OK WTI Spot Price FOB $/bbl'])
# From http://www.eia.gov/dnav/pet/pet_pri_spt_s1_d.htm
# wti1 = pd.read_excel('PET_PRI_SPT_S1_D.xls', 'Data 1', skiprows=2, index_col='Date', parse_dates=True, usecols=['Date','Cushing, OK WTI Spot Price FOB (Dollars per Barrel)'])
wti.columns = ['WTI']
wti.index.names = ['Date']
df = pd.DataFrame()
df = df.combineAdd(dow)
df = df.combineAdd(wti)
df.info()
<class 'pandas.core.frame.DataFrame'> DatetimeIndex: 35449 entries, 1885-05-04 00:00:00 to 2014-03-06 00:00:00 Data columns (total 2 columns): DJIA 35410 non-null float64 WTI 7100 non-null float64 dtypes: float64(2)
df.plot(subplots=True, figsize=(10, 10), sharex=True)
array([<matplotlib.axes.AxesSubplot object at 0x093B19B0>, <matplotlib.axes.AxesSubplot object at 0x0957FE30>], dtype=object)
df['19870102':].plot(subplots=True, figsize=(10, 10), sharex=True)
array([<matplotlib.axes.AxesSubplot object at 0x093B1EB0>, <matplotlib.axes.AxesSubplot object at 0x096D52D0>], dtype=object)
# df.diff().plot(xlim=datetime(2006,01,02), figsize=(10, 10))
# df.pct_change().plot(subplots=True, xlim=datetime(2006,01,02), figsize=(10, 10), sharex=True)
# df.pct_change().plot(subplots=True, figsize=(10, 10), xlim=datetime(1986,01,02), sharex=True)
# pd.rolling_max(df,100).plot(subplots=True, figsize=(10, 10), xlim=datetime(1999,01,02), sharex=True)
# pd.rolling_max(df['WTI'],100).plot(figsize=(10, 10), xlim=datetime(1999,01,02))
# pd.rolling_min(df['DJIA'],100).plot(figsize=(10, 10), xlim=datetime(1999,01,02))
df['DOW_WTI30'] = df.DJIA[df.WTI>30]
df['DOW_WTI70'] = df.DJIA[df.WTI>70]
df['DOW_WTI100'] = df.DJIA[df.WTI>100]
df['1987':][df.columns.drop('WTI')].plot(figsize=(10, 10))
<matplotlib.axes.AxesSubplot at 0xab35670>
df['2007':][['DJIA','DOW_WTI100']].plot(color=('b','r'), figsize=(10, 10))
<matplotlib.axes.AxesSubplot at 0xad92670>
df1 = df.drop(['DOW_WTI30', 'DOW_WTI70', 'DOW_WTI100'],axis=1)
plt.figure()
df1['1995':].plot(secondary_y=['WTI'], figsize=(10, 10))
<matplotlib.axes.AxesSubplot at 0xce15490>
<matplotlib.figure.Figure at 0xce0be50>