Cryptocurrency Analysis with Python — Buy and Hold | by Roman Orac | Towards Data Science

In this part, I am going to analyze which coin (Bitcoin, Ethereum or Litecoin) was the most profitable in the last two months using buy and hold strategy. We’ll go through the analysis of these 3 cryptocurrencies and try to give an objective answer.
In case you’ve missed my other articles about this topic:
romanorac.medium.com
Here are few links that might interest you:
- Complete your Python analyses 10x faster with Mito [Product]- Free skill tests for Data Scientists & ML Engineers [Test]- All New Self-Driving Car Engineer Nanodegree [Course]
Would you like to read more such articles? If so, you can support me by clicking on any links above. Some of them are affiliate links, but you don’t need to buy anything.
I am not a trader and this blog post is not financial advice. This is purely introductory knowledge. The conclusion here can be misleading as we analyze the period with immense growth.
seaborn: statistical data visualization
For other requirements, see my previous blog post in this series.
To get the latest data, go to the previous blog post, where I described how to download it using Cryptocompare API. You can also use the data I work within this example.
First, let’s download hourly data for BTC, ETH, and LTC from Coinbase exchange. This time we work with an hourly time interval as it has higher granularity. Cryptocompare API limits response to 2000 samples, which is 2.7 months of data for each coin.
import pandas as pddef get_filename(from_symbol, to_symbol, exchange, datetime_interval, download_date):    return '%s_%s_%s_%s_%s.csv' % (from_symbol, to_symbol, exchange, datetime_interval, download_date)def read_dataset(filename):    print('Reading data from %s' % filename)    df = pd.read_csv(filename)    df.datetime = pd.to_datetime(df.datetime) # change to datetime    df = df.set_index('datetime')     df = df.sort_index() # sort by datetime    print(df.shape)    return df
df_btc = read_dataset(get_filename('BTC', 'USD', 'Coinbase', 'hour', '2017-12-24'))df_eth = read_dataset(get_filename('ETH', 'USD', 'Coinbase', 'hour', '2017-12-24'))df_ltc = read_dataset(get_filename('LTC', 'USD', 'Coinbase', 'hour', '2017-12-24'))df_btc.head()
We are going to analyze closing prices, which are prices at which the hourly period closed. We merge BTC, ETH and LTC closing prices to a Dataframe to make analysis easier.
df = pd.DataFrame({'BTC': df_btc.close,                   'ETH': df_eth.close,                   'LTC': df_ltc.close})df.head()
In 2.7 months, all three cryptocurrencies fluctuated a lot as you can observe in the table below.
For each coin, we count the number of events and calculate mean, standard deviation, minimum, quartiles, and maximum closing price.
Observations
The difference between the highest and the lowest BTC price was more than $15000 in 2.7 months.
The LTC surged from $48.61 to $378.66 at a certain point, which is an increase of 678.98%.
df.describe()
We visualize the data in the table above with a box plot. A box plot shows the quartiles of the dataset with points that are determined to be outliers using a method of the inter-quartile range (IQR). In other words, the IQR is the first quartile (25%) subtracted from the third quartile (75%).
On the box plot below, we see that LTC closing hourly price was most of the time between $50 and $100 in the last 2.7 months. All values over $150 are outliers (using IQR). Note that outliers are specific to this data sample.
import seaborn as snsax = sns.boxplot(data=df['LTC'], orient="h")
Let’s estimate the frequency distribution of LTC closing prices. The histogram shows the number of hours LTC had a certain value.
Observations
LTC closing price was not over $100 for many hours.
it has right-skewed distribution because a natural limit prevents outcomes on one side.
blue dashed line (median) shows that half of the time closing prices were under $63.50.
df['LTC'].hist(bins=30, figsize=(15,10)).axvline(df['LTC'].median(), color='b', linestyle='dashed', linewidth=2)
The chart below shows the absolute closing prices. It is not of much use as BTC closing prices are much higher than prices of ETH and LTC.
df.plot(grid=True, figsize=(15, 10))
We are interested in a relative change of the price rather than in absolute price, so we use three different y-axis scales.
We see that closing prices move in tandem. When one coin closing price increases so do the other.
import matplotlib.pyplot as pltimport numpy as npfig, ax1 = plt.subplots(figsize=(20, 10))ax2 = ax1.twinx()rspine = ax2.spines['right']rspine.set_position(('axes', 1.15))ax2.set_frame_on(True)ax2.patch.set_visible(False)fig.subplots_adjust(right=0.7)df['BTC'].plot(ax=ax1, style='b-')df['ETH'].plot(ax=ax1, style='r-', secondary_y=True)df['LTC'].plot(ax=ax2, style='g-')# legendax2.legend([ax1.get_lines()[0],            ax1.right_ax.get_lines()[0],            ax2.get_lines()[0]],           ['BTC', 'ETH', 'LTC'])
We calculate the Pearson correlation between the closing prices of BTC, ETH, and LTC. Pearson correlation is a measure of the linear correlation between two variables X and Y. It has a value between +1 and −1, where 1 is the total positive linear correlation, 0 is no linear correlation, and −1 is the total negative linear correlation. The correlation matrix is symmetric so we only show the lower half.
Sifr Data daily updates Pearson correlations for many cryptocurrencies.
Observations
Closing prices aren’t normalized, see Log Returns, where we normalize closing prices before calculating correlation,
BTC, ETH and LTC were highly correlated in the past 2 months. This means, when BTC closing price increased, ETH and LTC followed.
ETH and LTC were even more correlated with 0.9565 Pearson correlation coefficient.
import seaborn as snsimport matplotlib.pyplot as plt# Compute the correlation matrixcorr = df.corr()# Generate a mask for the upper trianglemask = np.zeros_like(corr, dtype=np.bool)mask[np.triu_indices_from(mask)] = True# Set up the matplotlib figuref, ax = plt.subplots(figsize=(10, 10))# Draw the heatmap with the mask and correct aspect ratiosns.heatmap(corr, annot=True, fmt = '.4f', mask=mask, center=0, square=True, linewidths=.5)
Buy and hold is a passive investment strategy in which an investor buys a cryptocurrency and holds it for a long period, regardless of fluctuations in the market.
Let’s analyze returns using the Buy and hold strategy for the past 2.7 months. We calculate the return percentage, where t represents a certain period and price0 is the initial closing price:
df_return = df.apply(lambda x: x / x[0])df_return.head()
We show that LTC was the most profitable for the period between October 2, 2017 and December 24, 2017.
df_return.plot(grid=True, figsize=(15, 10)).axhline(y = 1, color = "black", lw = 2)
The cryptocurrencies we analyzed fluctuated a lot but all gained in a given 2.7 months period.
To run this code download the Jupyter notebook.
Follow me on Twitter, where I regularly tweet about Data Science and Machine Learning.