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]:
In [7]:
dow.dropna(inplace=True)
In [8]:
dow.tail()
Out[8]:
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()
In [13]:
df.plot(subplots=True, figsize=(10, 10), sharex=True)
Out[13]:
In [14]:
df['19870102':].plot(subplots=True, figsize=(10, 10), sharex=True)
Out[14]:
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]:
In [23]:
df['2007':][['DJIA','DOW_WTI100']].plot(color=('b','r'), figsize=(10, 10))
Out[23]:
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]:
In [34]: