About

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.

Setup

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.

Note

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!


Task 1: Charting Stock Time Series

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.

library("quantmod")
##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"
## extract Adjusted Close
geAdj = GE$GE.Adjusted["2000-01-01/2000-01-20"]
##compute max, min and mean
max(geAdj); min(geAdj); mean(geAdj)
## [1] 28.06313
## [1] 26.23787
## [1] 27.1946
## draw a chart
chartSeries(GE)

chartSeries(GE,TA=NULL,subset='2001-01::2001-02')

#Saving the data file on GE
saveRDS(GE,file="GE.rds")

Task 2: Comparing Performance of Stocks Time Series

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.

library("quantmod")
symbols=c('^VLIC','NFLX','TWTR','FB','SNAP')
getSymbols(symbols,src='yahoo',from="2017-11-14",to="2018-11-14")
## [1] "VLIC" "NFLX" "TWTR" "FB"   "SNAP"
#Getting the adjusted close prices
VLICad = VLIC$VLIC.Adjusted; NFLXad= NFLX$NFLX.Adjusted;
TWTRad=TWTR$TWTR.Adjusted; FBad=FB$FB.Adjusted;
SNAPad = SNAP$SNAP.Adjusted
#Compute the cumulative sum of the daily returns
vl = cumsum((Delt(VLICad)*100)[-1,])
NF = cumsum((Delt(NFLXad)*100)[-1,])
TW = cumsum((Delt(TWTRad)*100)[-1,])
F = cumsum((Delt(FBad)*100)[-1,])
SNP = cumsum((Delt(SNAPad)*100)[-1,])
#Setting the range of values to be used in a graph
lim = c(min(vl,NF,TW,F,SNP),max(vl,NF,TW,F,SNP))
#Creating the graph
plot(vl,main="",ylim=lim,xlab="dates",ylab="% benefits")

lines(NF,col="green"); lines(TW,col="red")

lines(F,col="violet"); lines(SNP,col="yellow")

legend(x="topleft",cex=0.4,c("VLIC","NF","TW","F","SNP"),
       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

Mathematical Representation:

(Sum((Term3/Term2)-1,(Term4/Term3)-1,(Term5/Term4)-1…,(Term365/Term364)-1))*100

##### 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 performances of these stocks over the period (11-14-17 to 11-14-18) are indicative of the market performance in general as well as their respective industries. For example, SNAP has seen its value decrease significantly as it struggles to maintain user growth and has seen its features being imitated,rather successfully, by its competitors. Meanwhile, Netflix has been gaining value as it attracts brand name investors with its popular original content offerings and user growth, although it has seen its value decline recently along with many other players in the tech sector.

Task 3: Problem(s) Solving

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. CU=40-20=20 CD=Max(15-20,0)=0 Delta=(20-0/40-15)=.8 B=(12/1.05)=11.43 Call=(.8)(25)-(1.05^0)(11.43)=8.57

Put: 8.57 - P = 25 - 20e^(-.05*1) = 2.59

##### 3B) Solve for 1.3.9 Problem/p. 36