Load the required packages.
#here put an r code which loads the requested packages
library("xts") #for time series
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library("nloptr") #for optimization
library(zoo)
library("dygraphs") #for plots
library(plotly) # for 3D plots
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library("magrittr") # pipes
library("webshot")
library(fBasics)
## Loading required package: timeDate
## Loading required package: timeSeries
##
## Attaching package: 'timeSeries'
## The following object is masked from 'package:zoo':
##
## time<-
##
## Rmetrics Package fBasics
## Analysing Markets and calculating Basic Statistics
## Copyright (C) 2005-2014 Rmetrics Association Zurich
## Educational Software for Financial Engineering and Computational Science
## Rmetrics is free software and comes with ABSOLUTELY NO WARRANTY.
## https://www.rmetrics.org --- Mail to: info@rmetrics.org
library('PerformanceAnalytics')
##
## Attaching package: 'PerformanceAnalytics'
## The following objects are masked from 'package:timeDate':
##
## kurtosis, skewness
## The following object is masked from 'package:graphics':
##
## legend
setwd('/Users/liulingdu/Downloads')
getwd()
## [1] "/Users/liulingdu/Downloads"
AAPL <- read.csv(paste(getwd(),"/data/AAPL.txt",sep=''))
D <- AAPL[,1]
D <- as.Date(toString(AAPL[1,1]),'%Y%m%d')
for (t in 2:dim(AAPL)[1])
D <- c(D,as.Date(toString(AAPL[t,1]),'%Y%m%d'))
AAPL_xts <- xts(AAPL[,1], order.by = D)
head(AAPL_xts)
## [,1]
## 2001-01-02 20010102
## 2001-01-03 20010103
## 2001-01-04 20010104
## 2001-01-05 20010105
## 2001-01-08 20010108
## 2001-01-09 20010109
summary(AAPL_xts)
## Index AAPL_xts
## Min. :2001-01-02 Min. :20010102
## 1st Qu.:2004-11-15 1st Qu.:20041116
## Median :2008-10-20 Median :20081020
## Mean :2008-10-21 Mean :20083787
## 3rd Qu.:2012-09-20 3rd Qu.:20120920
## Max. :2016-08-26 Max. :20160826
r_AAPL <- 100*diff(log(AAPL_xts[,1]))
basicStats(r_AAPL)
## r_AAPL
## nobs 3955.000000
## NAs 1.000000
## Minimum 0.000005
## Maximum 0.044320
## 1. Quartile 0.000005
## 3. Quartile 0.000005
## Mean 0.000190
## Median 0.000005
## Sum 0.750417
## SE Mean 0.000043
## LCL Mean 0.000105
## UCL Mean 0.000274
## Variance 0.000007
## Stdev 0.002715
## Skewness 16.120442
## Kurtosis 258.115017
#do not use dygraph() if you knit to pdf
#uncomment if you knit to html
#dygraph(AAPL_xts[,4]) %>%
#dyOptions(axisLineWidth = 1.5, fillGraph = FALSE, drawGrid = T, rightGap=50) %>%
#dyRangeSelector()
# for the homework and the project, if you use dygraph(), then you can knit to html, print the html file and submit the print out of the html file
ticker <- read.table(paste(getwd(),"/data/Tickers.txt",sep=''), header=FALSE)
data_xts<-AAPL_xts
rets_xts<-r_AAPL
for (k in 3:dim(ticker)[1])
{
prices <- read.csv(paste(getwd(),"/data/", ticker$V1[k],".txt",sep =''))
dates <- NULL;
dates <- as.Date(toString(prices[1,1]),'%Y%m%d')
for (t in 2:dim(prices)[1])
dates <- c(dates,as.Date(toString(prices[t,1]),'%Y%m%d'))
prices_xts <- xts(prices[,4],order.by = dates)
logret_xts <- 100*diff(log(prices_xts[,1]))
rets_xts<-merge(rets_xts,logret_xts)
data_xts<-merge(data_xts,prices_xts)
}
YOUR EMAIL↩