This worksheet is your warm-up exercises to getting started with some R for finance, based on examples from the book. It is also your getting started with problems solving (no R required) and expressing your methods/answers using proper mathematical symbols and representations.
Remember to always set your working directory to the source file location. Go to ‘Session’, scroll down to ‘Set Working Directory’, and click ‘To Source File Location’. Read carefully the below and follow the instructions to complete the tasks and answer any questions. Submit your work to RPubs as detailed in previous notes.
For clarity, tasks/questions to be completed/answered are highlighted in red color (color visible only in preview mode) and numbered according to their particular placement in the task section. Type your answers outside the red color tags!
Quite often you will need to add your own code chunk. Execute sequentially all code chunks, preview, publish, and submit link on Sakai following the naming convention. Make sure to add comments to your code where appropriate. Use own language!
Any sign of plagiarism, will result in dissmissal of work!
This task follows the book’s R Example 1.1/p. 8 also exhibited in the R-Labs section R Labs1 from the book’s website (*). In order to work with R in finance we need to first install the package quantmod that allows us to retrieve financial data and conduct various statistical analysis.
# Require will load the package only if not installed
# Dependencies = TRUE makes sure that dependencies are install
if(!require("quantmod",quietly = TRUE))
install.packages("quantmod",dependencies = TRUE, repos = "https://cloud.r-project.org")
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Version 0.4-0 included new data defaults. See ?getSymbols.
##### 1A) Insert in the code chunk below the rest of the code to calculate and chart the time series for stock GE. Follow the example in the book. Add comments, where appropriate, explaining what the particular line(s) of code is/are executing/solving for.
##retrieve historical price data for General Electric Co. from Yahoo Finance
getSymbols('GE',src='yahoo', from="2000-01-01", to="2009-12-30")
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
##
## WARNING: There have been significant changes to Yahoo Finance data.
## Please see the Warning section of '?getSymbols.yahoo' for details.
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.yahoo.warning"=FALSE).
## [1] "GE"
##to see headers of file (OHLCV type)
names(GE)
## [1] "GE.Open" "GE.High" "GE.Low" "GE.Close" "GE.Volume"
## [6] "GE.Adjusted"
chartSeries(GE)
##to see chart of the data
This follows the book’s example 1.3.4 R Lab/p. 34 also R Labs 1 from the book’s website (*).
##### 2A) Insert the code chunk to chart instead the performance of Value Line Index VLIC (sort of benchmark), and four other stocks of your choice over a time period dating back one year from present date.
##retrieve historical price data for 5 stocks
symbols = c('^VLIC','GOOG','MSFT','AYI','CSIQ')
getSymbols(symbols,src='yahoo', from="2017-11-12", to="2018-11-12")
## [1] "VLIC" "GOOG" "MSFT" "AYI" "CSIQ"
#obtain adjusted closed
VLICad = VLIC$VLIC.Adjusted; GOOGad = GOOG$GOOG.Adjusted;MSFTad =MSFT$MSFT.Adjusted;AYIad=AYI$AYI.Adjusted;
CSIQad = CSIQ$CSIQ.Adjusted
#compute cumulative sum (cumsum) of daily returns (Delt)
#Remove first term of the series, with [-1,],
#since cumsum is not defined for it
vl = cumsum((Delt(VLICad)*100)[-1,])
go = cumsum((Delt(GOOGad, type='arithmetic')*100)[-1,])
ms = cumsum((Delt(MSFTad)*100)[-1,])
ay = cumsum((Delt(AYIad)*100)[-1,])
cs = cumsum((Delt(CSIQad)*100)[-1,])
##range of values for the plot
lim = c(min(vl,go,ms,ay,cs),max(vl,go,ms,ay,cs))
##the plot
plot(vl,main="",ylim=lim,xlab="dates",ylab="% benefits")
lines(go,col="green"); lines(ms,col="red")
lines(ay,col="violet"); lines(cs,col="yellow")
legend(x="topleft",cex=0.4,c("VLIC","GOOG","MSFT","AYI","CSIQ"),lty=1,col=c("black","green","red","violet","yellow"))
##### 2B) Write the mathematical form/representation, using proper math symbols, to describe the formula being calculated and plotted in the code
\(Y_{n} = \sum_{n=1}^{j-1} Y_{n-1} + \frac{X_{n+1} - X_{n}}{X_{n}}\) Where Y_{n} is the cumulative delt value, X_{n} is the adjusted price, and j is the total number of periods
##### 2C) Write a paragraph, in your own words, comparing the stock performances based on your assessment of the companies general business and their valuation vis-a-vis the market overall performance, and pertinent econonomical factors. The four stocks selected are Google, Microsoft, Acuity Brands, and Canadian Solar. Google and Microsoft have had great stock performance over the past year, outperforming the VLIC consistently. Acuity Brands stock has underperformed, and although the company has beat expectations, there are rising input costs due to increased tariffs. Canadian Solar stock has also underperformed relative to the VLIC, but maintains high manufacturing margins compared to its competitors.
To add math symbols in R Markdown check the link MathinRmd.html. Many other examples are on Google. Following is an example: \(\sum_{n=1}^{10} n^2\). Other, less preferred options, is to neatly handwrite your equations on a paper or use own preferred tools like Microsoft Word, take a clear image capture of your work and include here. Hard to read work will be dismissed.
Below is an example on how to include an image. It is assumed that the image resides in the same folder as the worksheet. Otherwise the path to the folder where the image resides needs to be included in the name referencing below.
This task does not require use of R code. You will however need to type your work here, using proper mathematical symbols and representations, or include a clear image capture of your work.
##### 3A) Solve for 1.3.8 Problem/p. 36. \(C_{u} = S_{u} - K = 40 - 20 = 20\) \(C_{d} = 0\) \(\Delta = \frac{C_{u}-C_{d}}{S_{u}-S{d}} = \frac{20-0}{40-15} = 0.8\) \(\hat{B} = \frac{\Delta*S_{d}-C_{d}}{1+r} = \frac{0.8*.15}{1+.03} = 11.65\) \(C = \Delta*S - (1+r)^{t} * \hat{B} = 0.8*25 - (1+.03)*11.65 = 32\) \(P = C + K(e)^{-r} - S_{0} =32 + 20(e)^{-.03} - 25 = 26.41\)
##### 3B) Solve for 1.3.9 Problem/p. 36