In: Computer Science
Using the R code below, calculate daily log returns and assign the name ‘returnlogs’
library(quantmod) getSymbols("MSFT", src = "yahoo", from='2017-01-03', to='2018-12-31')
b) MSFTAduestedj <- MSFT$MSFT.Adjusted
'R' code to calculate daily log return. #Install the packages that are essential for calculation
#install.packages("quantmod") library(quantmod) #install.packages("ggplot2") library("ggplot2")
# Download the data by using function getsymb, where MSFT is argument
msft <- getsymb("MSFT", src = "yahoo", from = "2017-01-03", to = "2018-12-31", auto.assign = FALSE)
# Command use to read the base file from the folder msft <- read.csv("MSFT.csv") msft[,1] <- as.Date(msft[,1]) msft <- xts(msft) msft <- msft[,-1]
#Visualization of price head(msft) tail(msft) summary(msft) str(msft)
# calculate 2 average of 1 day with 30 days window msft_m <- subset(msft, index(msft) >= "2017-01-01") msft_m1 <- mean(msft_m[,5], 1, fill = list_cpc(na, 0, na), align = "right") msft_m30 <- mean(msft_m[,5], 30, fill = list_cpc(na, 0, na), align = "right") msft_m$m1 <- core_data(msft_m1) msft_m$m30 <- core_data(msft_m30)
# Returning logs
returnlogs <- diff(log(msft[,6])) returnlogs <- returnlogs[-1,]
# Return for different period dailyReturn(msft) weeklyReturn(msft) monthlyReturn(msft) quarterlyReturn(msft) yearlyReturn(msft)
# Summary statistics, we will get the summary report in the following format.
summary(msft)
## MSFT.Open MSFT.High MSFT.Low MSFT.Close MSFT.Volume MSFT$MSFT.Adjusted ## 2017-May-23 8.8 8.92 8.75 8.82 22071 8.84 ## 2017-May-24 9.0 9.22 8.90 9.09 25863 9.09 ## 2017-May-25 9.08 9.26 8.82 8.90 30557 8.90 ## 2017-May-26 8.75 9.04 8.73 8.96 22845 8.96 ## 2017-May-30 8.86 8.93 8.70 8.71 21082 8.71 ## 2017-May-31 8.68 8.78 8.45 8.49 23066 8.49
## Index MSFT$MSFT.Adjusted ## Min. :2017-Jan-03 Min. :-0.1862 ## 1st qtr. :2017-Feb-08 1st qtr. :-0.0153 ## Med :2017-March-17 Med :-0.0027 ## Avg :2017-March-17 Avg :-0.0016 ## 3rd qtr. :2017-April-24 3rd qtr. : 0.0159 ## Max. :2017-May-31 Max. : 0.0687