For financial data you will need the “quantmod” package. From that package you can use…
- User
For financial data you will need the “quantmod” package. From that package you can use the getSymbols function to download stock data. Example: You can get the stock price of Microsoft (ticker:MSFT) getSymbols(“MSFT” , src = "yahoo", from = start_date, auto.assign = TRUE) Where the start date is in yyyy-mm-dd format. Please learn all the options including beginning and end dates and frequencies (daily, weekly, etc. and the price available) that are available for getSymbols function. For future reference you can use getSymbols also to download macro data from the Federal Reserve system by defining the source as (src = “FRED”) You can also make a list of stocks by defining it first as a vector and populating it afterwards. stocks<- vector() stocks <- c(“MSFT, GOOG”) And get multiple stock data as: getSymbols(stocks , src = "yahoo", from = start_date, auto.assign = TRUE) Now, download weekly prices of two stocks of your choice between 2005-05-05 and 2023- 10-01 and draw their price data, you can use the closing price. Next, find the max, min, range, average and calculate the standard deviation for the price of each stock. Calculate also the coefficient of variation. Which stock is riskier? Calculate the weekly return, defined as the logarithmic change rt = log(P_t) - log(P_t-1) from a week earlier, for each stock and draw them together in the same graph. Now calculate a rolling window (8 weeks) of standard deviations of each stock, i.e first observation for this series is the standard deviation which is calculated using the first eight weeks, the second one is calculated using the next 8 weeks starting at the second week (hence the name rolling window). Is there a change? Comment. Now draw the returns and the rolling window of standard deviations in the same graph separately for each stock. Find the correlation coefficient between returns and standard deviations. Comment.
- Assistant
- User
I already wrote this "install.packages("quantmod") library(quantmod)
Choose Apple and Amazon
stocks <- c("AAPL", "AMZN")
start_date <- "2005-05-05" end_date <- "2023-10-01"
Download the weekly prices of the selected stocks
and extract the closing prices
getSymbols(stocks, src = "yahoo", from = start_date, to = end_date, auto.assign = TRUE) closing_prices_AAPL <- Cl(AAPL) closing_prices_AMZN <- Cl(AMZN)
Calculate summary statistics for each stock
max_price_AAPL <- max(closing_prices_AAPL) min_price_AAPL <- min(closing_prices_AAPL) range_price_AAPL <- max_price_AAPL - min_price_AAPL average_price_AAPL <- mean(closing_prices_AAPL) std_deviation_AAPL <- sd(closing_prices_AAPL) coefficient_of_variation_AAPL <- (std_deviation_AAPL / average_price_AAPL) * 100
max_price_AMZN <- max(closing_prices_AMZN) min_price_AMZN <- min(closing_prices_AMZN) range_price_AMZN <- max_price_AMZN - min_price_AMZN average_price_AMZN <- mean(closing_prices_AMZN) std_deviation_AMZN <- sd(closing_prices_AMZN) coefficient_of_variation_AMZN <- (std_deviation_AMZN / average_price_AMZN) * 100
- Assistant
Certainly! Here's the continuation of the code with comments in bold:
# Calculate weekly returns for AAPL and AMZN returns_AAPL <- log(closing_prices_AAPL) - lag(log(closing_prices_AAPL), k = 1) returns_AMZN <- log(closing_prices_AMZN) - lag(log(closing_prices_AMZN), k = 1) # Plot the weekly returns for AAPL and AMZN par(mfrow = c(1, 2)) plot(returns_AAPL, main = "Weekly Returns for AAPL", ylab = "Log Returns") plot(returns_AMZN, main = "Weekly Returns for AMZN", ylab = "Log Returns") # Calculate rolling window standard deviations for AAPL and AMZN rolling_sd_AAPL <- rollapply(closing_prices_AAPL, width = 8, FUN = sd, align = "right", fill = NA) rolling_sd_AMZN <- rollapply(closing_prices_AMZN, width = 8, FUN = sd, align = "right", fill = NA) # Plot the weekly returns and rolling window standard deviations for AAPL and AMZN par(mfrow = c(1, 2)) plot(returns_AAPL, main = "Weekly Returns for AAPL", ylab = "Log Returns") lines(rolling_sd_AAPL, col = "red") legend("topright", legend = c("Returns", "Rolling SD"), col = c("black", "red"), lty = 1) plot(returns_AMZN, main = "Weekly Returns for AMZN", ylab = "Log Returns") lines(rolling_sd_AMZN, col = "red") legend("topright", legend = c("Returns", "Rolling SD"), col = c("black", "red"), lty = 1) # Calculate the correlation coefficient between returns and standard deviations for AAPL and AMZN correlation_AAPL <- cor(returns_AAPL, rolling_sd_AAPL, use = "pairwise.complete.obs") correlation_AMZN <- cor(returns_AMZN, rolling_sd_AMZN, use = "pairwise.complete.obs") # Print the correlation coefficients correlation_AAPL correlation_AMZN