Stock Data Pages | TCA League Tables | About

Analyzing Bitcoin in R - Part 11 - Evaluating a portfolio, index and Bitcoin

Pub: 04.12.17

| By Anonymous

In Finance.

Tags: coding r bitcoin analysis .

How about a portfolio approach

The GSPC index from Yahoo is not really the right benchmark because it doesn't contain dividends, however, it will be okay for this example. If you want to get a better market benchmark, you can get one from Kenneth French's website, for example.

Also be aware that Yahoo has discontinued some downloading and the quantmod getSymbols command might only work from the latest version of quantmod.

firstdate <- index(first(rawdailyohlc))
lastdate <- index(last(rawdailyohlc))
getSymbols("^GSPC", src = "yahoo", from = as.Date(firstdate), to = as.Date(lastdate))
png(filename = "gspc_plot.png")

Daily GSPC Plot

Let's calculate monthly returns of Bitcoin and GSPC for our portfolio allocation. I had to cut some observations from the bottom to make them the same length.

btcnret <- monthlyReturn(rawdailyohlc)
btcnret <- head(btcnret, -1)
spidret <- monthlyReturn(Ad(GSPC))
spidret <- head(spidret, -1)
.index(btcnret) <- .index(spidret)

Join the time series together and make them a timeSeries object.

returns <- cbind(btcnret, spidret)
colnames(returns) <- c('BitcoinUSD', 'SP500')
returns <- as.timeSeries(returns)

Let's plot the efficient frontier.

frontier <- portfolioFrontier(returns)
png(filename = "btc_plot_frontier.png")
plot(frontier, which = "all")

Bitcoin and GSPC Frontier

We can also look at what weights produce what results:

png(filename = "btc_plot_weights.png")

Bitcoin and GSPC Weights

A minimum variance portfolio suggest putting everything in the index. Bitcoin is too volatile.

minvariancePortfolio(returns, constraints = "LongOnly")

Portfolio Weights:
BitcoinUSD      SP500 
         0          1 

A return maximized portfolio targeting 5% monthly returns suggest putting 6% weight in Bitcoin and the rest in the index.

Spec <- portfolioSpec()
setTargetRisk(Spec) <- 0.05
setSolver(Spec)= "solveRshortExact"
maxreturnPortfolio(returns, spec = Spec, constraints = "Short")
Portfolio Weights:
BitcoinUSD      SP500 
    0.0624     0.9376 

A maximum return-risk ratio porfolio would put 3% in Bitcoin.

maxratioPortfolio(returns, constraints = "LongOnly")
Portfolio Weights:
BitcoinUSD      SP500 
    0.0338     0.9662 

As does the tangency portfolio.

tangencyPortfolio(returns, constraints = "LongOnly")
Portfolio Weights:
BitcoinUSD      SP500 
    0.0338     0.9662 

So what if you are an investor in tech stocks, how much Bitcoin should you hold: Evaluating a stock portfolio

You can also jump to each section directly from here: