Preliminaries

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

Set working directory (this you have to change to your working directory)

setwd('/Users/liulingdu/Downloads')
getwd() 
## [1] "/Users/liulingdu/Downloads"

Load the APPLE data

AAPL <- read.csv(paste(getwd(),"/data/AAPL.txt",sep=''))

Convert the column with dates into Date format

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'))

Convert the data frame into xts object ordered by date in vector D

AAPL_xts <- xts(AAPL[,1], order.by = D)

Compute percentage log-returns from close prices of APPLE stock

r_AAPL <- 100*diff(log(AAPL_xts[,1]))

Example of a plot using dygraph and xts object. It plots the close price

#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

Read the tickers for the data

ticker <- read.table(paste(getwd(),"/data/Tickers.txt",sep=''), header=FALSE)

Read the rest of the data and merge into one xts object

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)
}

  1. YOUR EMAIL