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

Pub: 04.12.17

In Finance.

Tags: coding r bitcoin analysis .

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.

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")
chartSeries(GSPC)
dev.off()


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)
.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.

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


We can also look at what weights produce what results:

png(filename = "btc_plot_weights.png")
weightsPlot(frontier)
dev.off()


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