In [1]:
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
In [2]:
run_console = False
if run_console:
    %qtconsole
In [3]:
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
In [4]:
# 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'])
In [5]:
# dow['5/4/1970':].plot(figsize=(10, 10))
In [6]:
dow[np.isnan(dow['DJIA'])]
Out[6]:
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

In [7]:
dow.dropna(inplace=True)
In [8]:
dow.tail()
Out[8]:
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

In [9]:
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)'])
In [10]:
wti.columns = ['WTI']
wti.index.names = ['Date']
In [11]:
df = pd.DataFrame()
df = df.combineAdd(dow)
df = df.combineAdd(wti)
In [12]:
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)
In [13]:
df.plot(subplots=True, figsize=(10, 10), sharex=True)
Out[13]:
array([<matplotlib.axes.AxesSubplot object at 0x093B19B0>,
       <matplotlib.axes.AxesSubplot object at 0x0957FE30>], dtype=object)
In [14]:
df['19870102':].plot(subplots=True, figsize=(10, 10), sharex=True)
Out[14]:
array([<matplotlib.axes.AxesSubplot object at 0x093B1EB0>,
       <matplotlib.axes.AxesSubplot object at 0x096D52D0>], dtype=object)
In [15]:
# df.diff().plot(xlim=datetime(2006,01,02), figsize=(10, 10))
In [16]:
# df.pct_change().plot(subplots=True, xlim=datetime(2006,01,02), figsize=(10, 10), sharex=True)
In [17]:
# df.pct_change().plot(subplots=True, figsize=(10, 10), xlim=datetime(1986,01,02), sharex=True)
In [18]:
# pd.rolling_max(df,100).plot(subplots=True, figsize=(10, 10), xlim=datetime(1999,01,02), sharex=True)
In [19]:
# pd.rolling_max(df['WTI'],100).plot(figsize=(10, 10), xlim=datetime(1999,01,02))
In [20]:
# pd.rolling_min(df['DJIA'],100).plot(figsize=(10, 10), xlim=datetime(1999,01,02))
In [21]:
df['DOW_WTI30'] = df.DJIA[df.WTI>30]
df['DOW_WTI70'] = df.DJIA[df.WTI>70]
df['DOW_WTI100'] = df.DJIA[df.WTI>100]
In [22]:
df['1987':][df.columns.drop('WTI')].plot(figsize=(10, 10))
Out[22]:
<matplotlib.axes.AxesSubplot at 0xab35670>
In [23]:
df['2007':][['DJIA','DOW_WTI100']].plot(color=('b','r'), figsize=(10, 10))
Out[23]:
<matplotlib.axes.AxesSubplot at 0xad92670>
In [30]:
df1 = df.drop(['DOW_WTI30', 'DOW_WTI70', 'DOW_WTI100'],axis=1)
In [36]:
plt.figure()
df1['1995':].plot(secondary_y=['WTI'], figsize=(10, 10))
Out[36]:
<matplotlib.axes.AxesSubplot at 0xce15490>
<matplotlib.figure.Figure at 0xce0be50>
In [34]: