Stock prices analysis part 1 exercises

July 26, 2016
By

(This article was first published on R-exercises, and kindly contributed to R-bloggers)

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:

  1. column names
  2. data class and class of each column of data
  3. the lowest date
  4. the highest date
  5. 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 (pricet-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.

To leave a comment for the author, please follow the link and comment on their blog: R-exercises.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)