Stock prices analysis part 1 exercises
[This article was first published on R-exercises, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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:
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
- column names
- data class and class of each column of data
- the lowest date
- the highest date
- distinct stock symbols
Symbol | Max Price | Max Date | Min Price | Min Date |
---|
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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.