In this post I am going to analyze returns, cumulative returns and Sharpe ratio for different assets such as : Bitcoin, different currencies, Interest Rates, Bonds, Stock indexes, futures over Stock indexes and Commodites. At the end I will calculate a rolling correlation between Bitcoin and Nasdaq 100.
BITCOIN AND ASSET CLASSES COMPARISON SINCE 2017-01-01¶
In [1]:
# In this notebook I am going to analyze and compare the return, cumulative returns, sharpe ratio of different assets such as:
# Currencies, Interest rates, Bonds, Stock Indexes, Futures over Stock Indexes and Commodities. I will also study their
# correlations and rolling correlations since 2017-01-01 till now.
#
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
In [2]:
import quandl
BITCOIN AND CURRENCIES COMPARISON¶
In [3]:
# CURRENCIES
# Bitcoin Exchange Rate
# BITCOIN/USD Exchange Rate
quandl.ApiConfig.api_key = ""
bitcoin_usd = quandl.get("BITSTAMP/USD", start_date="2017-01-01", end_date="2019-01-24")
bitcoin_usd['returns'] = bitcoin_usd['High'].pct_change(1)
bitcoin_usd['MA50'] = bitcoin_usd['High'].rolling(50).mean()
bitcoin_usd['MA200'] = bitcoin_usd['High'].rolling(200).mean()
bitcoin_usd['Total Traded'] = bitcoin_usd['High']*bitcoin_usd['Volume']
bitcoin_usd['Cumulative Return'] = (1 + bitcoin_usd['returns']).cumprod()
bitcoin_usd[['High','MA50','MA200']].plot(figsize=(12,6), label='Bitcoin-USD',title='Bitcoin-USD Exchange rate',legend='Bitcoin-USD')
plt.legend(['Bitcoin','MA50','MA200']);
#bitcoin_usd.head(5)
In [4]:
# eur_usd
# SPOT EXCHANGE RATE - EURO AREA, Business day
eur_usd = quandl.get("FED/RXI_US_N_B_EU", start_date="2017-01-01", end_date="2019-01-24")
eur_usd['eur_usd']=eur_usd['Value']
eur_usd['returns'] = eur_usd['eur_usd'].pct_change(1)
eur_usd['MA50'] = eur_usd['eur_usd'].rolling(50).mean()
eur_usd['MA200'] = eur_usd['eur_usd'].rolling(200).mean()
eur_usd['Cumulative Return'] = (1 + eur_usd['returns']).cumprod()
eur_usd[['eur_usd','MA50','MA200']].plot(figsize=(12,6),label='eur_usd',title='Eur/Usd Exchange rate', legend='eur_usd');
#eur_usd.head(5)
In [5]:
#JAPAN -- SPOT EXCHANGE RATE, YEN/US$, Business day
jpy_usd=quandl.get("FED/RXI_N_B_JA", start_date="2017-01-01", end_date="2019-01-24")
jpy_usd['jpy_usd']=1/jpy_usd['Value']
jpy_usd['returns'] = jpy_usd['jpy_usd'].pct_change(1)
jpy_usd['MA50'] = jpy_usd['jpy_usd'].rolling(50).mean()
jpy_usd['MA200'] = jpy_usd['jpy_usd'].rolling(200).mean()
jpy_usd['Cumulative Return'] = (1 + jpy_usd['returns']).cumprod()
jpy_usd[['jpy_usd','MA50','MA200']].plot(figsize=(12,6),label='jpy_usd',title='JPY / USD Exchange rate', legend='JPY_USD');
#jpy_usd.head(5)
In [6]:
# CHINA -- SPOT EXCHANGE RATE, YUAN/US$ P.R., Business day
yuan_usd=quandl.get("FED/RXI_N_B_CH", start_date="2017-01-01", end_date="2019-01-24")
yuan_usd['yuan_usd']=1/yuan_usd['Value']
yuan_usd['returns'] = yuan_usd['yuan_usd'].pct_change(1)
yuan_usd['MA50'] = yuan_usd['yuan_usd'].rolling(50).mean()
yuan_usd['MA200'] = yuan_usd['yuan_usd'].rolling(200).mean()
yuan_usd['Cumulative Return'] = (1 + yuan_usd['returns']).cumprod()
yuan_usd[['yuan_usd','MA50','MA200']].plot(figsize=(12,6),label='yuan_usd',
title='YUAN / USD Exchange rate', legend='yuan_usd');
#yuan_usd.head(5)
In [7]:
# Comparing cumulative returns on Yuan, Euro, JPY, Bitcoin since 2017
bitcoin_usd['Cumulative Return'].plot(label='bitcoin_usd',figsize=(16,8),title='Cumulative Return')
eur_usd['Cumulative Return'].plot(label='eur_usd')
yuan_usd['Cumulative Return'].plot(label='yuan_usd')
jpy_usd['Cumulative Return'].plot(label='jpy_usd');
#plt.legend()
# It is quite evident that return in Bitcoin is higher. Later on we will check risk and sharpe ratio
# Even now, it is difficult to consider Bitcoin in this asset class.
In [8]:
# Comparing cumulative returns on Yuan, Euro, JPY since 2017 (leaving out Bitcoin)
eur_usd['Cumulative Return'].plot(label='eur_usd',figsize=(16,8),title='Cumulative Return')
yuan_usd['Cumulative Return'].plot(label='yuan_usd')
jpy_usd['Cumulative Return'].plot(label='jpy_usd');
#plt.legend()
# USD has become weaker and EUR has become stronger
In [9]:
#lets take a look to the correlations between the currency daily returns
#bitcoin_usd[['returns']]
#eur_usd[['returns']]
#'yuan_usd'[['returns']]
#jpy_usd[['returns']]
currency=pd.concat([bitcoin_usd[['returns']],eur_usd[['returns']],yuan_usd[['returns']],jpy_usd[['returns']]],axis=1)
currency.columns=['bitcoin_usd', 'eur_usd', 'yuan_usd','jpy_usd']
plt.figure(figsize=(16,8))
sns.heatmap(currency.corr(),cmap='coolwarm',annot=True);
# Bitcoin shows very little correlation with the rest of the currencies.
#It seems that it does not belong to this asset class
In [10]:
#Lets calculate annual volatitity
currency_std=currency.std()*(250^(1/2))
currency_std
Out[10]:
In [11]:
#lets calculate average return annual
currency_return=currency.mean()*250
currency_return
Out[11]:
In [12]:
# lets calculate Sharpe Ratio (We should have used geometric return instead
# aritmetic. We will do this as an approximation)
# I will assume 1% as Risk free rate
Rf=1/100
currency_sharpe_ratio= (currency_return-Rf)/currency_std
currency_sharpe_ratio
#bitcoin has higher risk adjusted return that other currencies
Out[12]:
INTEREST RATES AND BONDS COMPARISON¶
In [13]:
# INTEREST RATES
# Euribor 3 months
Euribor_3_M=quandl.get("BOF/QS_D_IEUTIO3M", start_date="2017-01-01", end_date="2019-01-24")
Euribor_3_M['returns'] = Euribor_3_M['Value'].pct_change(1)
Euribor_3_M['MA50'] = Euribor_3_M['Value'].rolling(50).mean()
Euribor_3_M['MA200'] = Euribor_3_M['Value'].rolling(200).mean()
Euribor_3_M['Cumulative Return'] = (1 + Euribor_3_M['returns']).cumprod()
Euribor_3_M[['Value','MA50','MA200']].plot(figsize=(12,6),label='Euribor_3_M'
,title='Euribor_3_M', legend='Euribor_3_M');
plt.legend(['Euribor_3_M','MA50','MA200']);
#Euribor_3_M.head(5)
In [ ]:
In [14]:
# Euribor 12 months
Euribor_12_M=quandl.get("BOF/QS_D_IEUTIO1A", start_date="2017-01-01", end_date="2019-01-24")
Euribor_12_M['returns'] = Euribor_12_M['Value'].pct_change(1)
Euribor_12_M['MA50'] = Euribor_12_M['Value'].rolling(50).mean()
Euribor_12_M['MA200'] = Euribor_12_M['Value'].rolling(200).mean()
Euribor_12_M['Cumulative Return'] = (1 + Euribor_12_M['returns']).cumprod()
Euribor_12_M[['Value','MA50','MA200']].plot(figsize=(12,6),label='Euribor_12_M'
,title='Euribor_12_M', legend='Euribor_12_M')
plt.legend(['Euribor_12_M','MA50','MA200']);
#Euribor_12_M.head(5)
In [15]:
# US Treasury Bill Rates 13 WK
#Treasury Bill Rates 13 WK
Treasury_Bill_rates_13WK=quandl.get("USTREASURY/BILLRATES", start_date="2017-01-01", end_date="2019-01-24")
Treasury_Bill_rates_13WK = Treasury_Bill_rates_13WK[(Treasury_Bill_rates_13WK[['13 Wk Bank Discount Rate']] != 0).all(axis=1)]
Treasury_Bill_rates_13WK['returns_13WK'] = Treasury_Bill_rates_13WK['13 Wk Bank Discount Rate'].pct_change(1)
Treasury_Bill_rates_13WK['MA50'] = Treasury_Bill_rates_13WK['13 Wk Bank Discount Rate'].rolling(50).mean()
Treasury_Bill_rates_13WK['MA200'] = Treasury_Bill_rates_13WK['13 Wk Bank Discount Rate'].rolling(200).mean()
Treasury_Bill_rates_13WK['Cumulative Return_13WK'] = (1 + Treasury_Bill_rates_13WK['returns_13WK']).cumprod()
Treasury_Bill_rates_13WK[['13 Wk Bank Discount Rate','MA50','MA200']].plot(figsize=(12,6),label='T_BILL_13WK'
,title='T_BILL_13WK', legend='T_BILL_13WK')
plt.legend(['T_BILL_13WK','MA50','MA200']);
#Treasury_Bill_rates_13WK.head(5)
In [16]:
# US Treasury Bill Rates 52 WK
#Treasury Bill Rates
Treasury_Bill_rates_52WK=quandl.get("USTREASURY/BILLRATES", start_date="2017-01-01", end_date="2019-01-24")
Treasury_Bill_rates_52WK = Treasury_Bill_rates_52WK[(Treasury_Bill_rates_52WK[['52 Wk Bank Discount Rate']] != 0).all(axis=1)]
Treasury_Bill_rates_52WK['returns_52WK'] = Treasury_Bill_rates_13WK['52 Wk Bank Discount Rate'].pct_change(1)
Treasury_Bill_rates_52WK['MA50'] = Treasury_Bill_rates_52WK['52 Wk Bank Discount Rate'].rolling(50).mean()
Treasury_Bill_rates_52WK['MA200'] = Treasury_Bill_rates_52WK['52 Wk Bank Discount Rate'].rolling(200).mean()
Treasury_Bill_rates_52WK['Cumulative Return_52WK'] = (1 + Treasury_Bill_rates_52WK['returns_52WK']).cumprod()
Treasury_Bill_rates_52WK[['52 Wk Bank Discount Rate','MA50','MA200']].plot(figsize=(12,6),label='T_BILL_52WK'
,title='T_BILL_52WK', legend='T_BILL_52WK')
plt.legend(['T_BILL_52WK','MA50','MA200']);
#Treasury_Bill_rates_52WK.head(5)
In [17]:
#BONDS
#Federal 10 years bond Germany
ten_y_bond_DE=quandl.get("BUNDESBANK/BBK01_WT1010", start_date="2017-01-01", end_date="2019-01-24")
ten_y_bond_DE = ten_y_bond_DE[(ten_y_bond_DE[['Value']] != 0).all(axis=1)]
ten_y_bond_DE['returns'] = ten_y_bond_DE['Value'].pct_change(1)
ten_y_bond_DE['MA50'] = ten_y_bond_DE['Value'].rolling(50).mean()
ten_y_bond_DE['MA200'] = ten_y_bond_DE['Value'].rolling(200).mean()
ten_y_bond_DE['Cumulative Return'] = (1 + ten_y_bond_DE['returns']).cumprod()
ten_y_bond_DE[['Value','MA50','MA200']].plot(figsize=(12,6),label='10y_bond_DE'
,title='10y_bond_DE', legend='10y_bond_DE')
plt.legend(['10y_bond_DE','MA50','MA200']);
#ten_y_bond_DE.head(5)
In [18]:
# Market yield on U.S. Treasury securities at 10-year constant maturity, quoted on investment basis
ten_y_bond_US=quandl.get("FED/RIFLGFCY10_N_B", start_date="2017-01-01", end_date="2019-01-24")
ten_y_bond_US = ten_y_bond_US[(ten_y_bond_US[['Value']] != 0).all(axis=1)]
ten_y_bond_US['returns'] = ten_y_bond_US['Value'].pct_change(1)
ten_y_bond_US['MA50'] = ten_y_bond_US['Value'].rolling(50).mean()
ten_y_bond_US['MA200'] = ten_y_bond_US['Value'].rolling(200).mean()
ten_y_bond_US['Cumulative Return'] = (1 + ten_y_bond_US['returns']).cumprod()
ten_y_bond_US[['Value','MA50','MA200']].plot(figsize=(12,6),label='10y_bond_US'
,title='10y_bond_US', legend='10y_bond_US')
plt.legend(['10y_bond_US','MA50','MA200']);
#ten_y_bond_US.head(5)
In [19]:
# Lets compare bitcoin return against interest and bonds
Euribor_12_M['Cumulative Return'].plot(label='Euribor_12_M',figsize=(16,8),title='Cumulative Return')
Euribor_3_M['Cumulative Return'].plot(label='Euribor_3_M')
Treasury_Bill_rates_13WK['Cumulative Return_13WK'].plot(label='Treasury_Bill_rates_13WK')
Treasury_Bill_rates_52WK['Cumulative Return_52WK'].plot(label='Treasury_Bill_rates_52WK')
ten_y_bond_DE['Cumulative Return'].plot(label='ten_y_bond_DE')
ten_y_bond_US['Cumulative Return'].plot(label='ten_y_bond_US')
bitcoin_usd['Cumulative Return'].plot(label='bitcoin_usd');
plt.legend();
# the cumulative returns of Bitcoin are much higher
In [20]:
# Lets compare interest and bonds
Euribor_12_M['Cumulative Return'].plot(label='Euribor_12_M',figsize=(16,8),title='Cumulative Return')
Euribor_3_M['Cumulative Return'].plot(label='Euribor_3_M')
Treasury_Bill_rates_13WK['Cumulative Return_13WK'].plot(label='Treasury_Bill_rates_13WK')
Treasury_Bill_rates_52WK['Cumulative Return_52WK'].plot(label='Treasury_Bill_rates_52WK')
ten_y_bond_DE['Cumulative Return'].plot(label='ten_y_bond_DE')
ten_y_bond_US['Cumulative Return'].plot(label='ten_y_bond_US');
plt.legend();
# the cumulative returns of Bitcoin are much higher
In [21]:
#lets take a look to the correlations between the bitcoin interest and bonnds
interest=pd.concat([bitcoin_usd[['returns']],ten_y_bond_US[['returns']],ten_y_bond_DE[['returns']]
,Treasury_Bill_rates_52WK[['returns_52WK']],Treasury_Bill_rates_13WK[['returns_13WK']]
,Euribor_3_M[['returns']],Euribor_12_M[['returns']]],axis=1)
interest.columns=['bitcoin_usd', 'ten_y_bond_US', 'ten_y_bond_DE','T_bill_52w','T_bill_13w','Euribor_3M','Euribor_12M']
plt.figure(figsize=(16,8))
sns.heatmap(interest.corr(),cmap='coolwarm',annot=True);
# Bitcoin shows very little correlation with the rest of the assets.
# It seems that it does not belong to this asset class
In [ ]:
BITCOIN AND STOCK INDEXES COMPARISON¶
In [22]:
# LET´S COMPARE NOW BITCOIN WITH SOME INDEXES : Nasdaq-100, DAX (futures), STOXX Euro 50 (futures), Hang Seng (futures)
# Indexes
#Nasdaq Index NASDAQ-100 (XQO)
Nasdaq100 = quandl.get("NASDAQOMX/XQO", start_date="2016-05-24", end_date="2019-01-24")
Nasdaq100['returns'] = Nasdaq100['Index Value'].pct_change(1)
Nasdaq100['MA50'] = Nasdaq100['Index Value'].rolling(50).mean()
Nasdaq100['MA200'] = Nasdaq100['Index Value'].rolling(200).mean()
Nasdaq100['Cumulative Return'] = (1 + Nasdaq100['returns']).cumprod()
Nasdaq100[['Index Value','MA50','MA200']].plot(figsize=(12,6), label='Nasdaq100',
title='Nasdaq100',legend='Nasdaq100');
#Nasdaq100.head(5)
In [23]:
#DAX Futures, Continuous Contract #2 (FDAX2)
Dax = quandl.get("CHRIS/EUREX_FDAX2", start_date="2016-05-24", end_date="2019-01-24")
Dax = Dax[(Dax[['High']] != 0).all(axis=1)]
Dax['returns'] = Dax['High'].pct_change(1)
Dax['MA50'] = Dax['High'].rolling(50).mean()
Dax['MA200'] = Dax['High'].rolling(200).mean()
Dax['Total Traded'] = Dax['High']*Dax['Volume']
Dax['Cumulative Return'] = (1 + Dax['returns']).cumprod()
Dax[['High','MA50','MA200']].plot(figsize=(12,6),
label='Dax',
title='Dax',
legend='Dax');
#Dax.head(5)
In [24]:
# Historical Futures Prices: STOXX Europe 50 Index Futures, Continuous Contract #1
stoxx_50 = quandl.get("CHRIS/EUREX_FSTX1", start_date="2016-05-24", end_date="2019-01-24")
stoxx_50 = stoxx_50[(stoxx_50[['High']] != 0).all(axis=1)]
stoxx_50['returns'] = stoxx_50['High'].pct_change(1)
stoxx_50['MA50'] = stoxx_50['High'].rolling(50).mean()
stoxx_50['MA200'] = stoxx_50['High'].rolling(200).mean()
stoxx_50['Total Traded'] = stoxx_50['High']*bitcoin_usd['Volume']
stoxx_50['Cumulative Return'] = (1 + stoxx_50['returns']).cumprod()
stoxx_50[['High','MA50','MA200']].plot(figsize=(12,6), label='stoxx_50',
title='stoxx_50',
legend='stoxx_50');
#stoxx_50.head(5)
In [25]:
#Hang Seng Index Futures, Continuous Contract #2 (HSI2)
hang_seng = quandl.get("CHRIS/HKEX_HSI2", start_date="2016-05-24", end_date="2019-01-24")
hang_seng['returns'] = hang_seng['High'].pct_change(1)
hang_seng['MA50'] = hang_seng['High'].rolling(50).mean()
hang_seng['MA200'] = hang_seng['High'].rolling(200).mean()
hang_seng['Total Traded'] = hang_seng['High']*hang_seng['Volume']
hang_seng['Cumulative Return'] = (1 + hang_seng['returns']).cumprod()
hang_seng[['High','MA50','MA200']].plot(figsize=(12,6), label='hang_seng',
title='hang_seng',
legend='hang_seng');
#hang_seng.head(5)
In [26]:
# Lets compare bitcoin return against indexes
Nasdaq100['Cumulative Return'].plot(label='Nasdaq100',figsize=(16,8),title='Cumulative Return')
Dax['Cumulative Return'].plot(label='Dax')
stoxx_50['Cumulative Return'].plot(label='stoxx_50')
hang_seng['Cumulative Return'].plot(label='hang_seng')
bitcoin_usd['Cumulative Return'].plot(label='bitcoin_usd');
plt.legend()
# the cumulative returns of Bitcoin are much higher
Out[26]:
In [27]:
# Lets compare the indexes without bitcoin
Nasdaq100['Cumulative Return'].plot(label='Nasdaq100',figsize=(16,8),title='Cumulative Return')
Dax['Cumulative Return'].plot(label='Dax')
stoxx_50['Cumulative Return'].plot(label='stoxx_50')
hang_seng['Cumulative Return'].plot(label='hang_seng');
plt.legend()
# the cumulative returns of Bitcoin are much higher
Out[27]:
In [28]:
#lets take a look to the correlations between the indexes and bitcoin
#bitcoin_usd[['returns']]
#Nasdaq100[['returns']]
#'Dax'[['returns']]
#stoxx_50[['returns']]
#
indexes=pd.concat([bitcoin_usd[['returns']],Nasdaq100[['returns']],Dax[['returns']],stoxx_50[['returns']],hang_seng[['returns']]],axis=1)
indexes.columns=['bitcoin_usd', 'Nasdaq100', 'Dax','stoxx_50','hang_seng']
plt.figure(figsize=(16,8))
sns.heatmap(indexes.corr(),cmap='coolwarm',annot=True);
# Bitcoin shows very little correlation with the rest of the currencies.
# It seems that it does not belong to this asset class
In [29]:
#Lets calculate annual volatitity
indexes_std=indexes.std()*(250^(1/2))
indexes_std
Out[29]:
In [30]:
#Lets calculate annual return
indexes_return=indexes.mean()*(250)
indexes_return
Out[30]:
In [31]:
# lets calculate Sharpe Ratio (We should have use geometric return instead
# aritmetic. We will do this as an approximation)
# I will assume 1% as Risk free rate
Rf=1/100
indexes_sharpe_ratio= (indexes_return-Rf)/indexes_std
indexes_sharpe_ratio
#bitcoin has similar risk adjusted return to Nasdaq100, but it is not correlated with this asset class.
Out[31]:
BITCOIN AND COMMODITIES COMPARISON¶
In [32]:
# COMMODITIES
# Copper prices
# Copper Futures, Continuous Contract #10 (HG10)
Copper = quandl.get("CHRIS/CME_HG10", start_date="2016-05-24", end_date="2019-01-24")
Copper['returns'] = Copper['High'].pct_change(1)
Copper['MA50'] = Copper['High'].rolling(50).mean()
Copper['MA200'] = Copper['High'].rolling(200).mean()
Copper['Total Traded'] = Copper['High']*Copper['Volume']
Copper['Cumulative Return'] = (1 + Copper['returns']).cumprod()
Copper[['High','MA50','MA200']].plot(figsize=(12,6), label='Copper',
title='Copper',
legend='Copper')
plt.legend(['Copper','MA50','MA200']);
#Copper.head(5)
In [33]:
# Zinc
Zinc = quandl.get("CHRIS/MCX_ZN2", start_date="2016-05-24", end_date="2019-01-24")
Zinc = Zinc[(Zinc[['High']] != 0).all(axis=1)]
Zinc['returns'] = Zinc['High'].pct_change(1)
Zinc['MA50'] = Zinc['High'].rolling(50).mean()
Zinc['MA200'] = Zinc['High'].rolling(200).mean()
Zinc['Total Traded'] = Zinc['High']*Zinc['Volume']
Zinc['Cumulative Return'] = (1 + Zinc['returns']).cumprod()
Zinc[['High','MA50','MA200']].plot(figsize=(12,6), label='Zinc',
title='Zinc',
legend='Zinc');
plt.legend(['Zinc','MA50','MA200']);
#Zinc.head(5)
In [34]:
#Coffee C Futures, Continuous Contract
Coffee = quandl.get("CHRIS/ICE_KC5", start_date="2016-05-24", end_date="2019-01-24")
Coffee['returns'] = Coffee['High'].pct_change(1)
Coffee['MA50'] = Coffee['High'].rolling(50).mean()
Coffee['MA200'] = Coffee['High'].rolling(200).mean()
Coffee['Total Traded'] = Coffee['High']*Coffee['Volume']
Coffee['Cumulative Return'] = (1 + Coffee['returns']).cumprod()
Coffee[['High','MA50','MA200']].plot(figsize=(12,6), label='Coffee',
title='Coffee',
legend='Coffee')
plt.legend(['Coffee','MA50','MA200']);
#Coffee.head(5)
In [35]:
#Corn Futures, Continuous Contract #4 (C4)
Corn = quandl.get("CHRIS/CME_C4", start_date="2016-05-24", end_date="2019-01-24")
#Zinc = Zinc[(Zinc[['High']] != 0).all(axis=1)]
Corn['returns'] = Corn['High'].pct_change(1)
Corn['MA50'] = Corn['High'].rolling(50).mean()
Corn['MA200'] = Corn['High'].rolling(200).mean()
Corn['Total Traded'] = Corn['High']*Corn['Volume']
Corn['Cumulative Return'] = (1 + Corn['returns']).cumprod()
Corn[['High','MA50','MA200']].plot(figsize=(12,6), label='Corn',
title='Corn',
legend='Corn')
plt.legend(['Corn','MA50','MA200']);
#Corn.head(5)
In [36]:
#Lean Hog Futures, Continuous Contract #2 (LN2)
Lean_Hogs=quandl.get("CHRIS/CME_LN2", start_date="2016-05-24", end_date="2019-01-24")
Lean_Hogs['returns'] = Lean_Hogs['High'].pct_change(1)
Lean_Hogs['MA50'] = Lean_Hogs['High'].rolling(50).mean()
Lean_Hogs['MA200'] = Lean_Hogs['High'].rolling(200).mean()
Lean_Hogs['Total Traded'] = Lean_Hogs['High']*Corn['Volume']
Lean_Hogs['Cumulative Return'] = (1 + Lean_Hogs['returns']).cumprod()
Lean_Hogs[['High','MA50','MA200']].plot(figsize=(12,6), label='Lean_Hogs',
title='Lean_Hogs',
legend='Lean_Hogs')
plt.legend(['Lean_Hogs','MA50','MA200']);
#Lean_Hogs.head(5)
In [37]:
# Commodities
# Gold Price: London Fixing
# Gold prices
# London Fixings, London Bullion Market Association (LBMA)
gold = quandl.get("LBMA/GOLD", start_date="2016-05-24", end_date="2019-01-24")
gold['returns'] = gold['USD (AM)'].pct_change(1)
gold['MA50'] = gold['USD (AM)'].rolling(50).mean()
gold['MA200'] = gold['USD (AM)'].rolling(200).mean()
gold['Cumulative Return'] = (1 + gold['returns']).cumprod()
gold[['USD (AM)','MA50','MA200']].plot(figsize=(12,6), label='gold',title='gold',legend='gold')
plt.legend(['Gold','MA50','MA200']);
#gold.head(5)
In [38]:
# Oil prices
# Reference Price for the OPEC Crude Oil Basket
Oil = quandl.get("OPEC/ORB", start_date="2016-05-24", end_date="2019-01-24")
Oil['returns'] = Oil['Value'].pct_change(1)
Oil['MA50'] = Oil['Value'].rolling(50).mean()
Oil['MA200'] = Oil['Value'].rolling(200).mean()
Oil['Cumulative Return'] = (1 + Oil['returns']).cumprod()
Oil[['Value','MA50','MA200']].plot(figsize=(12,6), label='Oil',title='Oil',legend='Oil')
plt.legend(['Oil','MA50','MA200']);
#Oil.head(5)
In [39]:
# Lets compare bitcoin return against commodities
Copper['Cumulative Return'].plot(label='Coffee',figsize=(16,8),title='Cumulative Return')
Zinc['Cumulative Return'].plot(label='Zinc')
Coffee['Cumulative Return'].plot(label='Coffee')
Corn['Cumulative Return'].plot(label='Corn')
Lean_Hogs['Cumulative Return'].plot(label='Lean_Hogs')
gold['Cumulative Return'].plot(label='gold')
Oil['Cumulative Return'].plot(label='Oil')
bitcoin_usd['Cumulative Return'].plot(label='bitcoin_usd')
plt.legend();
# the cumulative returns of Bitcoin are much higher
In [40]:
# Lets compare commodities alone
Copper['Cumulative Return'].plot(label='Coffee',figsize=(16,8),title='Cumulative Return')
Zinc['Cumulative Return'].plot(label='Zinc')
Coffee['Cumulative Return'].plot(label='Coffee')
Corn['Cumulative Return'].plot(label='Corn')
Lean_Hogs['Cumulative Return'].plot(label='Lean_Hogs')
gold['Cumulative Return'].plot(label='gold')
Oil['Cumulative Return'].plot(label='Oil')
plt.legend();
# the cumulative returns of Bitcoin are much higher
In [41]:
#Let´s print an Oil gold ratio
pd.DataFrame(Oil['Value']/gold['USD (AM)'],columns=['Oil / Gold ratio']).plot(label='Oil / Gold ratio',
figsize=(16,8),title='Oil / Gold ratio');
In [42]:
#lets take a look to the correlations between the commodities and bitcoin
commodities=pd.concat([bitcoin_usd[['returns']],Copper[['returns']],Zinc[['returns']],
Coffee[['returns']],Corn[['returns']],Lean_Hogs[['returns']],
gold[['returns']],Oil[['returns']]],axis=1)
commodities.columns=['bitcoin_usd', 'Copper', 'Zinc','Coffee','Corn', 'Lean_Hogs', 'gold', 'Oil']
plt.figure(figsize=(16,8))
sns.heatmap(commodities.corr(),cmap='coolwarm',annot=True);
# Bitcoin shows very little correlation with the rest of the commodities.
# It seems that it does not belong to this asset class
In [43]:
#Lets calculate annual volatitity
commodities_std=commodities.std()*(250^(1/2))
commodities_std
Out[43]:
In [44]:
#Lets calculate annual return
commodities_return=commodities.mean()*(250)
commodities_return
Out[44]:
In [45]:
# lets calculate Sharpe Ratio (We should have use geometric return instead
# aritmetic. We will do this as an approximation)
# I will assume 1% as Risk free rate
Rf=1/100
commodities_sharpe_ratio= (commodities_return-Rf)/commodities_std
commodities_sharpe_ratio
#bitcoin has similar risk adjusted return to Zinc, but it is not correlated with this asset class.
Out[45]:
In [46]:
#lets take a look to the correlations between the all assets
all_assets=pd.concat([bitcoin_usd[['returns']],Copper[['returns']],Zinc[['returns']],
Coffee[['returns']],Corn[['returns']],Lean_Hogs[['returns']],
gold[['returns']],Oil[['returns']],
Nasdaq100[['returns']],Dax[['returns']],stoxx_50[['returns']],hang_seng[['returns']],
eur_usd[['returns']],yuan_usd[['returns']],jpy_usd[['returns']],
ten_y_bond_US[['returns']],ten_y_bond_DE[['returns']]
,Treasury_Bill_rates_52WK[['returns_52WK']],Treasury_Bill_rates_13WK[['returns_13WK']]
,Euribor_3_M[['returns']],Euribor_12_M[['returns']]],axis=1)
all_assets.columns=['bitcoin_usd', 'Copper', 'Zinc','Coffee','Corn', 'Lean_Hogs', 'gold', 'Oil',
'Nasdaq100', 'Dax','stoxx_50','hang_seng','eur_usd', 'yuan_usd','jpy_usd',
'ten_y_bond_US', 'ten_y_bond_DE','T_bill_52w','T_bill_13w','Euribor_3M','Euribor_12M']
plt.figure(figsize=(16,8))
sns.heatmap(all_assets.corr(),cmap='coolwarm',annot=True);
In [48]:
# Bitcoin Nasdaq100 rolling correlation 100 days
return_corr =all_assets[['bitcoin_usd','Nasdaq100']]
return_corr.dropna(inplace=True)
return_corr.head(5)
fig = plt.figure(figsize=(18,10))
return12corr = pd.rolling_corr(return_corr['bitcoin_usd'], return_corr['Nasdaq100'], 100)
return12corr.plot(title='Rolling correlation 100 days Bitcoin - Nasdaq100')
#plt.show()
plt.savefig('Rolling correlation 100 days Bitcoin Nasddaq100')
In [ ]:
In [ ]: