Please write a code in R according to the instructions below. Question 1: Functi
ID: 3599477 • Letter: P
Question
Please write a code in R according to the instructions below.
Question 1: Function and for loop For this question, you are required to write a function to download equity data with quantmod In this function, you should have at least 3 input variables stock name » start date . end date Your function should be capable in solving these problems: Download daily equity data and calculate weekly return (either type of return) Calculate skewness and kurtosis for weekly return » Return a table format result Once you finish build this function, you should search the newest list for Dow 30 companies and implement your function on this list. The time range for this daily data set should start from 2017-01-01 to 2017-06-30. Your output from function should be similar like this example: Ticker Start date End date Skewness Kurtosis Hints: Use for loop to control the change of stock name.Explanation / Answer
R Code for downloading equity data using quantmod package.
# Downloading package quantmod
install.packages("quantmod")
# Loading package quantmod
library(quantmod)
# Downloading package moments
install.packages("moments")
#Loading package moments
library(moments)
# Creating function my_equity which will take three parameters and get the stock data.
# Skewness and kurtosis is calculated using functions of moments package
my_equity <- function(stock_name, start_date, end_date){
my.newdf <- getSymbols(Symbols = stock_name, auto.assign = FALSE, from = start_date, to = end_date)#Get equity data
my_skew <- skewness(my.newdf$FTSE.Adjusted) # Calculate Skewness. Change parameter as per requirement.
my_kurt <- kurtosis(my.newdf$FTSE.Adjusted) # Calculate Kurtosis. Change parameter here as per requirement.
new_equity <- data.frame("Ticker"= stock_name, "Start date" = start_date, "End date" = end_date,
"Skewness" = my_skew, "Kurtosis" = my_kurt) # Creating Data frame
return(new_equity) # Returning Data frame
}
my_equity('^FTSE','2017-01-01','2017-06-30') # Calling function my_equity with appropriate parameters
Code screen shot