In this set of exercises we are using R to analyse stock prices. This is the first part where we exercise basic descriptive statistics. You dont need to be an expert stock trader in order to understand examples. Where needed, additional explanations will be provided.

All examples will be based on real historical data acquired from Yahoo finance.

Answers to the exercises are available here. If you have a different solution, feel free to post it.

**Exercise 1**

Load data directly from: data . We will use this data in other exercises, so save it in a variable.

Note, if you dont have a complete understanding of how to load this .csv file into R, please refer to this exercise set

Examine the data and learn its:

- column names
- data class and class of each column of data
- the lowest date
- the highest date
- distinct stock symbols

**Exercise 2**

Get a subset of the data containing only daily opening prices of BAC in the year 2016.

**Exercise 3**

For each stock, obtain the lowest and highest closing price together with dates when these prices occured. Format output like this:

Symbol | Max Price | Max Date | Min Price | Min Date |
---|

**Exercise 4**

Write a function called `AvgPrice`

that returns average closing price for a given stock symbol and a period of time between given start and end date.

Using the function, what is the average closing price of X in January of 2016?

**Exercise 5**

Write a function called `WAvgPrice`

that returns weighted average closing price for a given stock symbol and given period of time between start and end date. *(Tip: Weighted average closing price is calculated as sum(close * volume)/sum(volume))*

Using the function, what is the weighted average closing price of GE in year 2016.

**Exercise 6**

Reshape original data frame to get data frame with dates as the first column and closing prices of stocks in other columns, each column containing one stock. Name the columns after stock symbol.

Order resulting data frame by date and save it in a variable for later use.

**Exercise 7**

Using the variable from the previous exercise, calculate daily return for each stock and save result in new variable for later use. Daily return is calculated as `(price`

._{t}-price_{(t-1)})/price_{(t-1)}

Calculate average daily return for each stock.*(Tip: average rate is calculated as a geometric mean)*

**Exercise 8**

Using daily return data from previous exercise, which stock symbol has highest average risk, calculated as standard deviation of daily returns?

**Exercise 9**

Write a function called `LowestRisk`

that returns stock symbol with lowest risk in a given period of time between `start`

and `end`

. Call the function to obtain lowest risk stock symbol.

**Exercise 10**

Using daily returns of stocks from exercise 7, calculate the correlation coefficient matrix which includes every stock symbol in the data set.

Image by Peggy_Marco (Pixabay post) [CC0 Public Domain ], via Pixabay.

**What's next:**

- Explore all our (>1000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below

Dr. Pankaj K Agarwal says

Excellent exercises. Thanks a ton. Keep doing good work.

Miodrag Sljukic says

Thank you very much for your kind words.

Aleksandar says

Da li postoji mogucnost provere resenja?

/possibility to check for solutions?.pozdrav

Aleksandar

Miodrag Sljukic says

Hi Aleksandar, nice to have you here on r-exercises.

Could you be more specific please? In what way do you mean to check for solutions?

Anshul Gupta says

Hi Miodrag,

It is very nice exercise.

Completed it using a different set of code.

If you want to have a look:

# Stock Market exercise

stocks % group_by(Symbol) %>% summarise()

#BAC 2016 opening prices

BAC_2016 % filter(Symbol == “BAC”, year(Date) == 2016) %>% select(1:3)

#Lowest and Highest Closing Price and respective Date

stocks %>% group_by(Symbol) %>%

summarise(Max_Price = max(Close),Max_Date = Date[Close == max(Close)], Min_Price = min(Close), Min_Date = Date[Close == min(Close)])

#Avg Price of a stock for a given Time Period

AvgPrice % filter(Symbol == Sym, Date >= stdate , Date %

summarise(Avg_Price = mean(Close))

}

AvgPrice(“X”,”2016-01-01″,”2016-01-31″)

# Weighted Avg Price

WavgPrice % filter(Symbol == Sym, Date >= stdate , Date %

summarise(Wavg_Price = sum(as.numeric(Close * Volume))/sum(as.numeric(Volume)))

}

WavgPrice(“GE”,”2016-01-01″,”2016-12-31″)

# Daily Closing Price of each Stock

Daily_Close % select(Symbol, Date, Close) %>% spread(Symbol, value = Close)

#Daily return for each stock

freturn <- function(X){(X – lag(X, default = 0))/lag(X)}

Drtrn <- apply(Daily_Close[,2:6], 2, freturn)

Drtrn <- as_tibble(Drtrn)

Drtrn % mutate(Date = Daily_Close$Date) %>% select(Date, 1:5)

#Avg Daily Return

map_dbl(Drtrn,mean, na.rm = T)

#Highest Avg Risk

map_dbl(Drtrn,sd, na.rm = T)

#Lowest Risk in a period

LowestRisk <- function(stdate, endate){

dt % filter(Date >= stdate, Date <= endate)

names(which.min(map_dbl(dt,sd, na.rm = T)))

}

LowestRisk("2016-04-01","2016-04-30")

# Corealtion Matrix

cor(Drtrn[,-1], use="pairwise.complete.obs")

Anshul Gupta says

sorry, don’t know why the pipe symbol ‘%>%’ not coming properly.