Bitcoin and Tech Stocks: are they playing the same song? -Part 2

Topaccina
4 min readAug 3, 2022

--

How Bitcoin is positioned with respect to the stock market? This second post is focused on SP500 stocks segmentation accordingly with their correlation with the Bitcoin price. Powered by Python and open data.

Welcome back! We closed the first part of the analysis [1]with the idea to implement the SP500 index stocks segmentation based on the correlation with Bitcoin and draw on considerations regarding the portfolio diversifications.

Unsupervised Machine Learning Algorithms in Python drive us to identify potential patterns based on the companies sectors.

SP500 Stocks Dataset.

SP500 componets with their GICS sector can be easily retrieved from the web in text or csv format [2] good to be read with Pandas (GICS stand for Global Industry Classification Standard). The last year daily close prices has been collected for all the assets. As done for the 1st part of the analysis, let’s include Microstrategy (MSTR) and US Treasury Bonds (TLT) as benchmarks. The former is well known as “BTC-friendly”, the latter is expected to be uncorrelated.

The “Phytonic magic” Toolbox is the same used in Part 1 together with scikit-learn to implement the clustering [3].

import yfinance as yf
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.graph_objects as go
from sklearn.cluster import KMeans

The first step of the analysis is to get prices and evaluate the rolling correlation with the Bitcoin price on the last year timeframe (below a Python-idea of the code)

#get Close price
for tck in tickerList:
print('retrieve '+tck)
df_temp=yf.download(tck, start='2021-06-15', \
end='2022-06-15', \
period='1d',progress=False)
df['%s'%(tck)]=df_temp['Close']
...
..
#eval BTC vs other stocks rolling correlation
for tck in tickerList:
df_temp=df[‘BTC-USD’].rolling(15).corr(df[‘%s’%(tck)])
...
..
#smooth the results
for tck in tickerList:
df_corr[tck+'_MAV']=df_corr[tck].rolling(100).mean()
...

Asset segmentation: method and results

It’s time to proceed with the assets segmentation on the basis of their correlation with Bitcoin by using the K-means clustering method with Entropy as evaluation metric [3]

This approach aims to split the observations (the stocks) into k clusters accordingly with their similarities with Bitcoin. Each observation belongs to the cluster with the nearest mean, that is the prototype of the cluster.

Before running the algoritm it is necessary to transpose the dataframe including the correlation results to have the Stocks as index and the Dates as columns (Figure1). (below a python-idea of the code)

from sklearn.cluster import KMeans#transpose the correlation dataframe
df_corr_t = df_corr.transpose()
#apply KMeans to get the clustering
km=KMeans(n_clusters=8)
km.fit(df_corr_t)
Figure1: dataframe including the correlation results to have the Stocks as index and the Dates as columns

Here’s below some examples of the resulting clusters (Figure2). The algorithm is able to catch the stocks with common “BTC-correlation trend”. Some stocks groups show a positive trend implies a correlation with Bitcoin (e.g. the reported cluster7). Other groups do not show strong relationship but are more like a stationary process.

Figure2: some example of the resulting cluster. Plot of assets correlation with BTC across time. The algorithm is able group assets by similar trend

Interesting to plot the heatmaps for each cluster. Two examples below . Cluster5 that includes Microstrategy (our benchmark) shows increasing correlation across time (Figure 3) and Cluster2 that has clearly no correlation (Figure4).

Figure3: heatmap showing correlation with BTC across the time. The correlation increase for this cluster. Correlation gradually turning from blue to yellow (>0.6). MSTR (the benchmark) is highly correlated over the entire timeframe.
Figure4: heatmap showing correlation with BTC across the time. No correlation for this cluster

By referring to the two above clusters 5 and 2, diving into their component could reveal interesting patterns.

Cluster#2 (aka: no correlation with BTC) is mainly populated by “more traditional sectors” . It is dominated by “consumer staples” stocks ( per GICS sector classification) that represents the companies that are noncyclical because they produce or sell goods or services that are always in demand. They are in general quite “steady” and solid, low volatile. Good for investors seeking for stability.

On the contrary Cluster#5 (aka: the most positive correlated with BTC) include tech companies, the so called “growth stock”. They typically attract investors with “high risk appetite” prepared to accept the volatiliy for high returns. This matches with “Bitcoiner” profile. NO “Consumer Staples” in this cluster at all.

Figure6: GICS sector of cluster5 components, the ones showing the best correlation with BTC
Figure6: GICS sectors of cluster2 components, the ones showing no correlation with BTC

Pandas output API help to generating a summary table showing the main companies belong to cluster 5 and 7. We can now see “what’s really under the hood” (Figure6, Figure7)

Figure6: Subset of cluster5 components (the best correlation with BTC)
Figure7: Subset of cluster2 components (no correlation with BTC)

Wrapping-up

On the basis of this first level analysis the Bitcoin market is no longer a stand-alone market uncorrelated with the traditional one. We need to keep this in mind if we aim to protect our porfolio through the diversification.

…it’s clear “Bitcoin is Here to Stay and plays the same song as the big Tech”…

❤️‍🔥❤️‍🔥Bitcoiners and Python Lovers.. we’’ll see you around!❤️‍🔥❤️‍🔥

References:

[1] https://medium.com/@laulor1981/bitcoin-and-tech-stocks-are-they-playing-the-same-song-part-1-bff1bbd94929

[2] https://stockmarketmba.com/stocksinthesp500.php

[3] https://scikit-learn.org/stable/index.html

[4]https://en.wikipedia.org/wiki/K-means_clustering

--

--

Topaccina

Interested in Data science, ML, AI & blockchain! 💕 -Data analyst, semiconductors industry. Runner for fun! linkedin.com/in/laura-lorenzi-14885a12