from IPython.display import HTML
from EIA_Util import *
import pandas as pd
import datetime as dt
import seaborn as sns
import matplotlib.pyplot as plt
eia = get_eia_db()
%matplotlib inline
HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
code_show = !code_show
}
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')
meta_spots, spots = eia.get_table(['PET.RWTC.D','PET.RBRTE.D'])
sns.set_style("darkgrid")
spots = spots.dropna(axis=0)
index_past_month = spots.index > (dt.datetime.now() - dt.timedelta(days=30))
index_past_quarter = spots.index > (dt.datetime.now() - dt.timedelta(days=90))
index_past_year = spots.index > (dt.datetime.now() - dt.timedelta(days=360))
index_past_10_years = spots.index > (dt.datetime.now() - dt.timedelta(days=10 * 360))
fig, axs = plt.subplots(2,2,figsize=(20, 18),sharey=False)
fig.subplots_adjust(hspace=0.2,wspace=0.2)
spots[index_past_month].plot(ax=axs[0][0],title='Past Month Spot Prices')
spots[index_past_quarter].plot(ax=axs[0][1],title='Past Quarter Spot Prices')
spots[index_past_year].plot(ax=axs[1][0],title='Past Year Spot Prices')
spots[index_past_10_years].plot(ax=axs[1][1],title='Past 10 Years Spot Prices')
for i in range(2):
for j in range(2):
axs[i][j].set_ylabel('$/bbl')
#spots['Cushing, OK WTI Spot Price FOB, Daily'].plot()
def brent_wti_spread(data):
brent = data['Europe Brent Spot Price FOB, Daily']
wti = data['Cushing, OK WTI Spot Price FOB, Daily']
return brent - wti
spread = spots.apply(brent_wti_spread,axis=1)
spread.name = 'Brent vs WTI Spread'
five_days_ma = pd.rolling_mean(spread,5)
one_month_ma = pd.rolling_mean(spread,30)
one_year_ma = pd.rolling_mean(spread,360)
spread = spread.to_frame()
spread['5 Days Moving Average'] = five_days_ma
spread['1 Month Moving Average'] = one_month_ma
spread['1 Year Moving Average'] = one_year_ma
fig, axs = plt.subplots(2,2,figsize=(20, 14),sharey=False)
spread[index_past_month][['Brent vs WTI Spread','5 Days Moving Average']].plot(ax=axs[0][0],color=['blue','red'])
spread[index_past_quarter][['Brent vs WTI Spread','1 Month Moving Average']].plot(ax=axs[0][1],color=['blue','red'])
spread[index_past_year][['Brent vs WTI Spread','1 Month Moving Average']].plot(ax=axs[1][0],color=['blue','red'])
spread[index_past_10_years][['Brent vs WTI Spread','1 Year Moving Average']].plot(ax=axs[1][1],color=['blue','red'])
for i in range(2):
for j in range(2):
axs[i][j].set_ylabel('$/bbl')
def brent_wti_relative_spread(data):
brent = data['Europe Brent Spot Price FOB, Daily']
wti = data['Cushing, OK WTI Spot Price FOB, Daily']
return 100 * (brent - wti)/wti
relative_spread = spots.apply(brent_wti_relative_spread,axis=1)
relative_spread.name = 'Brent vs WTI Spread'
five_days_ma = pd.rolling_mean(relative_spread,5)
one_month_ma = pd.rolling_mean(relative_spread,30)
one_year_ma = pd.rolling_mean(relative_spread,360)
relative_spread = relative_spread.to_frame()
relative_spread['5 Days Moving Average'] = five_days_ma
relative_spread['1 Month Moving Average'] = one_month_ma
relative_spread['1 Year Moving Average'] = one_year_ma
fig, axs = plt.subplots(2,2,figsize=(20, 14),sharey=False)
relative_spread[index_past_month][['Brent vs WTI Spread','5 Days Moving Average']].plot(ax=axs[0][0],color=['blue','red'])
relative_spread[index_past_quarter][['Brent vs WTI Spread','1 Month Moving Average']].plot(ax=axs[0][1],color=['blue','red'])
relative_spread[index_past_year][['Brent vs WTI Spread','1 Month Moving Average']].plot(ax=axs[1][0],color=['blue','red'])
relative_spread[index_past_10_years][['Brent vs WTI Spread','1 Year Moving Average']].plot(ax=axs[1][1],color=['blue','red'])
for i in range(2):
for j in range(2):
axs[i][j].set_ylabel('%')
meta_futures, futures = eia.get_table(['PET.RCLC1.M','PET.RCLC2.M','PET.RCLC3.M','PET.RCLC4.M'])
futures.index = futures.index.map(lambda d: pd.Period(d[:4] + "-" + d[-2:]))
t_futures = futures.copy().transpose()
t_futures = t_futures[[futures.index.max(),futures.index.max()-1,futures.index.max()-6,futures.index.max()-12]]
t_futures.index = ['1M','2M','3M','4M']
t_futures.index.name = 'Maturity'
t_futures.columns.name = 'Period'
fig = t_futures.plot(figsize=(10, 5))