<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Data Analysis Archives - Analytica Data Science Solutions</title>
	<atom:link href="https://analyticadss.com/tag/data-analysis/feed/" rel="self" type="application/rss+xml" />
	<link>https://analyticadss.com/tag/data-analysis/</link>
	<description>World&#039;s Leading Artificial Inelegance Company</description>
	<lastBuildDate>Mon, 02 Jan 2023 19:04:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://analyticadss.com/wp-content/uploads/2020/06/cropped-F.B-Cover-photo_V0.1-02-32x32.png</url>
	<title>Data Analysis Archives - Analytica Data Science Solutions</title>
	<link>https://analyticadss.com/tag/data-analysis/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Analyzing Crypto Market using R — Part 2</title>
		<link>https://analyticadss.com/analyzing-cryptocurrency-markets-using-r-part-2/</link>
		
		<dc:creator><![CDATA[Aous Abdo]]></dc:creator>
		<pubDate>Mon, 24 Dec 2018 10:34:58 +0000</pubDate>
				<category><![CDATA[Data Science]]></category>
		<category><![CDATA[R Statistical Language]]></category>
		<category><![CDATA[Bitcoin]]></category>
		<category><![CDATA[Cryptocurrency]]></category>
		<category><![CDATA[Data Analysis]]></category>
		<category><![CDATA[R]]></category>
		<guid isPermaLink="false">https://analyticadss.com/?p=4907</guid>

					<description><![CDATA[<p>Correlations in the Crypto World Analyzing crypto market Aous Abdo, WWW.ANALYTICADSS.COMAn interactive version of this post can be found on here. In my previous post I explored bitcoin data from different exchanges, we also covered some arbitrage-related data. In part 2 of this series I will explore alt coin related data. R Libraries Below is a list [&#8230;]</p>
<p>The post <a href="https://analyticadss.com/analyzing-cryptocurrency-markets-using-r-part-2/">Analyzing Crypto Market using R — Part 2</a> appeared first on <a href="https://analyticadss.com">Analytica Data Science Solutions</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading" id="bf86">Correlations in the Crypto World</h2>



<p class="wp-block-paragraph">Analyzing crypto market</p>



<p class="wp-block-paragraph"><a href="https://medium.com/u/4f20dbfad286?source=post_page-----b1a0aa44006e--------------------------------" rel="noreferrer noopener" target="_blank">Aous Abdo</a>, <a href="http://www.analyticadss.com/" rel="noreferrer noopener" target="_blank">WWW.ANALYTICADSS.COM</a><br>An interactive version of this post can be found on <a href="https://analyticadss.com/adss_blog/crypto_notebook_part2.nb.html" rel="noreferrer noopener" target="_blank">here</a>.</p>



<p class="wp-block-paragraph" id="ae2d">In my previous post I explored bitcoin data from different exchanges, we also covered some arbitrage-related data. In part 2 of this series I will explore alt coin related data.</p>



<h2 class="wp-block-heading" id="3a8a">R Libraries</h2>



<p class="wp-block-paragraph" id="46c5">Below is a list of R libraries we will be using to help us with our analysis. Not all of them are necessary but they all will make our life easier.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#F8F8F2;--cbp-line-number-width:15.395835876464844px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#272822"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="library(PoloniexR)
library(data.table)
library(lubridate)
library(Quandl)
library(plyr)
library(stringr)
library(ggplot2)
library(plotly)
library(janitor)
library(quantmod)
library(pryr)
library(corrplot)
library(PerformanceAnalytics)
library(tidyr)
library(MLmetrics)
library(tidyquant)
library(corrr)
library(cowplot)" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki" style="background-color: #272822"><code><span class="line"><span style="color: #66D9EF">library</span><span style="color: #F8F8F2">(PoloniexR)</span></span>
<span class="line"><span style="color: #66D9EF">library</span><span style="color: #F8F8F2">(data.table)</span></span>
<span class="line"><span style="color: #66D9EF">library</span><span style="color: #F8F8F2">(lubridate)</span></span>
<span class="line"><span style="color: #66D9EF">library</span><span style="color: #F8F8F2">(Quandl)</span></span>
<span class="line"><span style="color: #66D9EF">library</span><span style="color: #F8F8F2">(plyr)</span></span>
<span class="line"><span style="color: #66D9EF">library</span><span style="color: #F8F8F2">(stringr)</span></span>
<span class="line"><span style="color: #66D9EF">library</span><span style="color: #F8F8F2">(ggplot2)</span></span>
<span class="line"><span style="color: #66D9EF">library</span><span style="color: #F8F8F2">(plotly)</span></span>
<span class="line"><span style="color: #66D9EF">library</span><span style="color: #F8F8F2">(janitor)</span></span>
<span class="line"><span style="color: #66D9EF">library</span><span style="color: #F8F8F2">(quantmod)</span></span>
<span class="line"><span style="color: #66D9EF">library</span><span style="color: #F8F8F2">(pryr)</span></span>
<span class="line"><span style="color: #66D9EF">library</span><span style="color: #F8F8F2">(corrplot)</span></span>
<span class="line"><span style="color: #66D9EF">library</span><span style="color: #F8F8F2">(PerformanceAnalytics)</span></span>
<span class="line"><span style="color: #66D9EF">library</span><span style="color: #F8F8F2">(tidyr)</span></span>
<span class="line"><span style="color: #66D9EF">library</span><span style="color: #F8F8F2">(MLmetrics)</span></span>
<span class="line"><span style="color: #66D9EF">library</span><span style="color: #F8F8F2">(tidyquant)</span></span>
<span class="line"><span style="color: #66D9EF">library</span><span style="color: #F8F8F2">(corrr)</span></span>
<span class="line"><span style="color: #66D9EF">library</span><span style="color: #F8F8F2">(cowplot)</span></span></code></pre></div>



<h2 class="wp-block-heading" id="7a90">Data</h2>



<p class="wp-block-paragraph" id="f5c0">The best source I know off to get alt-coin data is through <a href="https://cran.r-project.org/web/packages/PoloniexR/index.html" rel="noreferrer noopener" target="_blank">PoloniexR</a>. I have written an R function to help download data.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#F8F8F2;--cbp-line-number-width:23.104170322418213px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#272822"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="get_alt_data <- function(tz = &quot;UTC&quot;
                         , coin = c(&quot;ETH&quot;, &quot;LTC&quot;)
                         , add_bitcoin = TRUE
                         , return_in_USDT = TRUE
                         , from = &quot;2017-01-01&quot;
                         , to = &quot;2018-04-09&quot;
                         , period = &quot;D&quot;
                         , verbose = FALSE){
  
  # We will be using the public API
  poloniex.public <- PoloniexPublicAPI()
  
  # set the time zone to utc
  Sys.setenv(tz = tz)
  
  # convert from and to into time obj
  from  <- as.POSIXct(paste(from, tz, sep = &quot;&quot;))
  to    <- as.POSIXct(paste(to, tz, sep = &quot;&quot;))
  
  # lists to store data.tables and xts objects
  chart_list <- list()
  dt_list    <- list()
  
  # make sure the coin pair is in upper case
  coin       <- toupper(coin)
  coin_pairs <- paste0(&quot;BTC_&quot;, coin[coin != &quot;BTC&quot;])
  if(add_bitcoin | return_in_USDT) coin_pairs <- c(&quot;USDT_BTC&quot;, coin_pairs)
  
  # loop over the coins to get the data
  for(i in coin_pairs){
    if(verbose)
      invisible(cat('\tGetting data for ', i, ' pair\n'))
    
    # this is a list that will contain the chart data for each coin pair
    try(chart_list[[i]] <- ReturnChartData(theObject = poloniex.public
                                       , pair      = i
                                       , from      = from
                                       , to        = to
                                       , period    = period)
        , silent = TRUE)
    
    # list to contain data.tables 
    try(dt_list[[i]] <- as.data.table(chart_list[[i]]), silent = TRUE)
  }
  
  # convert to data.table and make sure to add a column containing the pairs
  coin_dt <- rbindlist(l = dt_list, use.names = TRUE, idcol = &quot;pair&quot;)
  
  # return data in usdt prices
  if(return_in_USDT){
    # to get the price of the alt coin in usdt is not that simple but we'll do it
    # get a DT of the btc_usdt pair
    btc_usd <- coin_dt[pair == &quot;USDT_BTC&quot;]
    btc_usd <- btc_usd[, .(index, pair, weightedaverage)]
    setnames(btc_usd, c(&quot;Date&quot;, &quot;USDT_BTC_pair&quot;, &quot;USDT_BTC_price&quot;))
    
    # get DT with only alt coins
    alt_coins <- copy(coin_dt)#[pair != &quot;USDT_BTC&quot;]
    
    # now we need to add an index to the alt_coins table, but first we have to rename the index column
    alt_coins[, Date := index]
    alt_coins[, index := 1:.N]
    setkey(alt_coins, index)
    
    # now merge the data tables
    coin_dt_usdt <- merge(x = alt_coins, y = btc_usd, by = &quot;Date&quot;)
    
    # now calcualte the price in usdt
    coin_dt_usdt[, price_usdt := ifelse(pair == &quot;USDT_BTC&quot;, USDT_BTC_price, weightedaverage * USDT_BTC_price)]
    
    # now get rid of the extra columns
    coin_dt_usdt[, c(&quot;USDT_BTC_price&quot;, &quot;USDT_BTC_pair&quot;) := NULL]
    
    # we need to change some column names
    col_names_to_change <- c(&quot;pair&quot;, &quot;high&quot;, &quot;low&quot;, &quot;open&quot;, &quot;close&quot;, &quot;volume&quot;, &quot;quotevolume&quot;, &quot;weightedaverage&quot;)
    col_names <- names(coin_dt_usdt)
    col_names[col_names %in% col_names_to_change] <- paste0(col_names_to_change, '_btc')
    
    setnames(coin_dt_usdt, col_names)
    
    # add a column for the usdt pair
    coin_dt_usdt[, pair_usdt := gsub(&quot;BTC_&quot;, &quot;USDT_&quot;, pair_btc)]
    
    # adjust col order
    setcolorder(coin_dt_usdt, c(1:10, 12, 11))
    
    # set key again
    setkey(coin_dt_usdt, index)
    
    # now get rid of the index column since it is not needed anymore
    coin_dt_usdt[, index := NULL]
    
    # now put together the return list  
    return_list <- list(alt_chart_list = chart_list, alt_dt = coin_dt, alt_usdt_dt = coin_dt_usdt)
  }else{
    return_list <- list(alt_chart_list = chart_list, alt_dt = coin_dt)
  }
  
  return(return_list)
}" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki" style="background-color: #272822"><code><span class="line"><span style="color: #A6E22E">get_alt_data</span><span style="color: #F8F8F2"> </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">function</span><span style="color: #F8F8F2">(</span><span style="color: #FD971F">tz</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;UTC&quot;</span></span>
<span class="line"><span style="color: #F8F8F2">                         , </span><span style="color: #FD971F">coin</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">c</span><span style="color: #F8F8F2">(</span><span style="color: #E6DB74">&quot;ETH&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&quot;LTC&quot;</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">                         , </span><span style="color: #FD971F">add_bitcoin</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">TRUE</span></span>
<span class="line"><span style="color: #F8F8F2">                         , </span><span style="color: #FD971F">return_in_USDT</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">TRUE</span></span>
<span class="line"><span style="color: #F8F8F2">                         , </span><span style="color: #FD971F">from</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;2017-01-01&quot;</span></span>
<span class="line"><span style="color: #F8F8F2">                         , </span><span style="color: #FD971F">to</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;2018-04-09&quot;</span></span>
<span class="line"><span style="color: #F8F8F2">                         , </span><span style="color: #FD971F">period</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;D&quot;</span></span>
<span class="line"><span style="color: #F8F8F2">                         , </span><span style="color: #FD971F">verbose</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">FALSE</span><span style="color: #F8F8F2">){</span></span>
<span class="line"><span style="color: #F8F8F2">  </span></span>
<span class="line"><span style="color: #F8F8F2">  </span><span style="color: #88846F"># We will be using the public API</span></span>
<span class="line"><span style="color: #F8F8F2">  poloniex.public </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> PoloniexPublicAPI()</span></span>
<span class="line"><span style="color: #F8F8F2">  </span></span>
<span class="line"><span style="color: #F8F8F2">  </span><span style="color: #88846F"># set the time zone to utc</span></span>
<span class="line"><span style="color: #F8F8F2">  </span><span style="color: #66D9EF">Sys.setenv</span><span style="color: #F8F8F2">(</span><span style="color: #FD971F">tz</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> tz)</span></span>
<span class="line"><span style="color: #F8F8F2">  </span></span>
<span class="line"><span style="color: #F8F8F2">  </span><span style="color: #88846F"># convert from and to into time obj</span></span>
<span class="line"><span style="color: #F8F8F2">  from  </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">as.POSIXct</span><span style="color: #F8F8F2">(</span><span style="color: #66D9EF">paste</span><span style="color: #F8F8F2">(from, tz, </span><span style="color: #FD971F">sep</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;&quot;</span><span style="color: #F8F8F2">))</span></span>
<span class="line"><span style="color: #F8F8F2">  to    </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">as.POSIXct</span><span style="color: #F8F8F2">(</span><span style="color: #66D9EF">paste</span><span style="color: #F8F8F2">(to, tz, </span><span style="color: #FD971F">sep</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;&quot;</span><span style="color: #F8F8F2">))</span></span>
<span class="line"><span style="color: #F8F8F2">  </span></span>
<span class="line"><span style="color: #F8F8F2">  </span><span style="color: #88846F"># lists to store data.tables and xts objects</span></span>
<span class="line"><span style="color: #F8F8F2">  chart_list </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">list</span><span style="color: #F8F8F2">()</span></span>
<span class="line"><span style="color: #F8F8F2">  dt_list    </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">list</span><span style="color: #F8F8F2">()</span></span>
<span class="line"><span style="color: #F8F8F2">  </span></span>
<span class="line"><span style="color: #F8F8F2">  </span><span style="color: #88846F"># make sure the coin pair is in upper case</span></span>
<span class="line"><span style="color: #F8F8F2">  coin       </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">toupper</span><span style="color: #F8F8F2">(coin)</span></span>
<span class="line"><span style="color: #F8F8F2">  coin_pairs </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">paste0</span><span style="color: #F8F8F2">(</span><span style="color: #E6DB74">&quot;BTC_&quot;</span><span style="color: #F8F8F2">, coin[coin </span><span style="color: #F92672">!=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;BTC&quot;</span><span style="color: #F8F8F2">])</span></span>
<span class="line"><span style="color: #F8F8F2">  </span><span style="color: #F92672">if</span><span style="color: #F8F8F2">(add_bitcoin </span><span style="color: #F92672">|</span><span style="color: #F8F8F2"> return_in_USDT) coin_pairs </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">c</span><span style="color: #F8F8F2">(</span><span style="color: #E6DB74">&quot;USDT_BTC&quot;</span><span style="color: #F8F8F2">, coin_pairs)</span></span>
<span class="line"><span style="color: #F8F8F2">  </span></span>
<span class="line"><span style="color: #F8F8F2">  </span><span style="color: #88846F"># loop over the coins to get the data</span></span>
<span class="line"><span style="color: #F8F8F2">  </span><span style="color: #F92672">for</span><span style="color: #F8F8F2">(i </span><span style="color: #F92672">in</span><span style="color: #F8F8F2"> coin_pairs){</span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #F92672">if</span><span style="color: #F8F8F2">(verbose)</span></span>
<span class="line"><span style="color: #F8F8F2">      </span><span style="color: #F92672">invisible</span><span style="color: #F8F8F2">(</span><span style="color: #66D9EF">cat</span><span style="color: #F8F8F2">(</span><span style="color: #E6DB74">&#39;</span><span style="color: #AE81FF">\t</span><span style="color: #E6DB74">Getting data for &#39;</span><span style="color: #F8F8F2">, i, </span><span style="color: #E6DB74">&#39; pair</span><span style="color: #AE81FF">\n</span><span style="color: #E6DB74">&#39;</span><span style="color: #F8F8F2">))</span></span>
<span class="line"><span style="color: #F8F8F2">    </span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #88846F"># this is a list that will contain the chart data for each coin pair</span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #66D9EF">try</span><span style="color: #F8F8F2">(chart_list[[i]] </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> ReturnChartData(</span><span style="color: #FD971F">theObject</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> poloniex.public</span></span>
<span class="line"><span style="color: #F8F8F2">                                       , pair      = i</span></span>
<span class="line"><span style="color: #F8F8F2">                                       , from      = from</span></span>
<span class="line"><span style="color: #F8F8F2">                                       , to        = to</span></span>
<span class="line"><span style="color: #F8F8F2">                                       , period    = period)</span></span>
<span class="line"><span style="color: #F8F8F2">        , </span><span style="color: #FD971F">silent</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">TRUE</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">    </span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #88846F"># list to contain data.tables </span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #66D9EF">try</span><span style="color: #F8F8F2">(dt_list[[i]] </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> as.data.table(chart_list[[i]]), </span><span style="color: #FD971F">silent</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">TRUE</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">  }</span></span>
<span class="line"><span style="color: #F8F8F2">  </span></span>
<span class="line"><span style="color: #F8F8F2">  </span><span style="color: #88846F"># convert to data.table and make sure to add a column containing the pairs</span></span>
<span class="line"><span style="color: #F8F8F2">  coin_dt </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> rbindlist(</span><span style="color: #FD971F">l</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> dt_list, </span><span style="color: #FD971F">use.names</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">TRUE</span><span style="color: #F8F8F2">, </span><span style="color: #FD971F">idcol</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;pair&quot;</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">  </span></span>
<span class="line"><span style="color: #F8F8F2">  </span><span style="color: #88846F"># return data in usdt prices</span></span>
<span class="line"><span style="color: #F8F8F2">  </span><span style="color: #F92672">if</span><span style="color: #F8F8F2">(return_in_USDT){</span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #88846F"># to get the price of the alt coin in usdt is not that simple but we&#39;ll do it</span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #88846F"># get a DT of the btc_usdt pair</span></span>
<span class="line"><span style="color: #F8F8F2">    btc_usd </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> coin_dt[pair </span><span style="color: #F92672">==</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;USDT_BTC&quot;</span><span style="color: #F8F8F2">]</span></span>
<span class="line"><span style="color: #F8F8F2">    btc_usd </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> btc_usd[, .(index, pair, weightedaverage)]</span></span>
<span class="line"><span style="color: #F8F8F2">    setnames(btc_usd, </span><span style="color: #66D9EF">c</span><span style="color: #F8F8F2">(</span><span style="color: #E6DB74">&quot;Date&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&quot;USDT_BTC_pair&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&quot;USDT_BTC_price&quot;</span><span style="color: #F8F8F2">))</span></span>
<span class="line"><span style="color: #F8F8F2">    </span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #88846F"># get DT with only alt coins</span></span>
<span class="line"><span style="color: #F8F8F2">    alt_coins </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> copy(coin_dt)</span><span style="color: #88846F">#[pair != &quot;USDT_BTC&quot;]</span></span>
<span class="line"><span style="color: #F8F8F2">    </span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #88846F"># now we need to add an index to the alt_coins table, but first we have to rename the index column</span></span>
<span class="line"><span style="color: #F8F8F2">    alt_coins[, Date </span><span style="color: #F92672">:=</span><span style="color: #F8F8F2"> index]</span></span>
<span class="line"><span style="color: #F8F8F2">    alt_coins[, index </span><span style="color: #F92672">:=</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">1</span><span style="color: #F92672">:</span><span style="color: #F8F8F2">.N]</span></span>
<span class="line"><span style="color: #F8F8F2">    setkey(alt_coins, index)</span></span>
<span class="line"><span style="color: #F8F8F2">    </span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #88846F"># now merge the data tables</span></span>
<span class="line"><span style="color: #F8F8F2">    coin_dt_usdt </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">merge</span><span style="color: #F8F8F2">(</span><span style="color: #FD971F">x</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> alt_coins, </span><span style="color: #FD971F">y</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> btc_usd, </span><span style="color: #FD971F">by</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;Date&quot;</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">    </span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #88846F"># now calcualte the price in usdt</span></span>
<span class="line"><span style="color: #F8F8F2">    coin_dt_usdt[, price_usdt </span><span style="color: #F92672">:=</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">ifelse</span><span style="color: #F8F8F2">(pair </span><span style="color: #F92672">==</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;USDT_BTC&quot;</span><span style="color: #F8F8F2">, USDT_BTC_price, weightedaverage </span><span style="color: #F92672">*</span><span style="color: #F8F8F2"> USDT_BTC_price)]</span></span>
<span class="line"><span style="color: #F8F8F2">    </span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #88846F"># now get rid of the extra columns</span></span>
<span class="line"><span style="color: #F8F8F2">    coin_dt_usdt[, </span><span style="color: #66D9EF">c</span><span style="color: #F8F8F2">(</span><span style="color: #E6DB74">&quot;USDT_BTC_price&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&quot;USDT_BTC_pair&quot;</span><span style="color: #F8F8F2">) </span><span style="color: #F92672">:=</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">NULL</span><span style="color: #F8F8F2">]</span></span>
<span class="line"><span style="color: #F8F8F2">    </span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #88846F"># we need to change some column names</span></span>
<span class="line"><span style="color: #F8F8F2">    col_names_to_change </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">c</span><span style="color: #F8F8F2">(</span><span style="color: #E6DB74">&quot;pair&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&quot;high&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&quot;low&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&quot;open&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&quot;close&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&quot;volume&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&quot;quotevolume&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&quot;weightedaverage&quot;</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">    col_names </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">names</span><span style="color: #F8F8F2">(coin_dt_usdt)</span></span>
<span class="line"><span style="color: #F8F8F2">    col_names[col_names </span><span style="color: #F92672">%in%</span><span style="color: #F8F8F2"> col_names_to_change] </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">paste0</span><span style="color: #F8F8F2">(col_names_to_change, </span><span style="color: #E6DB74">&#39;_btc&#39;</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">    </span></span>
<span class="line"><span style="color: #F8F8F2">    setnames(coin_dt_usdt, col_names)</span></span>
<span class="line"><span style="color: #F8F8F2">    </span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #88846F"># add a column for the usdt pair</span></span>
<span class="line"><span style="color: #F8F8F2">    coin_dt_usdt[, pair_usdt </span><span style="color: #F92672">:=</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">gsub</span><span style="color: #F8F8F2">(</span><span style="color: #E6DB74">&quot;BTC_&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&quot;USDT_&quot;</span><span style="color: #F8F8F2">, pair_btc)]</span></span>
<span class="line"><span style="color: #F8F8F2">    </span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #88846F"># adjust col order</span></span>
<span class="line"><span style="color: #F8F8F2">    setcolorder(coin_dt_usdt, </span><span style="color: #66D9EF">c</span><span style="color: #F8F8F2">(</span><span style="color: #AE81FF">1</span><span style="color: #F92672">:</span><span style="color: #AE81FF">10</span><span style="color: #F8F8F2">, </span><span style="color: #AE81FF">12</span><span style="color: #F8F8F2">, </span><span style="color: #AE81FF">11</span><span style="color: #F8F8F2">))</span></span>
<span class="line"><span style="color: #F8F8F2">    </span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #88846F"># set key again</span></span>
<span class="line"><span style="color: #F8F8F2">    setkey(coin_dt_usdt, index)</span></span>
<span class="line"><span style="color: #F8F8F2">    </span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #88846F"># now get rid of the index column since it is not needed anymore</span></span>
<span class="line"><span style="color: #F8F8F2">    coin_dt_usdt[, index </span><span style="color: #F92672">:=</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">NULL</span><span style="color: #F8F8F2">]</span></span>
<span class="line"><span style="color: #F8F8F2">    </span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #88846F"># now put together the return list  </span></span>
<span class="line"><span style="color: #F8F8F2">    return_list </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">list</span><span style="color: #F8F8F2">(</span><span style="color: #FD971F">alt_chart_list</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> chart_list, </span><span style="color: #FD971F">alt_dt</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> coin_dt, </span><span style="color: #FD971F">alt_usdt_dt</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> coin_dt_usdt)</span></span>
<span class="line"><span style="color: #F8F8F2">  }</span><span style="color: #F92672">else</span><span style="color: #F8F8F2">{</span></span>
<span class="line"><span style="color: #F8F8F2">    return_list </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">list</span><span style="color: #F8F8F2">(</span><span style="color: #FD971F">alt_chart_list</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> chart_list, </span><span style="color: #FD971F">alt_dt</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> coin_dt)</span></span>
<span class="line"><span style="color: #F8F8F2">  }</span></span>
<span class="line"><span style="color: #F8F8F2">  </span></span>
<span class="line"><span style="color: #F8F8F2">  </span><span style="color: #F92672">return</span><span style="color: #F8F8F2">(return_list)</span></span>
<span class="line"><span style="color: #F8F8F2">}</span></span></code></pre></div>



<p class="wp-block-paragraph">The function above can be used to download data for multiple coin at the same time. The function returns a data.table object with data for all coins in the function call. Even if the user doesn’t add bitcoin to the list of coins, the function adds bitcoin by default. This can be deactivated with the add_bitcoin argument. Here is an example</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#F8F8F2;--cbp-line-number-width:7.6875px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#272822"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="# get alt data for some coins
alt_data <- get_alt_data(return_in_USDT = T
                         , from = &quot;2015-01-01&quot;
                         , coin = c('ETH','XRP', 'BCH', 'LTC', 'NEO', 'XMR', 'DASH', 'XEM'))[['alt_usdt_dt']]" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki" style="background-color: #272822"><code><span class="line"><span style="color: #88846F"># get alt data for some coins</span></span>
<span class="line"><span style="color: #F8F8F2">alt_data </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> get_alt_data(</span><span style="color: #FD971F">return_in_USDT</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> T</span></span>
<span class="line"><span style="color: #F8F8F2">                         , </span><span style="color: #FD971F">from</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;2015-01-01&quot;</span></span>
<span class="line"><span style="color: #F8F8F2">                         , </span><span style="color: #FD971F">coin</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">c</span><span style="color: #F8F8F2">(</span><span style="color: #E6DB74">&#39;ETH&#39;</span><span style="color: #F8F8F2">,</span><span style="color: #E6DB74">&#39;XRP&#39;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&#39;BCH&#39;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&#39;LTC&#39;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&#39;NEO&#39;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&#39;XMR&#39;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&#39;DASH&#39;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&#39;XEM&#39;</span><span style="color: #F8F8F2">))[[</span><span style="color: #E6DB74">&#39;alt_usdt_dt&#39;</span><span style="color: #F8F8F2">]]</span></span></code></pre></div>



<p class="wp-block-paragraph" id="a8ab">Let’s look at the data we just downloaded</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#F8F8F2;--cbp-line-number-width:7.704856872558594px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#272822"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="head(alt_data)" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki" style="background-color: #272822"><code><span class="line"><span style="color: #66D9EF">head</span><span style="color: #F8F8F2">(alt_data)</span></span></code></pre></div>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="828" height="249" loading="lazy" src="https://analyticadss.com/wp-content/uploads/2022/12/1_yabOeX6Rqn8e6eDO1XzGFg.webp" alt="" class="wp-image-4908" srcset="https://analyticadss.com/wp-content/uploads/2022/12/1_yabOeX6Rqn8e6eDO1XzGFg.webp 828w, https://analyticadss.com/wp-content/uploads/2022/12/1_yabOeX6Rqn8e6eDO1XzGFg-500x150.webp 500w, https://analyticadss.com/wp-content/uploads/2022/12/1_yabOeX6Rqn8e6eDO1XzGFg-150x45.webp 150w, https://analyticadss.com/wp-content/uploads/2022/12/1_yabOeX6Rqn8e6eDO1XzGFg-768x231.webp 768w" sizes="auto, (max-width: 828px) 100vw, 828px" /></figure>
</div>


<p class="wp-block-paragraph" id="9a46">The table shows the date, OHLC, Volume, and weightedaverage price in BTC. It also shows the pair and we added the price in USD.</p>



<h2 class="wp-block-heading" id="c5e5">Bitcoin-Altcoins Correlations</h2>



<p class="wp-block-paragraph" id="dbac">Wheneven I look at the prices of the coins available on my <a href="https://www.coinbase.com/" target="_blank" rel="noreferrer noopener">coinbase</a> app I always get struck by the similarity of the price trends between the four coins available on coinbase: BTC, ETH, BCH, and LTC, see Figure below. So I thought it will be a good idea to explore the correlation in price trends between altcoins and bitcoin.</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="661" height="1024" loading="lazy" src="https://analyticadss.com/wp-content/uploads/2022/12/1_6a1iEbXQTe5L9tKq9xs1XA-661x1024.webp" alt="" class="wp-image-4909" srcset="https://analyticadss.com/wp-content/uploads/2022/12/1_6a1iEbXQTe5L9tKq9xs1XA-661x1024.webp 661w, https://analyticadss.com/wp-content/uploads/2022/12/1_6a1iEbXQTe5L9tKq9xs1XA-323x500.webp 323w, https://analyticadss.com/wp-content/uploads/2022/12/1_6a1iEbXQTe5L9tKq9xs1XA-97x150.webp 97w, https://analyticadss.com/wp-content/uploads/2022/12/1_6a1iEbXQTe5L9tKq9xs1XA-768x1190.webp 768w, https://analyticadss.com/wp-content/uploads/2022/12/1_6a1iEbXQTe5L9tKq9xs1XA.webp 786w" sizes="auto, (max-width: 661px) 100vw, 661px" /></figure>
</div>


<p class="wp-block-paragraph" id="47a1">Let’s look at price trends of the coins we just downloaded. To better see potential correlations I am going to only zoom in on 2018.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#F8F8F2;--cbp-line-number-width:7.70486307144165px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#272822"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="p <- ggplot(alt_data[year(Date) == 2018], aes(x = Date, y =  price_usdt, col = pair_usdt)) + geom_line()
p <- p + facet_wrap(~pair_usdt, scales = &quot;free&quot;, ncol = 3) + theme_minimal() + theme(legend.position=&quot;none&quot;) + ylab(&quot;Price (USD)&quot;)
p" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki" style="background-color: #272822"><code><span class="line"><span style="color: #F8F8F2">p </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> ggplot(alt_data[year(Date) </span><span style="color: #F92672">==</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">2018</span><span style="color: #F8F8F2">], aes(</span><span style="color: #FD971F">x</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> Date, </span><span style="color: #FD971F">y</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2">  price_usdt, </span><span style="color: #FD971F">col</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> pair_usdt)) </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> geom_line()</span></span>
<span class="line"><span style="color: #F8F8F2">p </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> p </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> facet_wrap(</span><span style="color: #F92672">~</span><span style="color: #F8F8F2">pair_usdt, </span><span style="color: #FD971F">scales</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;free&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #FD971F">ncol</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">3</span><span style="color: #F8F8F2">) </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> theme_minimal() </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> theme(</span><span style="color: #FD971F">legend.position</span><span style="color: #F92672">=</span><span style="color: #E6DB74">&quot;none&quot;</span><span style="color: #F8F8F2">) </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> ylab(</span><span style="color: #E6DB74">&quot;Price (USD)&quot;</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">p</span></span></code></pre></div>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="828" height="499" loading="lazy" src="https://analyticadss.com/wp-content/uploads/2022/12/1_lXmT2c5hQ6tik7sNfwvFPw.webp" alt="" class="wp-image-4910" srcset="https://analyticadss.com/wp-content/uploads/2022/12/1_lXmT2c5hQ6tik7sNfwvFPw.webp 828w, https://analyticadss.com/wp-content/uploads/2022/12/1_lXmT2c5hQ6tik7sNfwvFPw-500x301.webp 500w, https://analyticadss.com/wp-content/uploads/2022/12/1_lXmT2c5hQ6tik7sNfwvFPw-150x90.webp 150w, https://analyticadss.com/wp-content/uploads/2022/12/1_lXmT2c5hQ6tik7sNfwvFPw-768x463.webp 768w" sizes="auto, (max-width: 828px) 100vw, 828px" /><figcaption class="wp-element-caption">Prices of Bitcoin and other altcoins in 2018<br></figcaption></figure>
</div>


<p class="wp-block-paragraph" id="3bf6">The figure above shows that some coins seems to be more correlated with Bitcoin than others. The figure also shows that this variablity between Bitcoin and another coin varies over time. More on this below.</p>



<p class="wp-block-paragraph" id="73c0">Tyring to find correlations bewteen time series data using Pearson correlation coefficient or other metrics used with stationary data, time series is not a form of stationary data, can give misleading results. Similar trends in time series data can also be very misleading, a nice article on this topic can be found <a href="https://svds.com/avoiding-common-mistakes-with-time-series/" rel="noreferrer noopener" target="_blank">here</a>. And always remember that <strong>Correlation doesn’t guarantee Causation</strong></p>



<p class="wp-block-paragraph" id="0f96">Bottom line is the following, one has to be careful when cross-correlating time serice. In order to perform proper correlation analysis we need to add some new variables to our table.</p>



<h2 class="wp-block-heading" id="510f">Percentage Daily Change</h2>



<p class="wp-block-paragraph" id="0ed2">Percentage daily change calculates the price change of a coin over a period of a day. Let’s add that to the table. Notice that we are calcualting this variable using the USD price, and not the price in Bitcoin.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#F8F8F2;--cbp-line-number-width:7.704864501953125px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#272822"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="# add daily price change
alt_data[, pct_change := Delt(price_usdt), by = pair_usdt]" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki" style="background-color: #272822"><code><span class="line"><span style="color: #88846F"># add daily price change</span></span>
<span class="line"><span style="color: #F8F8F2">alt_data[, pct_change </span><span style="color: #F92672">:=</span><span style="color: #F8F8F2"> Delt(price_usdt), </span><span style="color: #FD971F">by</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> pair_usdt]</span></span></code></pre></div>



<h2 class="wp-block-heading" id="a775">Normalized Price in USD</h2>



<p class="wp-block-paragraph" id="a42d">Since the prices vary a lot, both overtime for the same coin and between coins, we will add a variable of the normalized price in USD. This variable will make it easy to plot prices of coins on the same figure.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#F8F8F2;--cbp-line-number-width:7.70489501953125px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#272822"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="# add normalized prices in udst
alt_data[, price_usdt_norm := price_usdt/max(price_usdt), by = pair_usdt]" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki" style="background-color: #272822"><code><span class="line"><span style="color: #88846F"># add normalized prices in udst</span></span>
<span class="line"><span style="color: #F8F8F2">alt_data[, price_usdt_norm </span><span style="color: #F92672">:=</span><span style="color: #F8F8F2"> price_usdt</span><span style="color: #F92672">/</span><span style="color: #66D9EF">max</span><span style="color: #F8F8F2">(price_usdt), </span><span style="color: #FD971F">by</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> pair_usdt]</span></span></code></pre></div>



<p class="wp-block-paragraph" id="9cec">Now that we have the normalized prices in USD, let’s look at the prices of bitcoin and litcoin on the same figure. We’ll do that for 2018 so we can better see any possible correlations.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#F8F8F2;--cbp-line-number-width:7.704864501953125px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#272822"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="p <- ggplot(alt_data[year(Date) == 2018 & pair_usdt %like% &quot;BTC|LTC&quot;], aes(x = Date, y =  price_usdt_norm, col = pair_usdt)) + geom_line()
p <- p + theme_minimal() + ylab(&quot;Price (USD)&quot;)
ggplotly(p)" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki" style="background-color: #272822"><code><span class="line"><span style="color: #F8F8F2">p </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> ggplot(alt_data[year(Date) </span><span style="color: #F92672">==</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">2018</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">&</span><span style="color: #F8F8F2"> pair_usdt </span><span style="color: #F92672">%like%</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;BTC|LTC&quot;</span><span style="color: #F8F8F2">], aes(</span><span style="color: #FD971F">x</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> Date, </span><span style="color: #FD971F">y</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2">  price_usdt_norm, </span><span style="color: #FD971F">col</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> pair_usdt)) </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> geom_line()</span></span>
<span class="line"><span style="color: #F8F8F2">p </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> p </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> theme_minimal() </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> ylab(</span><span style="color: #E6DB74">&quot;Price (USD)&quot;</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">ggplotly(p)</span></span></code></pre></div>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="828" height="483" loading="lazy" src="https://analyticadss.com/wp-content/uploads/2022/12/1_HHs59LsNRYT3sbL8I0Sdmg.webp" alt="" class="wp-image-4913" srcset="https://analyticadss.com/wp-content/uploads/2022/12/1_HHs59LsNRYT3sbL8I0Sdmg.webp 828w, https://analyticadss.com/wp-content/uploads/2022/12/1_HHs59LsNRYT3sbL8I0Sdmg-500x292.webp 500w, https://analyticadss.com/wp-content/uploads/2022/12/1_HHs59LsNRYT3sbL8I0Sdmg-150x88.webp 150w, https://analyticadss.com/wp-content/uploads/2022/12/1_HHs59LsNRYT3sbL8I0Sdmg-768x448.webp 768w" sizes="auto, (max-width: 828px) 100vw, 828px" /></figure>
</div>


<p class="wp-block-paragraph">The trends in the prices of BTC and LTC are very similar, Let’s look at price trends for 2017</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#F8F8F2;--cbp-line-number-width:7.704864501953125px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#272822"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="p <- ggplot(alt_data[year(Date) == 2017 & pair_usdt %like% &quot;BTC|LTC&quot;], aes(x = Date, y =  price_usdt_norm, col = pair_usdt)) + geom_line()
p <- p + theme_minimal() + ylab(&quot;Price (USD)&quot;)
ggplotly(p)" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki" style="background-color: #272822"><code><span class="line"><span style="color: #F8F8F2">p </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> ggplot(alt_data[year(Date) </span><span style="color: #F92672">==</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">2017</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">&</span><span style="color: #F8F8F2"> pair_usdt </span><span style="color: #F92672">%like%</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;BTC|LTC&quot;</span><span style="color: #F8F8F2">], aes(</span><span style="color: #FD971F">x</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> Date, </span><span style="color: #FD971F">y</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2">  price_usdt_norm, </span><span style="color: #FD971F">col</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> pair_usdt)) </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> geom_line()</span></span>
<span class="line"><span style="color: #F8F8F2">p </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> p </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> theme_minimal() </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> ylab(</span><span style="color: #E6DB74">&quot;Price (USD)&quot;</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">ggplotly(p)</span></span></code></pre></div>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="828" height="480" loading="lazy" src="https://analyticadss.com/wp-content/uploads/2022/12/1_6t9j_F4QaweBgp_Wl7Ntwg.webp" alt="" class="wp-image-4911" srcset="https://analyticadss.com/wp-content/uploads/2022/12/1_6t9j_F4QaweBgp_Wl7Ntwg.webp 828w, https://analyticadss.com/wp-content/uploads/2022/12/1_6t9j_F4QaweBgp_Wl7Ntwg-500x290.webp 500w, https://analyticadss.com/wp-content/uploads/2022/12/1_6t9j_F4QaweBgp_Wl7Ntwg-150x87.webp 150w, https://analyticadss.com/wp-content/uploads/2022/12/1_6t9j_F4QaweBgp_Wl7Ntwg-768x445.webp 768w" sizes="auto, (max-width: 828px) 100vw, 828px" /><figcaption class="wp-element-caption">Prices of Bitcoin and LTC in 2017</figcaption></figure>
</div>


<p class="wp-block-paragraph" id="c49a">Seems like we need to zoon in on the last quarter of 2017, let’s do that</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#F8F8F2;--cbp-line-number-width:7.704862594604492px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#272822"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="p <- ggplot(alt_data[Date >= &quot;2017-10-01&quot;  & Date < &quot;2018-01-01&quot; &  pair_usdt %like% &quot;BTC|LTC&quot;], aes(x = Date, y =  price_usdt_norm, col = pair_usdt)) + geom_line()
p <- p + theme_minimal() + ylab(&quot;Price (USD)&quot;)
ggplotly(p)
" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki" style="background-color: #272822"><code><span class="line"><span style="color: #F8F8F2">p </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> ggplot(alt_data[Date </span><span style="color: #F92672">>=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;2017-10-01&quot;</span><span style="color: #F8F8F2">  </span><span style="color: #F92672">&</span><span style="color: #F8F8F2"> Date </span><span style="color: #F92672"><</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;2018-01-01&quot;</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">&</span><span style="color: #F8F8F2">  pair_usdt </span><span style="color: #F92672">%like%</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;BTC|LTC&quot;</span><span style="color: #F8F8F2">], aes(</span><span style="color: #FD971F">x</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> Date, </span><span style="color: #FD971F">y</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2">  price_usdt_norm, </span><span style="color: #FD971F">col</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> pair_usdt)) </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> geom_line()</span></span>
<span class="line"><span style="color: #F8F8F2">p </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> p </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> theme_minimal() </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> ylab(</span><span style="color: #E6DB74">&quot;Price (USD)&quot;</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">ggplotly(p)</span></span>
<span class="line"></span></code></pre></div>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="828" height="490" loading="lazy" src="https://analyticadss.com/wp-content/uploads/2022/12/1_yEZOrUPaK-ZhizMdmeFCmA.webp" alt="" class="wp-image-4914" srcset="https://analyticadss.com/wp-content/uploads/2022/12/1_yEZOrUPaK-ZhizMdmeFCmA.webp 828w, https://analyticadss.com/wp-content/uploads/2022/12/1_yEZOrUPaK-ZhizMdmeFCmA-500x296.webp 500w, https://analyticadss.com/wp-content/uploads/2022/12/1_yEZOrUPaK-ZhizMdmeFCmA-150x89.webp 150w, https://analyticadss.com/wp-content/uploads/2022/12/1_yEZOrUPaK-ZhizMdmeFCmA-768x454.webp 768w" sizes="auto, (max-width: 828px) 100vw, 828px" /><figcaption class="wp-element-caption">Prices of Bitcoin and LTC in the last quarter of 2017<br></figcaption></figure>
</div>


<p class="wp-block-paragraph" id="fcd8">It is clear from the above figure that the correlation in the prices of bitcoin and LTC vary over time. Note how the highest price for bitcoin on December 17 2017, preceded that of LTC by two days, which occurred on December 19 2017. This wasn’t the case for the ATH which occurred on January 6th 2018 for both coins.</p>



<h2 class="wp-block-heading" id="6009">Static Correlations </h2>



<h3 class="wp-block-heading" id="6009">(and why you shouldn’t use them with crypto!)</h3>



<p class="wp-block-paragraph" id="9035">Up until now I haven’t calculated any correltaions between the price of different coins. You might ask why should we even care about correlations in time series. Well, in the case of financial time series data, if one can show that a correlation exists between two time series then one can use this correlation to model/predict the price movement of one coin/stock given the price trends of another coin/stock. </p>



<p class="wp-block-paragraph" id="9035"><strong>However</strong>, as we mentioned earlier, correlation for time series data is not static, it changes over time. Actually let’s show that. To do that I am going to be calculating the <a href="https://en.wikipedia.org/wiki/Pearson_correlation_coefficient" target="_blank" rel="noreferrer noopener">Pearson correlation coefficient</a>. In simple words, Pearson correlation coefficient for two vectors of data is a measure that shows how correlated these two vectors of data are. The value of this coefficient varies from -1, perfectly anti-correlated, to 1, perfectly correlated. So the correlation coefficient for a series of numbers on itself is 1. A value of zero means these is no correlation. Remember, this only works for static data.</p>



<p class="wp-block-paragraph" id="62e8">In order to perform correlation on our data I am going to need to do some data transformation:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#F8F8F2;--cbp-line-number-width:7.70489501953125px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#272822"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="# subset data, only keep the date, the pair, and the price
alt_data_sub <- alt_data[, .(Date, pair_usdt, price_usdt)]
# convert to wide format 
alt_data_sub <- spread(data = alt_data_sub, key = &quot;pair_usdt&quot;, value = &quot;price_usdt&quot;)
# clean column names
setnames(alt_data_sub, gsub(&quot;USDT_&quot;, &quot;&quot;, colnames(alt_data_sub)))" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki" style="background-color: #272822"><code><span class="line"><span style="color: #88846F"># subset data, only keep the date, the pair, and the price</span></span>
<span class="line"><span style="color: #F8F8F2">alt_data_sub </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> alt_data[, .(Date, pair_usdt, price_usdt)]</span></span>
<span class="line"><span style="color: #88846F"># convert to wide format </span></span>
<span class="line"><span style="color: #F8F8F2">alt_data_sub </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> spread(</span><span style="color: #FD971F">data</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> alt_data_sub, </span><span style="color: #FD971F">key</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;pair_usdt&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #FD971F">value</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;price_usdt&quot;</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #88846F"># clean column names</span></span>
<span class="line"><span style="color: #F8F8F2">setnames(alt_data_sub, </span><span style="color: #66D9EF">gsub</span><span style="color: #F8F8F2">(</span><span style="color: #E6DB74">&quot;USDT_&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&quot;&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #66D9EF">colnames</span><span style="color: #F8F8F2">(alt_data_sub)))</span></span></code></pre></div>



<p class="wp-block-paragraph">The new table we created contains the date along with the prices in USDT for each coin we have in our table.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#F8F8F2;--cbp-line-number-width:7.7048492431640625px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#272822"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="tail(alt_data_sub)" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki" style="background-color: #272822"><code><span class="line"><span style="color: #66D9EF">tail</span><span style="color: #F8F8F2">(alt_data_sub)</span></span></code></pre></div>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="828" height="236" loading="lazy" src="https://analyticadss.com/wp-content/uploads/2022/12/1_3onSqsAzx-A1jNaprt9BMw.webp" alt="" class="wp-image-4915" srcset="https://analyticadss.com/wp-content/uploads/2022/12/1_3onSqsAzx-A1jNaprt9BMw.webp 828w, https://analyticadss.com/wp-content/uploads/2022/12/1_3onSqsAzx-A1jNaprt9BMw-500x143.webp 500w, https://analyticadss.com/wp-content/uploads/2022/12/1_3onSqsAzx-A1jNaprt9BMw-150x43.webp 150w, https://analyticadss.com/wp-content/uploads/2022/12/1_3onSqsAzx-A1jNaprt9BMw-768x219.webp 768w" sizes="auto, (max-width: 828px) 100vw, 828px" /></figure>
</div>


<p class="wp-block-paragraph">Again, what I am doing here is not correct, I am just trying to show you why we shouldn’t be doing static correlations on crypto data. Now we’ll calculate the Pearson correlation coefficient between the coins we have, then we are going to make a nice plot of these coefficients.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#F8F8F2;--cbp-line-number-width:7.704833984375px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#272822"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="# calculate the correlation matrix
M <- cor(alt_data_sub[, -1], use = &quot;complete.obs&quot;) # notice how we are ignoring missing data with the last argument
# plot the correlation matrix
corrplot.mixed(corr = M, upper = &quot;ellipse&quot;, lower = &quot;number&quot;, order = &quot;AOE&quot;, tl.col = &quot;black&quot;)" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki" style="background-color: #272822"><code><span class="line"><span style="color: #88846F"># calculate the correlation matrix</span></span>
<span class="line"><span style="color: #F8F8F2">M </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">cor</span><span style="color: #F8F8F2">(alt_data_sub[, </span><span style="color: #F92672">-</span><span style="color: #AE81FF">1</span><span style="color: #F8F8F2">], </span><span style="color: #FD971F">use</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;complete.obs&quot;</span><span style="color: #F8F8F2">) </span><span style="color: #88846F"># notice how we are ignoring missing data with the last argument</span></span>
<span class="line"><span style="color: #88846F"># plot the correlation matrix</span></span>
<span class="line"><span style="color: #F8F8F2">corrplot.mixed(</span><span style="color: #FD971F">corr</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> M, </span><span style="color: #FD971F">upper</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;ellipse&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #FD971F">lower</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;number&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #FD971F">order</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;AOE&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #FD971F">tl.col</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;black&quot;</span><span style="color: #F8F8F2">)</span></span></code></pre></div>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="826" height="722" loading="lazy" src="https://analyticadss.com/wp-content/uploads/2022/12/1_mjAaFbtDf5GeYp6RCppeSA.webp" alt="" class="wp-image-4916" srcset="https://analyticadss.com/wp-content/uploads/2022/12/1_mjAaFbtDf5GeYp6RCppeSA.webp 826w, https://analyticadss.com/wp-content/uploads/2022/12/1_mjAaFbtDf5GeYp6RCppeSA-500x437.webp 500w, https://analyticadss.com/wp-content/uploads/2022/12/1_mjAaFbtDf5GeYp6RCppeSA-150x131.webp 150w, https://analyticadss.com/wp-content/uploads/2022/12/1_mjAaFbtDf5GeYp6RCppeSA-768x671.webp 768w" sizes="auto, (max-width: 826px) 100vw, 826px" /></figure>
</div>


<p class="wp-block-paragraph" id="99ac">The figure above shows the correlation coefficients between the different coins. It is easy to read, visually, the darker the color of the ellipse, and the more diagonal the ellipse, the higher the correlation coefficient. Of course you can also just look at the numbers on the bottom left part of the figure to get the value of the coefficient between two coins :). The figure shows how highly correlated the prices of crypto currencies can be. For example XRP and XEM have a correlation coefficient of 0.93. The highest correlation seems to be between BCH and DASH at 0.97 correlation coefficient.</p>



<p class="wp-block-paragraph" id="bcf4">All of the correlation coefficient we see in the above figure are significant, the question is, do these correlations vary over time. To answer this question I will calculate the correlation coefficient between Bitcoin and DASH on a monthly basis, you can do that for any time period, and will show that this coefficient varies greatly over time. Let’s do that</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#F8F8F2;--cbp-line-number-width:15.395828247070312px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#272822"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="# subset the data
btc_dash <- alt_data_sub[, .(Date, BTC, DASH)]
# add a year_month column
btc_dash[, year_month := as.yearmon(Date)]
# calculate the correlation coefficient on montly basis
btc_dash_2 <- btc_dash[, cor(BTC, DASH), by = year_month]
# now plot the correlation coefficient as a function of month and year
plot(btc_dash_2$year_month, btc_dash_2$V1, xlab = &quot;Year-Month&quot;, main = &quot;Correlation Coeff. Between BTC and DASH Over time&quot;
     , ylab = &quot;Correlation Coefficient&quot;, type = &quot;b&quot;, pch = 19, col = ifelse(btc_dash_2$V1 > 0, &quot;blue&quot;, &quot;red&quot;)
     , ylim = c(-1, 1))" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki" style="background-color: #272822"><code><span class="line"><span style="color: #88846F"># subset the data</span></span>
<span class="line"><span style="color: #F8F8F2">btc_dash </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> alt_data_sub[, .(Date, BTC, DASH)]</span></span>
<span class="line"><span style="color: #88846F"># add a year_month column</span></span>
<span class="line"><span style="color: #F8F8F2">btc_dash[, year_month </span><span style="color: #F92672">:=</span><span style="color: #F8F8F2"> as.yearmon(Date)]</span></span>
<span class="line"><span style="color: #88846F"># calculate the correlation coefficient on montly basis</span></span>
<span class="line"><span style="color: #F8F8F2">btc_dash_2 </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> btc_dash[, </span><span style="color: #66D9EF">cor</span><span style="color: #F8F8F2">(BTC, DASH), </span><span style="color: #FD971F">by</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> year_month]</span></span>
<span class="line"><span style="color: #88846F"># now plot the correlation coefficient as a function of month and year</span></span>
<span class="line"><span style="color: #66D9EF">plot</span><span style="color: #F8F8F2">(btc_dash_2</span><span style="color: #F92672">$</span><span style="color: #F8F8F2">year_month, btc_dash_2</span><span style="color: #F92672">$</span><span style="color: #F8F8F2">V1, </span><span style="color: #FD971F">xlab</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;Year-Month&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #FD971F">main</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;Correlation Coeff. Between BTC and DASH Over time&quot;</span></span>
<span class="line"><span style="color: #F8F8F2">     , </span><span style="color: #FD971F">ylab</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;Correlation Coefficient&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #FD971F">type</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;b&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #FD971F">pch</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">19</span><span style="color: #F8F8F2">, </span><span style="color: #FD971F">col</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">ifelse</span><span style="color: #F8F8F2">(btc_dash_2</span><span style="color: #F92672">$</span><span style="color: #F8F8F2">V1 </span><span style="color: #F92672">></span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">0</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&quot;blue&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&quot;red&quot;</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">     , </span><span style="color: #FD971F">ylim</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">c</span><span style="color: #F8F8F2">(</span><span style="color: #F92672">-</span><span style="color: #AE81FF">1</span><span style="color: #F8F8F2">, </span><span style="color: #AE81FF">1</span><span style="color: #F8F8F2">))</span></span></code></pre></div>



<p class="wp-block-paragraph" id="094a">This is interesting, the value of the monthly correlation coefficient between bitcoin and DASH varies between <strong>-0.91</strong>, highly anti-correlated, to <strong>0.98</strong>, highly correlated. And this is why <strong><em>you should never use static correlation metrics with crypto data!</em></strong></p>



<p class="wp-block-paragraph" id="78e2">A good blog post on this same topic is written by <a href="https://twitter.com/tomeff" rel="noreferrer noopener" target="_blank">Tom Fawcett</a> from <a href="https://www.svds.com/" rel="noreferrer noopener" target="_blank">Silicon Valley Data Science</a> and can be found <a href="https://www.svds.com/avoiding-common-mistakes-with-time-series/" rel="noreferrer noopener" target="_blank">here</a>. In his post Tom shows, with simple simulations, why static correlations should never be used with time series.</p>



<h3 class="wp-block-heading" id="2441">Correlation Networks</h3>



<p class="wp-block-paragraph" id="a16e">There is one more plot I would like to make, which is a network plot of the correlations between the different coins. The correlation network plot helps show strengths of correlation between the different coins. Agian, these correlations are time dependent and the figure we will be making will change over time, but I still think it is a good figure to make. Here it is:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#F8F8F2;--cbp-line-number-width:15.395835876464844px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#272822"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="# we will be using the great corrr package for this work
# get the correlation matrix, just like we did before
# build the correlation matrix 
# the code snippets below are taken from, that is a great blog BTW 
# http://www.business-science.io/timeseries-analysis/2017/07/30/tidy-timeseries-analysis-pt-3.html
corr_2 <- correlate(alt_data_sub[, -1])
# make the network plot
# Network plot
corr_net <- corr_2 %>%
  network_plot(colours = c(palette_light()[[2]], &quot;white&quot;, palette_light()[[4]]), legend = TRUE) +
  labs(
    title = &quot;Static Correlations of some Crypto Currencies&quot;,
    subtitle = &quot;2014 through 2018&quot;
  ) +
  theme_tq() +
  theme(legend.position = &quot;bottom&quot;)
corr_net" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki" style="background-color: #272822"><code><span class="line"><span style="color: #88846F"># we will be using the great corrr package for this work</span></span>
<span class="line"><span style="color: #88846F"># get the correlation matrix, just like we did before</span></span>
<span class="line"><span style="color: #88846F"># build the correlation matrix </span></span>
<span class="line"><span style="color: #88846F"># the code snippets below are taken from, that is a great blog BTW </span></span>
<span class="line"><span style="color: #88846F"># http://www.business-science.io/timeseries-analysis/2017/07/30/tidy-timeseries-analysis-pt-3.html</span></span>
<span class="line"><span style="color: #F8F8F2">corr_2 </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> correlate(alt_data_sub[, </span><span style="color: #F92672">-</span><span style="color: #AE81FF">1</span><span style="color: #F8F8F2">])</span></span>
<span class="line"><span style="color: #88846F"># make the network plot</span></span>
<span class="line"><span style="color: #88846F"># Network plot</span></span>
<span class="line"><span style="color: #F8F8F2">corr_net </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> corr_2 </span><span style="color: #F92672">%>%</span></span>
<span class="line"><span style="color: #F8F8F2">  network_plot(</span><span style="color: #FD971F">colours</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">c</span><span style="color: #F8F8F2">(palette_light()[[</span><span style="color: #AE81FF">2</span><span style="color: #F8F8F2">]], </span><span style="color: #E6DB74">&quot;white&quot;</span><span style="color: #F8F8F2">, palette_light()[[</span><span style="color: #AE81FF">4</span><span style="color: #F8F8F2">]]), </span><span style="color: #FD971F">legend</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">TRUE</span><span style="color: #F8F8F2">) </span><span style="color: #F92672">+</span></span>
<span class="line"><span style="color: #F8F8F2">  labs(</span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #FD971F">title</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;Static Correlations of some Crypto Currencies&quot;</span><span style="color: #F8F8F2">,</span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #FD971F">subtitle</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;2014 through 2018&quot;</span></span>
<span class="line"><span style="color: #F8F8F2">  ) </span><span style="color: #F92672">+</span></span>
<span class="line"><span style="color: #F8F8F2">  theme_tq() </span><span style="color: #F92672">+</span></span>
<span class="line"><span style="color: #F8F8F2">  theme(</span><span style="color: #FD971F">legend.position</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;bottom&quot;</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">corr_net</span></span></code></pre></div>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="828" height="519" loading="lazy" src="https://analyticadss.com/wp-content/uploads/2022/12/1_brRhP1L95H00I6m3TcD-2A.webp" alt="" class="wp-image-4917" srcset="https://analyticadss.com/wp-content/uploads/2022/12/1_brRhP1L95H00I6m3TcD-2A.webp 828w, https://analyticadss.com/wp-content/uploads/2022/12/1_brRhP1L95H00I6m3TcD-2A-500x313.webp 500w, https://analyticadss.com/wp-content/uploads/2022/12/1_brRhP1L95H00I6m3TcD-2A-150x94.webp 150w, https://analyticadss.com/wp-content/uploads/2022/12/1_brRhP1L95H00I6m3TcD-2A-768x481.webp 768w" sizes="auto, (max-width: 828px) 100vw, 828px" /></figure>
</div>


<p class="wp-block-paragraph" id="9225">The figure above shows a network which measures how strongly correlated the prices of the coins under stugy are. The darker the color of the edge, line, connecting two coins and the closer they are in the network the stronger the correlation between these two coins.</p>



<p class="wp-block-paragraph" id="851b">From the figure, it seems like XMR, LTC, and BTC are in the heart of this network, while BCH seems to be the least correlated with the rest of the coins. Let’s see how the network plot changes between 2017 and 2018:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#F8F8F2;--cbp-line-number-width:15.395835876464844px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#272822"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="# subset the data and get correlation matrices
corr_2017 <- correlate(alt_data_sub[year(Date) == 2017][, -1])
corr_2018 <- correlate(alt_data_sub[year(Date) == 2018][, -1])
# build Network plots
corr_net_2017 <- corr_2017 %>%
  network_plot(colours = c(palette_light()[[2]], &quot;white&quot;, palette_light()[[4]]), legend = TRUE) +
  labs(
    title = &quot;Static Correlations of some Crypto Currencies&quot;,
    subtitle = &quot;2017&quot;
  ) +
  theme_tq() +
  theme(legend.position = &quot;bottom&quot;)
corr_net_2018 <- corr_2018 %>%
  network_plot(colours = c(palette_light()[[2]], &quot;white&quot;, palette_light()[[4]]), legend = TRUE) +
  labs(
    title = &quot;Static Correlations of some Crypto Currencies&quot;,
    subtitle = &quot;2018&quot;
  ) +
  theme_tq() +
  theme(legend.position = &quot;bottom&quot;)
# combine network plots
cow_net_plots <-plot_grid(corr_net_2017, corr_net_2018, ncol = 2)
title <- ggdraw() + 
    draw_label(label = 'Crypto Correlation Networks',
               fontface = 'bold', size = 18)
cow_out <- plot_grid(title, cow_net_plots, ncol=1, rel_heights=c(0.1, 1))
cow_out" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki" style="background-color: #272822"><code><span class="line"><span style="color: #88846F"># subset the data and get correlation matrices</span></span>
<span class="line"><span style="color: #F8F8F2">corr_2017 </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> correlate(alt_data_sub[year(Date) </span><span style="color: #F92672">==</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">2017</span><span style="color: #F8F8F2">][, </span><span style="color: #F92672">-</span><span style="color: #AE81FF">1</span><span style="color: #F8F8F2">])</span></span>
<span class="line"><span style="color: #F8F8F2">corr_2018 </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> correlate(alt_data_sub[year(Date) </span><span style="color: #F92672">==</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">2018</span><span style="color: #F8F8F2">][, </span><span style="color: #F92672">-</span><span style="color: #AE81FF">1</span><span style="color: #F8F8F2">])</span></span>
<span class="line"><span style="color: #88846F"># build Network plots</span></span>
<span class="line"><span style="color: #F8F8F2">corr_net_2017 </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> corr_2017 </span><span style="color: #F92672">%>%</span></span>
<span class="line"><span style="color: #F8F8F2">  network_plot(</span><span style="color: #FD971F">colours</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">c</span><span style="color: #F8F8F2">(palette_light()[[</span><span style="color: #AE81FF">2</span><span style="color: #F8F8F2">]], </span><span style="color: #E6DB74">&quot;white&quot;</span><span style="color: #F8F8F2">, palette_light()[[</span><span style="color: #AE81FF">4</span><span style="color: #F8F8F2">]]), </span><span style="color: #FD971F">legend</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">TRUE</span><span style="color: #F8F8F2">) </span><span style="color: #F92672">+</span></span>
<span class="line"><span style="color: #F8F8F2">  labs(</span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #FD971F">title</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;Static Correlations of some Crypto Currencies&quot;</span><span style="color: #F8F8F2">,</span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #FD971F">subtitle</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;2017&quot;</span></span>
<span class="line"><span style="color: #F8F8F2">  ) </span><span style="color: #F92672">+</span></span>
<span class="line"><span style="color: #F8F8F2">  theme_tq() </span><span style="color: #F92672">+</span></span>
<span class="line"><span style="color: #F8F8F2">  theme(</span><span style="color: #FD971F">legend.position</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;bottom&quot;</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">corr_net_2018 </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> corr_2018 </span><span style="color: #F92672">%>%</span></span>
<span class="line"><span style="color: #F8F8F2">  network_plot(</span><span style="color: #FD971F">colours</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">c</span><span style="color: #F8F8F2">(palette_light()[[</span><span style="color: #AE81FF">2</span><span style="color: #F8F8F2">]], </span><span style="color: #E6DB74">&quot;white&quot;</span><span style="color: #F8F8F2">, palette_light()[[</span><span style="color: #AE81FF">4</span><span style="color: #F8F8F2">]]), </span><span style="color: #FD971F">legend</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">TRUE</span><span style="color: #F8F8F2">) </span><span style="color: #F92672">+</span></span>
<span class="line"><span style="color: #F8F8F2">  labs(</span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #FD971F">title</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;Static Correlations of some Crypto Currencies&quot;</span><span style="color: #F8F8F2">,</span></span>
<span class="line"><span style="color: #F8F8F2">    </span><span style="color: #FD971F">subtitle</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;2018&quot;</span></span>
<span class="line"><span style="color: #F8F8F2">  ) </span><span style="color: #F92672">+</span></span>
<span class="line"><span style="color: #F8F8F2">  theme_tq() </span><span style="color: #F92672">+</span></span>
<span class="line"><span style="color: #F8F8F2">  theme(</span><span style="color: #FD971F">legend.position</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;bottom&quot;</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #88846F"># combine network plots</span></span>
<span class="line"><span style="color: #F8F8F2">cow_net_plots </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2">plot_grid(corr_net_2017, corr_net_2018, </span><span style="color: #FD971F">ncol</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">2</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">title </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> ggdraw() </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> </span></span>
<span class="line"><span style="color: #F8F8F2">    draw_label(</span><span style="color: #FD971F">label</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&#39;Crypto Correlation Networks&#39;</span><span style="color: #F8F8F2">,</span></span>
<span class="line"><span style="color: #F8F8F2">               </span><span style="color: #FD971F">fontface</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&#39;bold&#39;</span><span style="color: #F8F8F2">, </span><span style="color: #FD971F">size</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">18</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">cow_out </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> plot_grid(title, cow_net_plots, </span><span style="color: #FD971F">ncol</span><span style="color: #F92672">=</span><span style="color: #AE81FF">1</span><span style="color: #F8F8F2">, </span><span style="color: #FD971F">rel_heights</span><span style="color: #F92672">=</span><span style="color: #66D9EF">c</span><span style="color: #F8F8F2">(</span><span style="color: #AE81FF">0.1</span><span style="color: #F8F8F2">, </span><span style="color: #AE81FF">1</span><span style="color: #F8F8F2">))</span></span>
<span class="line"><span style="color: #F8F8F2">cow_out</span></span></code></pre></div>



<p class="wp-block-paragraph" id="058c">As can be seen, the correlation networks do change overtime. This is not news since we already saw in the previous section that the value of the correlation varies overtime (I know we showed this to be true for the BTC-DASH air but we’ll show that this is true for the rest of the coins in the next section.)</p>



<h3 class="wp-block-heading" id="eace">Daily Returns Correlations</h3>



<p class="wp-block-paragraph" id="ffb8">Let’s look at the percentage daily changes of the altcoins between 2015 and today.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#F8F8F2;--cbp-line-number-width:7.704864501953125px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#272822"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="# plot the percent changes
p <- ggplot(alt_data[Date > ymd(&quot;2015-01-01&quot;)], aes(x = Date, y =  (100*pct_change), col = pair_usdt)) + geom_line()
p <- p + ggtitle(&quot;% Daily Returns over time&quot;) + ylab(&quot;Daily Return (%)&quot;) 
p <- p + theme_bw() + guides(col=guide_legend(title=&quot;Coin Pair&quot;))
ggplotly(p)" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki" style="background-color: #272822"><code><span class="line"><span style="color: #88846F"># plot the percent changes</span></span>
<span class="line"><span style="color: #F8F8F2">p </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> ggplot(alt_data[Date </span><span style="color: #F92672">></span><span style="color: #F8F8F2"> ymd(</span><span style="color: #E6DB74">&quot;2015-01-01&quot;</span><span style="color: #F8F8F2">)], aes(</span><span style="color: #FD971F">x</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> Date, </span><span style="color: #FD971F">y</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2">  (</span><span style="color: #AE81FF">100</span><span style="color: #F92672">*</span><span style="color: #F8F8F2">pct_change), </span><span style="color: #FD971F">col</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> pair_usdt)) </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> geom_line()</span></span>
<span class="line"><span style="color: #F8F8F2">p </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> p </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> ggtitle(</span><span style="color: #E6DB74">&quot;% Daily Returns over time&quot;</span><span style="color: #F8F8F2">) </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> ylab(</span><span style="color: #E6DB74">&quot;Daily Return (%)&quot;</span><span style="color: #F8F8F2">) </span></span>
<span class="line"><span style="color: #F8F8F2">p </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> p </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> theme_bw() </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> guides(</span><span style="color: #FD971F">col</span><span style="color: #F92672">=</span><span style="color: #F8F8F2">guide_legend(</span><span style="color: #FD971F">title</span><span style="color: #F92672">=</span><span style="color: #E6DB74">&quot;Coin Pair&quot;</span><span style="color: #F8F8F2">))</span></span>
<span class="line"><span style="color: #F8F8F2">ggplotly(p)</span></span></code></pre></div>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="828" height="492" loading="lazy" src="https://analyticadss.com/wp-content/uploads/2022/12/1_DF6wejykPy9QxICaxujyWg.webp" alt="" class="wp-image-4918" srcset="https://analyticadss.com/wp-content/uploads/2022/12/1_DF6wejykPy9QxICaxujyWg.webp 828w, https://analyticadss.com/wp-content/uploads/2022/12/1_DF6wejykPy9QxICaxujyWg-500x297.webp 500w, https://analyticadss.com/wp-content/uploads/2022/12/1_DF6wejykPy9QxICaxujyWg-150x89.webp 150w, https://analyticadss.com/wp-content/uploads/2022/12/1_DF6wejykPy9QxICaxujyWg-768x456.webp 768w" sizes="auto, (max-width: 828px) 100vw, 828px" /><figcaption class="wp-element-caption">Percentage daily returns for some coins</figcaption></figure>
</div>


<p class="wp-block-paragraph" id="def1">Although the above figure is very cluttered, one thing is certain, percentage daily returns vary greatly for crypto. Let’s try to make this figure a bit easier to read</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#F8F8F2;--cbp-line-number-width:7.704864501953125px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#272822"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="p <- ggplot(alt_data[Date > ymd(&quot;2015-01-01&quot;)], aes(x = Date, y =  (100*pct_change), col = pair_usdt)) + geom_line() + facet_wrap(~ pair_usdt)
p <- p + ggtitle(&quot;Percentage Daily Returns over time&quot;) + ylab(&quot;Daily Return (%)&quot;) 
p <- p + theme_bw() + theme(legend.position=&quot;none&quot;) 
ggplotly(p)" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki" style="background-color: #272822"><code><span class="line"><span style="color: #F8F8F2">p </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> ggplot(alt_data[Date </span><span style="color: #F92672">></span><span style="color: #F8F8F2"> ymd(</span><span style="color: #E6DB74">&quot;2015-01-01&quot;</span><span style="color: #F8F8F2">)], aes(</span><span style="color: #FD971F">x</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> Date, </span><span style="color: #FD971F">y</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2">  (</span><span style="color: #AE81FF">100</span><span style="color: #F92672">*</span><span style="color: #F8F8F2">pct_change), </span><span style="color: #FD971F">col</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> pair_usdt)) </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> geom_line() </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> facet_wrap(</span><span style="color: #F92672">~</span><span style="color: #F8F8F2"> pair_usdt)</span></span>
<span class="line"><span style="color: #F8F8F2">p </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> p </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> ggtitle(</span><span style="color: #E6DB74">&quot;Percentage Daily Returns over time&quot;</span><span style="color: #F8F8F2">) </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> ylab(</span><span style="color: #E6DB74">&quot;Daily Return (%)&quot;</span><span style="color: #F8F8F2">) </span></span>
<span class="line"><span style="color: #F8F8F2">p </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> p </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> theme_bw() </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> theme(</span><span style="color: #FD971F">legend.position</span><span style="color: #F92672">=</span><span style="color: #E6DB74">&quot;none&quot;</span><span style="color: #F8F8F2">) </span></span>
<span class="line"><span style="color: #F8F8F2">ggplotly(p)</span></span></code></pre></div>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="828" height="563" loading="lazy" src="https://analyticadss.com/wp-content/uploads/2022/12/1_frUEGj6qn8sgyJOH87UsWA.webp" alt="" class="wp-image-4919" srcset="https://analyticadss.com/wp-content/uploads/2022/12/1_frUEGj6qn8sgyJOH87UsWA.webp 828w, https://analyticadss.com/wp-content/uploads/2022/12/1_frUEGj6qn8sgyJOH87UsWA-500x340.webp 500w, https://analyticadss.com/wp-content/uploads/2022/12/1_frUEGj6qn8sgyJOH87UsWA-150x102.webp 150w, https://analyticadss.com/wp-content/uploads/2022/12/1_frUEGj6qn8sgyJOH87UsWA-768x522.webp 768w" sizes="auto, (max-width: 828px) 100vw, 828px" /><figcaption class="wp-element-caption">Percentage daily returns for some coins</figcaption></figure>
</div>


<p class="wp-block-paragraph" id="dc7b">It is kind of surprising that Bitcoin has the least variability in daily returns. The nice big spike around April 2nd 2017 shows a percentage daily return of ~88% for XRP, this is the highest daily return I have seen!</p>



<p class="wp-block-paragraph" id="1715">Let’s look at the percentage daily returns for Bitcoin and Litecoin since they seem to be highly correlated. I am going to zoom in on the time period 2016–02–01 and 2016–05–01.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#F8F8F2;--cbp-line-number-width:7.70486307144165px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#272822"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="start_date <- ymd(&quot;2016-02-01&quot;)
end_date <- ymd(&quot;2016-05-01&quot;)
p <- ggplot(alt_data[pair_usdt %like% &quot;BTC|LTC&quot; & Date > start_date & Date < end_date], aes(x = Date, y =  (100*pct_change), col = pair_usdt)) + geom_line() + theme_bw() + ylab(&quot;Price (USD)&quot;)
p" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki" style="background-color: #272822"><code><span class="line"><span style="color: #F8F8F2">start_date </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> ymd(</span><span style="color: #E6DB74">&quot;2016-02-01&quot;</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">end_date </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> ymd(</span><span style="color: #E6DB74">&quot;2016-05-01&quot;</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">p </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> ggplot(alt_data[pair_usdt </span><span style="color: #F92672">%like%</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;BTC|LTC&quot;</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">&</span><span style="color: #F8F8F2"> Date </span><span style="color: #F92672">></span><span style="color: #F8F8F2"> start_date </span><span style="color: #F92672">&</span><span style="color: #F8F8F2"> Date </span><span style="color: #F92672"><</span><span style="color: #F8F8F2"> end_date], aes(</span><span style="color: #FD971F">x</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> Date, </span><span style="color: #FD971F">y</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2">  (</span><span style="color: #AE81FF">100</span><span style="color: #F92672">*</span><span style="color: #F8F8F2">pct_change), </span><span style="color: #FD971F">col</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> pair_usdt)) </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> geom_line() </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> theme_bw() </span><span style="color: #F92672">+</span><span style="color: #F8F8F2"> ylab(</span><span style="color: #E6DB74">&quot;Price (USD)&quot;</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">p</span></span></code></pre></div>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="828" height="480" loading="lazy" src="https://analyticadss.com/wp-content/uploads/2022/12/1_f0yWVUQ02uKmgNS3Euz8cA.webp" alt="" class="wp-image-4920" srcset="https://analyticadss.com/wp-content/uploads/2022/12/1_f0yWVUQ02uKmgNS3Euz8cA.webp 828w, https://analyticadss.com/wp-content/uploads/2022/12/1_f0yWVUQ02uKmgNS3Euz8cA-500x290.webp 500w, https://analyticadss.com/wp-content/uploads/2022/12/1_f0yWVUQ02uKmgNS3Euz8cA-150x87.webp 150w, https://analyticadss.com/wp-content/uploads/2022/12/1_f0yWVUQ02uKmgNS3Euz8cA-768x445.webp 768w" sizes="auto, (max-width: 828px) 100vw, 828px" /><figcaption class="wp-element-caption">Daily Return for Bitcoin and LTC in 2018</figcaption></figure>
</div>


<p class="wp-block-paragraph" id="db3e">The figure shows clear correlation between the daily returns of Bitcoin and litcoin. It also shows that these correlations can vary overtime. In fact, let’s look at how these correlations vary overtime.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#F8F8F2;--cbp-line-number-width:15.395828247070312px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#272822"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="# these steps are similar to the ones in the previous section, the only differnect is that now we are looking at the percentage change in price difference on daily basis instead of the actual price
# subset data, only keep the date, the pair, and the price
alt_data_sub_pct <- alt_data[, .(Date, pair_usdt, pct_change)]
# convert to wide format 
alt_data_sub_pct <- spread(data = alt_data_sub_pct, key = &quot;pair_usdt&quot;, value = &quot;pct_change&quot;)
# clean column names
setnames(alt_data_sub_pct, gsub(&quot;USDT_&quot;, &quot;&quot;, colnames(alt_data_sub)))
# subset the data
btc_ltc <- alt_data_sub_pct[, .(Date, BTC, LTC)]
# add a year_month column
btc_ltc[, year_month := as.yearmon(Date)]
# calculate the correlation coefficient on montly basis
btc_ltc_2 <- btc_ltc[, cor(BTC, LTC), by = year_month]
# now plot the correlation coefficient as a function of month and year
plot(btc_ltc_2$year_month, btc_ltc_2$V1, xlab = &quot;Year-Month&quot;, main = &quot;Correlation Coeff. Between Daily Returns of BTC and LTC Over time&quot;
     , ylab = &quot;Correlation Coefficient&quot;, type = &quot;b&quot;, pch = 19, col = ifelse(btc_ltc_2$V1 > 0, &quot;blue&quot;, &quot;red&quot;)
     , ylim = c(-1, 1))" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki" style="background-color: #272822"><code><span class="line"><span style="color: #88846F"># these steps are similar to the ones in the previous section, the only differnect is that now we are looking at the percentage change in price difference on daily basis instead of the actual price</span></span>
<span class="line"><span style="color: #88846F"># subset data, only keep the date, the pair, and the price</span></span>
<span class="line"><span style="color: #F8F8F2">alt_data_sub_pct </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> alt_data[, .(Date, pair_usdt, pct_change)]</span></span>
<span class="line"><span style="color: #88846F"># convert to wide format </span></span>
<span class="line"><span style="color: #F8F8F2">alt_data_sub_pct </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> spread(</span><span style="color: #FD971F">data</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> alt_data_sub_pct, </span><span style="color: #FD971F">key</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;pair_usdt&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #FD971F">value</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;pct_change&quot;</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #88846F"># clean column names</span></span>
<span class="line"><span style="color: #F8F8F2">setnames(alt_data_sub_pct, </span><span style="color: #66D9EF">gsub</span><span style="color: #F8F8F2">(</span><span style="color: #E6DB74">&quot;USDT_&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&quot;&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #66D9EF">colnames</span><span style="color: #F8F8F2">(alt_data_sub)))</span></span>
<span class="line"><span style="color: #88846F"># subset the data</span></span>
<span class="line"><span style="color: #F8F8F2">btc_ltc </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> alt_data_sub_pct[, .(Date, BTC, LTC)]</span></span>
<span class="line"><span style="color: #88846F"># add a year_month column</span></span>
<span class="line"><span style="color: #F8F8F2">btc_ltc[, year_month </span><span style="color: #F92672">:=</span><span style="color: #F8F8F2"> as.yearmon(Date)]</span></span>
<span class="line"><span style="color: #88846F"># calculate the correlation coefficient on montly basis</span></span>
<span class="line"><span style="color: #F8F8F2">btc_ltc_2 </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> btc_ltc[, </span><span style="color: #66D9EF">cor</span><span style="color: #F8F8F2">(BTC, LTC), </span><span style="color: #FD971F">by</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> year_month]</span></span>
<span class="line"><span style="color: #88846F"># now plot the correlation coefficient as a function of month and year</span></span>
<span class="line"><span style="color: #66D9EF">plot</span><span style="color: #F8F8F2">(btc_ltc_2</span><span style="color: #F92672">$</span><span style="color: #F8F8F2">year_month, btc_ltc_2</span><span style="color: #F92672">$</span><span style="color: #F8F8F2">V1, </span><span style="color: #FD971F">xlab</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;Year-Month&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #FD971F">main</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;Correlation Coeff. Between Daily Returns of BTC and LTC Over time&quot;</span></span>
<span class="line"><span style="color: #F8F8F2">     , </span><span style="color: #FD971F">ylab</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;Correlation Coefficient&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #FD971F">type</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;b&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #FD971F">pch</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">19</span><span style="color: #F8F8F2">, </span><span style="color: #FD971F">col</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">ifelse</span><span style="color: #F8F8F2">(btc_ltc_2</span><span style="color: #F92672">$</span><span style="color: #F8F8F2">V1 </span><span style="color: #F92672">></span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">0</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&quot;blue&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&quot;red&quot;</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">     , </span><span style="color: #FD971F">ylim</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">c</span><span style="color: #F8F8F2">(</span><span style="color: #F92672">-</span><span style="color: #AE81FF">1</span><span style="color: #F8F8F2">, </span><span style="color: #AE81FF">1</span><span style="color: #F8F8F2">))</span></span></code></pre></div>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="828" height="519" loading="lazy" src="https://analyticadss.com/wp-content/uploads/2022/12/1_YozYWrtQM_qHssvavSNHFw-1.webp" alt="" class="wp-image-4922" srcset="https://analyticadss.com/wp-content/uploads/2022/12/1_YozYWrtQM_qHssvavSNHFw-1.webp 828w, https://analyticadss.com/wp-content/uploads/2022/12/1_YozYWrtQM_qHssvavSNHFw-1-500x313.webp 500w, https://analyticadss.com/wp-content/uploads/2022/12/1_YozYWrtQM_qHssvavSNHFw-1-150x94.webp 150w, https://analyticadss.com/wp-content/uploads/2022/12/1_YozYWrtQM_qHssvavSNHFw-1-768x481.webp 768w" sizes="auto, (max-width: 828px) 100vw, 828px" /></figure>
</div>


<p class="wp-block-paragraph" id="62de">Interesting, the correlation between the percentage daily change of the prices for bitcoin and litecoin is much more on the positive side, we only have one month in which this correlation is negtive, barely negative. This is a lot different than what we saw between Bitcoin and DASH, but that was for the actual prices and not the daily returns. Let’s redo this plot but his time for the actual prices for bitcoin and litecoin, just like we did with DASH.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" style="font-size:.875rem;--cbp-line-number-color:#F8F8F2;--cbp-line-number-width:15.395828247070312px;line-height:1.25rem"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#272822"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="# subset the data
btc_ltc_price <- alt_data_sub[, .(Date, BTC, LTC)]
# add a year_month column
btc_ltc_price[, year_month := as.yearmon(Date)]
# calculate the correlation coefficient on montly basis
btc_ltc_price_2 <- btc_ltc_price[, cor(BTC, LTC), by = year_month]
# now plot the correlation coefficient as a function of month and year
plot(btc_ltc_price_2$year_month, btc_ltc_price_2$V1, xlab = &quot;Year-Month&quot;, main = &quot;Correlation Coeff. Between BTC and Litecoin Over time&quot;
     , ylab = &quot;Correlation Coefficient&quot;, type = &quot;b&quot;, pch = 19, col = ifelse(btc_ltc_price_2$V1 > 0, &quot;blue&quot;, &quot;red&quot;)
     , ylim = c(-1, 1))" style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki" style="background-color: #272822"><code><span class="line"><span style="color: #88846F"># subset the data</span></span>
<span class="line"><span style="color: #F8F8F2">btc_ltc_price </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> alt_data_sub[, .(Date, BTC, LTC)]</span></span>
<span class="line"><span style="color: #88846F"># add a year_month column</span></span>
<span class="line"><span style="color: #F8F8F2">btc_ltc_price[, year_month </span><span style="color: #F92672">:=</span><span style="color: #F8F8F2"> as.yearmon(Date)]</span></span>
<span class="line"><span style="color: #88846F"># calculate the correlation coefficient on montly basis</span></span>
<span class="line"><span style="color: #F8F8F2">btc_ltc_price_2 </span><span style="color: #F92672"><-</span><span style="color: #F8F8F2"> btc_ltc_price[, </span><span style="color: #66D9EF">cor</span><span style="color: #F8F8F2">(BTC, LTC), </span><span style="color: #FD971F">by</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> year_month]</span></span>
<span class="line"><span style="color: #88846F"># now plot the correlation coefficient as a function of month and year</span></span>
<span class="line"><span style="color: #66D9EF">plot</span><span style="color: #F8F8F2">(btc_ltc_price_2</span><span style="color: #F92672">$</span><span style="color: #F8F8F2">year_month, btc_ltc_price_2</span><span style="color: #F92672">$</span><span style="color: #F8F8F2">V1, </span><span style="color: #FD971F">xlab</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;Year-Month&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #FD971F">main</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;Correlation Coeff. Between BTC and Litecoin Over time&quot;</span></span>
<span class="line"><span style="color: #F8F8F2">     , </span><span style="color: #FD971F">ylab</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;Correlation Coefficient&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #FD971F">type</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #E6DB74">&quot;b&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #FD971F">pch</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">19</span><span style="color: #F8F8F2">, </span><span style="color: #FD971F">col</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">ifelse</span><span style="color: #F8F8F2">(btc_ltc_price_2</span><span style="color: #F92672">$</span><span style="color: #F8F8F2">V1 </span><span style="color: #F92672">></span><span style="color: #F8F8F2"> </span><span style="color: #AE81FF">0</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&quot;blue&quot;</span><span style="color: #F8F8F2">, </span><span style="color: #E6DB74">&quot;red&quot;</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #F8F8F2">     , </span><span style="color: #FD971F">ylim</span><span style="color: #F8F8F2"> </span><span style="color: #F92672">=</span><span style="color: #F8F8F2"> </span><span style="color: #66D9EF">c</span><span style="color: #F8F8F2">(</span><span style="color: #F92672">-</span><span style="color: #AE81FF">1</span><span style="color: #F8F8F2">, </span><span style="color: #AE81FF">1</span><span style="color: #F8F8F2">))</span></span></code></pre></div>



<p class="wp-block-paragraph" id="494c">Trends in the correlations of the daily return of bitcoin and litecoin on mothly basis, boy this is a mouth full, are very similar to those for the prices as we saw in the previous figure.</p>



<p class="wp-block-paragraph" id="2a55">In the next post we’ll do something more statistically sound, rolling correlations.</p>



<p class="wp-block-paragraph">Read More blogs in AnalyticaDSS Blogs here : <a href="https://analyticadss.com/blog">BLOGS</a></p>



<p class="wp-block-paragraph">Read More blogs in Medium : <a href="https://medium.com/@aousabdo">Medium Blogs</a></p>



<p class="wp-block-paragraph">Read More blogs in R-bloggers : <a href="https://www.r-bloggers.com/">https://www.r-bloggers.com</a></p>
<p>The post <a href="https://analyticadss.com/analyzing-cryptocurrency-markets-using-r-part-2/">Analyzing Crypto Market using R — Part 2</a> appeared first on <a href="https://analyticadss.com">Analytica Data Science Solutions</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
