wti oil
from quantopian.research.experimental import history
import pandas as pd
import matplotlib.pyplot as plt
m_list = ['F', 'G', 'H', 'J', 'K', 'M', 'N', 'Q', 'U', 'V', 'X', 'Z']
cont_list = []
for i in range(1, 21):
for m in m_list:
cont_list.append('CL'+m+str(i).zfill(2))
print(cont_list)
cl_contracts = symbols(cont_list)
# Pricing data for our consecutive contracts from earlier.
cl_close = history(
cl_contracts,
fields='close_price',
frequency='daily',
start='2002-01-01',
end='2020-12-31'
)
print(cl_close.columns)
df = pd.DataFrame()
for i in range(len(cl_close.columns)-1):
tmp_df = cl_close.iloc[:,i] - cl_close.iloc[:, i+1]
tmp_df = tmp_df.dropna()
if not tmp_df.empty:
tmp_df.plot()
df = df.append(pd.DataFrame(tmp_df))
plt.hist(df, bins = 48)
df.describe()
head_df = pd.DataFrame()
tail_df = pd.DataFrame()
for i in range(len(cl_close.columns)-1):
tmp_df = cl_close.iloc[:,i] - cl_close.iloc[:, i+1]
tmp_df = tmp_df.dropna()
head_df = df.append(pd.DataFrame(tmp_df.head(1)))
tail_df = df.append(pd.DataFrame(tail_df.head(1)))
plt.hist(head_df, alpha=0.5,color='blue', bins = 24)
plt.hist(tail_df, alpha=0.5, color='orange',bins = 24)
print(head_df.describe())
print(tail_df.describe())
VIX future
m_list = ['F', 'G', 'H', 'J', 'K', 'M', 'N', 'Q', 'U', 'V', 'X', 'Z']
cont_list = []
for i in range(2, 21):
for m in m_list:
cont_list.append('VX'+m+str(i).zfill(2))
print(cont_list)
vx_contracts = symbols(cont_list)
# Pricing data for our consecutive contracts from earlier.
vx_close = history(
vx_contracts,
fields='close_price',
frequency='daily',
start='2002-01-01',
end='2020-12-31'
)
vx_close.columns
df = pd.DataFrame()
for i in range(len(vx_close.columns)-1):
tmp_df = vx_close.iloc[:,i] - vx_close.iloc[:, i+1]
tmp_df = tmp_df.dropna()
if not tmp_df.empty:
tmp_df.plot()
df = df.append(pd.DataFrame(tmp_df))
plt.hist(df, bins = 24)
GOLD
m_list = ['F', 'G', 'H', 'J', 'K', 'M', 'N', 'Q', 'U', 'V', 'X', 'Z']
cont_list = []
for i in range(2, 20):
for m in m_list:
cont_list.append('GC'+m+str(i).zfill(2))
print(cont_list)
gc_contracts = symbols(cont_list)
# Pricing data for our consecutive contracts from earlier.
gc_close = history(
gc_contracts,
fields='close_price',
frequency='daily',
start='2002-01-01',
end='2020-12-31'
)
print(gc_close.columns)
df = pd.DataFrame()
for i in range(len(gc_close.columns)-1):
tmp_df = gc_close.iloc[:,i] - gc_close.iloc[:, i+1]
tmp_df = tmp_df.dropna()
if not tmp_df.empty:
tmp_df.plot()
df = df.append(pd.DataFrame(tmp_df))
plt.hist(df, bins = 24)
S&P500
m_list = ['F', 'G', 'H', 'J', 'K', 'M', 'N', 'Q', 'U', 'V', 'X', 'Z']
cont_list = []
for i in range(2, 21):
for m in m_list:
cont_list.append('SP'+m+str(i).zfill(2))
print(cont_list)
sp_contracts = symbols(cont_list)
# Pricing data for our consecutive contracts from earlier.
sp_close = history(
sp_contracts,
fields='close_price',
frequency='daily',
start='2002-01-01',
end='2020-12-31'
)
print(sp_close.columns)
df = pd.DataFrame()
for i in range(len(sp_close.columns)-1):
tmp_df = sp_close.iloc[:,i] - sp_close.iloc[:, i+1]
tmp_df = tmp_df.dropna()
if not tmp_df.empty:
tmp_df.plot()
df = df.append(pd.DataFrame(tmp_df))
plt.hist(df, bins = 24)
Tnote 10 year
m_list = ['F', 'G', 'H', 'J', 'K', 'M', 'N', 'Q', 'U', 'V', 'X', 'Z']
cont_list = []
for i in range(2, 21):
for m in m_list:
cont_list.append('TY'+m+str(i).zfill(2))
print(cont_list)
ty_contracts = symbols(cont_list)
# Pricing data for our consecutive contracts from earlier.
ty_close = history(
ty_contracts,
fields='close_price',
frequency='daily',
start='2002-01-01',
end='2020-12-31'
)
print(ty_close.columns)
df = pd.DataFrame()
for i in range(len(ty_close.columns)-1):
tmp_df = ty_close.iloc[:,i] - ty_close.iloc[:, i+1]
tmp_df = tmp_df.dropna()
if not tmp_df.empty:
tmp_df.plot()
df = df.append(pd.DataFrame(tmp_df))
plt.hist(df, bins = 64)