The two basic factors in finance you are usually interested in are return and risk, where the latter statistically depends on the historic behaviour of the former. Thus, returns are the basic data we need for stastistical analysis. The “quantmod” package in R has useful tools for downloading market data and calculating returns.
Set the working directory and load the library:
setwd(...) library('quantmod') #install.packages('quantmod')
In order to calculate daily returns, R has to download market data for the stocks you are interested in with the command “getSymbols()”. The data include opening, closing, maximum, minimum prices of and the volume that was traded during the day. All you have to do is to enter the ticker of the stock or index you are interested in. If you do not know the ticker I recommend to use Yahoo!Finance. In this example I will use the ticker of the oil producing firm “OMV” which is listed at the Austrian Stock Exchange (ATX).
The command saves the data from January 2012 – specified by the part “from=’2012-01-01′”, where ‘2012-01-01′ is a convenient format for dates in R – to the present in a separate environment. Adding “end=’2013-12-31’ would limit the time window to the years 2012 and 2013. Entering the ticker, here OMV.VI, gives a list of the downloaded values. (If you want to plot it type “chartSeries(OMV.VI)” for a nice graph.
Next, we want to calculate returns. Quantmod provides you with a variety of choices for that. You can calculate daily, weekly, monthly, quarterly or annual returns both in absolute as well in relative values, i.e., approximated percentage changes*. For more information just use “?periodReturn”.
So, let’s calculate daily percentage returns and save them.
There we are. If you type “hist(returns,20)” – 20 in this case defines the number of bars in the plot – the plotted histogram should show something that looks like, but actually isn’t, a normal distribution.
The beta-factor gives the sensibility of a stock to movements of the index in which it is listed. It is an eminent part of the CAPM, where it serves as a parameter for the calculation of the expected returns of a portfolio.
In order for the code to work you also have to install the “stringr”-package that allows you to manipulate string characters. but there is no necessity to do it. I only included it in the code since R will do everything else after you entered the needed information.
setwd("...") rm(list=ls()) library(quantmod) library(stringr)
After loading the relevant packages you can enter the ticker of the stock and the index in which it is listed. Finally, you have to specify from which date on you want to use data. Ultimately, this is the question how many observations you want to have. From then on R does the rest: It loads the data, calculates daily log-returns* – i.e. approximate percentage changes -, puts these lists into a single data frame** and estimates a simple OLS regression of the stock returns on percentage changes of the index. The results are then summarized in a data frame.
index<-"^ATX" stock<-"OMV.VI" time<-"2009-01-01" getSymbols(index,from=time) getSymbols(stock,from=time) index.data<-dailyReturn(log(get(str_replace(index,"[[:punct:]]","")))) stock.data<-dailyReturn(log(get(stock))) data<-merge(stock.data,index.data) lm.beta<-summary(lm(data[,1] ~ data[,2])) data.frame(Beta=lm.beta$coefficients[2,1], sd=lm.beta$coefficients[2,2], t.value=lm.beta$coefficients[2,3])
*Here the command from the “stringr”-packages is needed since you have to get rid of the “^” in front of the index ticker. Otherwise you cannot use the get()-command to get the index data.
**This is necessary, because the data downloaded from the internet differ in length, i.e. some days are captured in one list but not in the other. Thus, since both lists have indices containing the date of the observation, they are merged in a data frame in accordance to the date. For the regression only those observations are used, where there is data both for the stock and the index.