R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

# Building my portfolio workbook using R and quantmod
install.packages("quantmod")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
install.packages("PerformanceAnalytics")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
install.packages("xts")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
install.packages("ggplot2")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(PerformanceAnalytics)
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
library(xts)
library(ggplot2)

## My stocks
NVDA <-getSymbols("NVDA", auto.assign =FALSE)

DIS <-getSymbols("DIS", auto.assign =FALSE)


stocks <-as.xts(data.frame(NVDA = NVDA[, "NVDA.Adjusted"], 
                           DIS = DIS[, "DIS.Adjusted"]))


## Now, let's build a graph

autoplot(stocks, facets= NULL) +
  ggtitle("Daily Adjusted Prices") +
  ylab("Adjusted Prices") +
  xlab("year")

## Analyzing returns of these stocks
DIS <- dailyReturn(Ad(getSymbols("DIS", auto.assign = F)))
NVDA <- dailyReturn(Ad(getSymbols("NVDA", auto.assign = F)))
portfolio <-merge(NVDA, DIS, all=F)
charts.PerformanceSummary(DIS, main = "Disney")

charts.PerformanceSummary(
  portfolio, main = "Disney vs. NVDA")

table.AnnualizedReturns(portfolio, scale =252, Rf = .004/252)
##                             daily.returns daily.returns.1
## Annualized Return                  0.3358          0.0823
## Annualized Std Dev                 0.4909          0.2877
## Annualized Sharpe (Rf=0.4%)        0.6732          0.2711