The Oil and Gold Decouple! What does it mean? Hands-On Market Analysis with Python
Introduction
The Oil and Gold Decouple! What does it mean? What happens when the price of oil moves in the opposite direction than gold? People say it's a bad omen when that happens.
Code
In [17]:
from IPython.display import Image
Image(filename='viralml-book.png')
Out[17]:
In [18]:
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.dates as mdates
from mpl_finance import candlestick_ohlc
mpl.style.use('default')
import io, base64, os, json, re
import pandas as pd
import numpy as np
import datetime
import warnings
warnings.filterwarnings('ignore')
In [19]:
path_to_market_data = '/Users/manuel/Documents/financial-research/market-report/2019-07-03/'
Load Data¶
In [20]:
# https://finance.yahoo.com/quote/%5EGSPC
sp500_df = pd.read_csv(path_to_market_data + '^GSPC.csv')
sp500_df['Date'] = pd.to_datetime(sp500_df['Date'])
sp500_df = sp500_df[['Date','Adj Close']]
sp500_df.columns = ['Date', 'SP500_Close']
print(np.min(sp500_df['Date'] ),np.max(sp500_df['Date'] ))
sp500_df = sp500_df.sort_values('Date', ascending=True) # sort in ascending date order
sp500_df.tail()
Out[20]:
In [21]:
# https://www.quandl.com/data/LBMA/GOLD-Gold-Price-London-Fixing
gold_df = pd.read_csv(path_to_market_data + 'LBMA-GOLD.csv')
gold_df['Date'] = pd.to_datetime(gold_df['Date'])
gold_df = gold_df[['Date', 'USD (PM)']]
gold_df.columns = ['Date', 'GLD']
gold_df['GLD'] = pd.to_numeric(gold_df['GLD'], errors='coerce')
print(np.min(gold_df['Date'] ),np.max(gold_df['Date'] ))
gold_df = gold_df.sort_values('Date', ascending=True) # sort in ascending date order
gold_df['GLD_pct_change'] = gold_df['GLD'].pct_change()
gold_df.tail()
Out[21]:
In [22]:
# Crude Oil Prices: West Texas Intermediate (WTI)
# https://fred.stlouisfed.org/series/DCOILWTICO
oil_df = pd.read_csv(path_to_market_data + 'DCOILWTICO.csv')
oil_df['Date'] = pd.to_datetime(oil_df['DATE'])
oil_df = oil_df[['Date', 'DCOILWTICO']]
oil_df.columns = ['Date', 'OIL']
oil_df['OIL'] = pd.to_numeric(oil_df['OIL'], errors='coerce')
print(np.min(oil_df['Date'] ),np.max(oil_df['Date'] ))
oil_df = oil_df.sort_values('Date', ascending=True) # sort in ascending date order
oil_df['OIL_pct_change'] = oil_df['OIL'].pct_change()
oil_df.tail()
Out[22]:
Visualize¶
In [23]:
gold_df_tmp = gold_df.copy()
oil_df_tmp = oil_df.copy()
sp500_df_tmp = sp500_df.copy()
cut_off_date = '1999-01-01'
gold_df_tmp = gold_df_tmp[gold_df_tmp['Date'] >= cut_off_date]
oil_df_tmp = oil_df_tmp[oil_df_tmp['Date'] >= cut_off_date]
sp500_df_tmp = sp500_df_tmp[sp500_df_tmp['Date'] >= cut_off_date]
fig, ax = plt.subplots(figsize=(16, 8))
plt.plot(gold_df_tmp['Date'], gold_df_tmp['GLD'], label='GLD', color='gold')
plt.legend(loc='lower left')
plt.title('OIL & GLD & SP500 - ' + str(np.max(gold_df_tmp['Date']))[0:10])
plt.grid()
# Get second axis
ax2 = ax.twinx()
plt.plot(oil_df_tmp['Date'], oil_df_tmp['OIL'], label='OIL',color='black')
plt.legend(loc='upper left')
# Get third axis
ax3 = ax.twinx()
plt.plot(sp500_df_tmp['Date'], sp500_df_tmp['SP500_Close'], label='SP500',color='blue')
plt.legend(loc='lower right')
plt.show()
Let's do a correlation on gold and oil to better understand the relationship¶
In [24]:
# join data sets together
# join both datasets together
together = pd.merge(gold_df, oil_df, on= ['Date'], how='inner')
# last valid observation forward
together = together.fillna(method='ffill')
# drop NAs
together = together.dropna(axis=0)
together.tail()
together['corr'] = together['GLD'].rolling(200).corr(together['OIL'].rolling(200))
together['corr'] = together['corr'].rolling(10).mean()
together.tail()
Out[24]:
In [25]:
together_tmp = together.copy()
sp500_df_tmp = sp500_df.copy()
together_tmp = together_tmp[together_tmp['Date'] >= cut_off_date]
sp500_df_tmp = sp500_df_tmp[sp500_df_tmp['Date'] >= cut_off_date]
fig, ax = plt.subplots(figsize=(16, 8))
plt.plot(together_tmp['Date'], together_tmp['corr'], label='GLD corr OIL', color='blue')
plt.legend(loc='lower left')
plt.title('GLD corr OIL & ' + str(np.max(together_tmp['Date']))[0:10])
plt.axhline(0, color='red', linewidth=3, linestyle='-.' )
plt.grid()
# Get second axis
ax2 = ax.twinx()
plt.plot(sp500_df_tmp['Date'], sp500_df_tmp['SP500_Close'], label='SP500',color='black')
plt.legend(loc='upper left')
Out[25]:
Show Notes
(pardon typos and formatting -these are the notes I use to make the videos)
The Oil and Gold Decouple! What does it mean? What happens when the price of oil moves in the opposite direction than gold? People say it's a bad omen when that happens.