Let's do the same thing as in the previous section, but with multiple stocks instead of an index. These are some of the most held stocks by individuals according to online source.
getSymbols("TSLA", src = "yahoo", from = as.Date(firstdate), to = as.Date(lastdate))
getSymbols("NVDA", src = "yahoo", from = as.Date(firstdate), to = as.Date(lastdate))
getSymbols("AMD", src = "yahoo", from = as.Date(firstdate), to = as.Date(lastdate))
tslaret <- monthlyReturn(Ad(TSLA))
nvdaret <- monthlyReturn(Ad(NVDA))
amdret <- monthlyReturn(Ad(AMD))
returns <- cbind(btcnret, tslaret, nvdaret, amdret)
colnames(returns) <- c('BitcoinUSD', 'TSLA', 'NVDA', 'AMD')
returns <- head(returns, -1)
returns <- as.timeSeries(returns)
frontier <- portfolioFrontier(returns)
png(filename = "btc_plot_stocks_frontier.png")
plot(frontier, which = "all")
dev.off()
png(filename = "btc_plot_stocks_weights.png")
weightsPlot(frontier)
dev.off()
out <- minvariancePortfolio(returns, constraints = "LongOnly")
store <- out@portfolio@portfolio$weights
out <- maxratioPortfolio(returns, constraints = "LongOnly")
store <- rbind(store, out@portfolio@portfolio$weights)
rownames(store) <- c('minvar', 'maxratio')
The resulting allocations would range from 3 to 9 percent into Bitcoin and the rest into TSLA, NVDA, and AMD.
BitcoinUSD TSLA NVDA AMD
minvar 0.02944844 0.2398669 0.6818332 0.04885147
maxratio 0.09466859 0.3087685 0.5965629 0.00000000
Does the structure of the covariance estimator effect the results of our portfolio optimization
estimatorlist <- c(
'covEstimator',
'kendallEstimator',
'spearmanEstimator',
'mcdEstimator',
'mveEstimator',
'covMcdEstimator',
'covOGKEstimator',
'shrinkEstimator')
png(filename = "btc_plot_stocks_covarestim.png")
par(mfrow = c(4, 2))
par(mar = c(3, 3, 4, 3))
for (i in 1:length(estimatorlist)) {
Spec <- portfolioSpec()
setEstimator(Spec) <- estimatorlist[i]
frontier <- portfolioFrontier(returns, spec = Spec)
weightsPlot(frontier)
}
dev.off()
What if you believe that our return estimates are bogus, especially related to their size and how they are correlated. Or you think more observation might help.
Well, you could decide to use Copulas or extreme value theory to help you: Copulas and extreme values with Bitcoin
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