<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/stock-prices-analysis-part-1-exercises/"></div></div><div><a data-url="http://www.r-bloggers.com/stock-prices-analysis-part-1-exercises/" data-counturl="http://www.r-bloggers.com/stock-prices-analysis-part-1-exercises/" data-count="horizontal" data-text="Stock prices analysis  part 1 exercises" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2016/07/26/stock-prices-analysis-part-1-exercises/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" href="http://r-exercises.com/wp-content/uploads/2016/07/financial-equalization-1015294_960_720.jpg?ace78c"><img sizes="(max-width: 300px) 100vw, 300px" srcset="http://r-exercises.com/wp-content/uploads/2016/07/financial-equalization-1015294_960_720-300x300.jpg 300w, http://r-exercises.com/wp-content/uploads/2016/07/financial-equalization-1015294_960_720-150x150.jpg 150w, http://r-exercises.com/wp-content/uploads/2016/07/financial-equalization-1015294_960_720-600x600.jpg 600w, http://r-exercises.com/wp-content/uploads/2016/07/financial-equalization-1015294_960_720.jpg 720w" alt="financial-equalization-1015294_960_720" class="wp-image-983" src="http://i0.wp.com/r-exercises.com/wp-content/uploads/2016/07/financial-equalization-1015294_960_720-300x300.jpg?resize=300%2C300" data-recalc-dims="1"></a>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.</p>
<p>All examples will be based on real historical data acquired from Yahoo finance.</p>
<p>Answers to the exercises are available <a target="_blank" href="http://r-exercises.com/2016/07/26/stock-prices-analysis-part-1-solutions/">here</a>. If you have a different solution, feel free to post it.</p>
<p><strong>Exercise 1</strong></p>
<p>Load data directly from: <a target="_blank" href="http://r-exercises.com/wp-content/uploads/2016/07/data.csv">data</a> . We will use this data in other exercises, so save it in a variable.</p>
<p>Note, if you dont have a complete understanding of how to load this .csv file into R, please refer to <a target="_blank" href="http://r-exercises.com/2016/01/07/reading-delimited-data/">this</a> exercise set</p>
<p>Examine the data and learn its:</p>
<ol>
<li>column names</li>
<li>data class and class of each column of data</li>
<li>the lowest date</li>
<li>the highest date</li>
<li>distinct stock symbols</li>
</ol>
<p><strong>Exercise 2</strong></p>
<p>Get a subset of the data containing only daily opening prices of BAC in the year 2016.</p>
<p><strong>Exercise 3</strong></p>
<p>For each stock, obtain the lowest and highest closing price together with dates when these prices occured. Format output like this:</p>
<table>
<tbody>
<tr>
<th> Symbol</th>
<th> Max Price</th>
<th> Max Date</th>
<th> Min Price</th>
<th> Min Date</th>
</tr>
</tbody>
</table>
<p><strong>Exercise 4</strong></p>
<p>Write a function called <code>AvgPrice</code> that returns average closing price for a given stock symbol and a period of time between given start and end date.</p>
<p>Using the function, what is the average closing price of X in January of 2016?</p>
<p><strong>Exercise 5</strong></p>
<p>Write a function called <code>WAvgPrice</code> that returns weighted average closing price for a given stock symbol and given period of time between start and end date. <i>(Tip: Weighted average closing price is calculated as <code>sum(close * volume)/sum(volume)</code>)</i></p>
<p>Using the function, what is the weighted average closing price of GE in year 2016.</p>
<p><strong>Exercise 6</strong></p>
<p>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.</p>
<p>Order resulting data frame by date and save it in a variable for later use.</p>
<p><strong>Exercise 7</strong></p>
<p>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 <code>(price<sub>t</sub>-price<sub>(t-1)</sub>)/price<sub>(t-1)</sub></code>.</p>
<p>Calculate average daily return for each stock.<i>(Tip: average rate is calculated as a geometric mean)</i></p>
<p><strong>Exercise 8</strong></p>
<p>Using daily return data from previous exercise, which stock symbol has highest average risk, calculated as standard deviation of daily returns?</p>
<p><strong>Exercise 9</strong></p>
<p>Write a function called <code>LowestRisk</code> that returns stock symbol with lowest risk in a given period of time between <code>start</code> and <code>end</code>. Call the function to obtain lowest risk stock symbol.</p>
<p><strong>Exercise 10</strong></p>
<p>Using daily returns of stocks from exercise 7, calculate the correlation coefficient matrix which includes every stock symbol in the data set.</p>
<p>Image by Peggy_Marco (Pixabay post) [<a target="_blank" href="https://creativecommons.org/publicdomain/zero/1.0/deed.nl">CC0 Public Domain </a>], <a target="_blank" href="http://r-exercises.com/wp-content/uploads/2016/07/financial-equalization-1015294_960_720-300x300.jpg?ace78c">via Pixabay</a>.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/stock-prices-analysis-part-1-exercises/"></div></div><div><a data-url="http://www.r-bloggers.com/stock-prices-analysis-part-1-exercises/" data-counturl="http://www.r-bloggers.com/stock-prices-analysis-part-1-exercises/" data-count="vertical" data-text="Stock prices analysis  part 1 exercises" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r-exercises.com/2016/07/26/stock-prices-analysis-part-1-exercises/"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/lQbQmebWJMo"></a></div>