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
## Version 0.4-0 included new data defaults. See ?getSymbols.
mdate <- "2007-01-01"
ebayPrices <- getSymbols('ebay', from=mdate, auto.assign = FALSE)[,4]
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
amazonPrices <- getSymbols('AMZN', from=mdate, auto.assign=FALSE)[,4]
GoogPrices <- getSymbols('GOOG', from=mdate, auto.assign = FALSE)[,4]
stockPrices <- merge(ebayPrices,amazonPrices,GoogPrices)
DailyRets <- ROC(stockPrices,type = 'discrete')
head(DailyRets)
## EBAY.Close AMZN.Close GOOG.Close
## 2007-01-03 NA NA NA
## 2007-01-04 0.04706670 0.005167984 0.033512299
## 2007-01-05 -0.02564109 -0.013624755 0.008132263
## 2007-01-08 -0.03508769 -0.022673939 -0.007409836
## 2007-01-09 0.00168352 0.007466640 0.003970390
## 2007-01-10 -0.01512605 -0.016675411 0.008156555
MonthlyRets <- periodReturn(stockPrices, period='monthly', type = 'log')
head(MonthlyRets)
## monthly.returns
## 2007-01-31 0.07100165
## 2007-02-28 -0.01024052
## 2007-03-30 0.03343363
## 2007-04-30 0.02355150
## 2007-05-31 -0.04150971
## 2007-06-29 -0.01173942
weeklyRets <- weeklyReturn(stockPrices, type = 'log')
head(weeklyRets)
## weekly.returns
## 2007-01-05 0.02001708
## 2007-01-12 -0.02566768
## 2007-01-19 -0.01139807
## 2007-01-26 0.06493881
## 2007-02-02 0.02342031
## 2007-02-09 0.03368550
library(zoo)
library(xts)
library(PortfolioAnalytics)
## Loading required package: foreach
## Loading required package: PerformanceAnalytics
##
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
##
## legend
library(fPortfolio)
## Loading required package: timeDate
##
## Attaching package: 'timeDate'
## The following objects are masked from 'package:PerformanceAnalytics':
##
## kurtosis, skewness
## Loading required package: timeSeries
##
## Attaching package: 'timeSeries'
## The following object is masked from 'package:zoo':
##
## time<-
## Loading required package: fBasics
##
## Attaching package: 'fBasics'
## The following object is masked from 'package:TTR':
##
## volatility
## Loading required package: fAssets
library(quadprog)
library(timeSeries)
library(caTools)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:timeSeries':
##
## filter, lag
## The following objects are masked from 'package:xts':
##
## first, last
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(PerformanceAnalytics)
library(ggplot2)
tickers <- c("GOOG", "AMZN", "ebay")
portfolioPrices <- NULL
for (Ticker in tickers)
portfolioPrices <- cbind(portfolioPrices,
getSymbols(Ticker, from="2010-01-01", to="2013-12-31", auto.assign = FALSE)[,4])
portfolioPrices <- portfolioPrices[apply(portfolioPrices,1,function(x) all(!is.na(x))),]
colnames(portfolioPrices) <- tickers
portfolioReturns <- na.omit(ROC(portfolioPrices, type = "discrete"))
portfolioReturns <- as.timeSeries(portfolioReturns)
stock_data <- tickers %>% lapply(function(x) getSymbols(x, from="2010-01-01", to="2013-12-31", auto.assign = FALSE)[,4]) %>%
lapply(function(x) monthlyReturn(x))
portfolioReturns <- do.call(merge, stock_data)
protfolioreturns <- portfolioReturns[apply(portfolioReturns,1,function(x) all(!is.na(x)))]
colnames(portfolioReturns) <- tickers
portfolioReturns <- as.timeSeries(portfolioReturns)
print(portfolioReturns)
## GMT
## GOOG AMZN ebay
## 2010-01-29 -0.154463558 -0.0634054547 -0.0368201447
## 2010-02-26 -0.005925163 -0.0558966731 0.0000000000
## 2010-03-31 0.076537582 0.1467060955 0.1715899342
## 2010-04-30 -0.073035690 0.0097959929 -0.1182796068
## 2010-05-28 -0.076222178 -0.0849015791 -0.0996636131
## 2010-06-30 -0.083767454 -0.1291247978 -0.0840727862
## 2010-07-30 0.089672959 0.0789858763 0.0662927021
## 2010-08-31 -0.071836634 0.0588684626 0.1109517011
## 2010-09-30 0.168370277 0.2581911038 0.0503659838
## 2010-10-29 0.167196018 0.0520183249 0.2237703982
## 2010-11-30 -0.094492369 0.0615505553 -0.0241125011
## 2010-12-31 0.068848844 0.0262258048 -0.0449554031
## 2011-01-31 0.010758096 -0.0575555611 0.0909091064
## 2011-02-28 0.021720326 0.0215161166 0.1037549721
## 2011-03-31 -0.043430133 0.0394714772 -0.0737093875
## 2011-04-29 -0.072704252 0.0870482017 0.1079252843
## 2011-05-31 -0.027715590 0.0044941730 -0.0936318779
## 2011-06-30 -0.042796081 0.0396563268 0.0352903457
## 2011-07-29 0.192167941 0.0881705636 0.0148744814
## 2011-08-31 -0.103910962 -0.0327611355 -0.0574045954
## 2011-09-30 -0.047914759 0.0046461925 -0.0447035903
## 2011-10-31 0.150667893 -0.0125792029 0.0793488938
## 2011-11-30 0.011389677 -0.0993864573 -0.0703737942
## 2011-12-30 0.077595591 -0.0997971174 0.0250084633
## 2012-01-31 -0.101857877 0.1232813129 0.0418727486
## 2012-02-29 0.065746177 -0.0758588760 0.1310126331
## 2012-03-30 0.037185593 0.1269964536 0.0324565978
## 2012-04-30 -0.056749420 0.1451286343 0.1116530695
## 2012-05-31 -0.039662701 -0.0818887041 -0.0446123271
## 2012-06-29 -0.001360082 0.0725189127 0.0719571498
## 2012-07-31 0.091195899 0.0216772361 0.0545107886
## 2012-08-31 0.082341979 0.0641663129 0.0715575581
## 2012-09-28 0.101315085 0.0243686426 0.0189593490
## 2012-10-31 -0.098343278 -0.0842639486 -0.0022741714
## 2012-11-30 0.026561833 0.0822706174 0.0944882460
## 2012-12-31 0.012901478 -0.0046816425 -0.0344566328
## 2013-01-31 0.068294246 0.0583170777 0.0974508922
## 2013-02-28 0.060223146 -0.0046328098 -0.0225120175
## 2013-03-28 -0.008749402 0.0084005036 -0.0089562737
## 2013-04-30 0.038252852 -0.0475814945 -0.0337513814
## 2013-05-31 0.056574895 0.0606359644 0.0326397717
## 2013-06-28 0.010502525 0.0315378515 -0.0439925848
## 2013-07-31 0.008382868 0.0847347720 -0.0005800343
## 2013-08-30 -0.046015241 -0.0671933800 -0.0328884083
## 2013-09-30 0.034254379 0.1126770687 0.1162232370
## 2013-10-31 0.176582078 0.1643743012 -0.0553763431
## 2013-11-29 0.028149170 0.0812844988 -0.0415480413
## 2013-12-30 0.047065365 -0.0006351303 0.0690815392
efffrontier <- portfolioFrontier(portfolioReturns, constraints = "longOnly")
plot(efffrontier, c(1,2))
