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")
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)
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.
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
You can also jump to each section directly from here:
- Introduction to Bitcoin analysis with R
- Retrieving Bitcoin transaction data
- Part 2 - Reading the bitcoin data in to R
- Using the xts package and dates
- Using xts to summarize Bitcoin transaction data
- Setting up Bitcoin data in OHLC format
- Charting Bitcoin data
- Prliminary return analysis with plots
- Preliminary return analysis with rolling windows
- Technical analysis plots of Bitcoin
- Bitcoin's future price path
- Evaluating a portfolio
- Evaluating a stock portfolio
- Copulas and extreme values with Bitcoin
- Copulas and extreme value, many assets