Abstract
In this workshop we practice the following topics: a) introduction to time-series variables, b) online financial data collection, and c) calculation of historical time-series financial returns.You will work in RStudio. Create an R Notebook document (File -> New File -> R Notebook), where you have to write whatever is asked in this workshop.
Create an R Notebook document (File -> New File -> R Notebook), where you have to write whatever is asked in this workshop. More specifically, you have to:
Replicate all the R Code along with its output.
You have to respond to the CHALLENGES of the workshop and any other question asked in the workshop.
Any QUESTION or any INTERPRETATION you need to do will be written in CAPITAL LETTERS. For ANY QUESTION or INTERPRETATION, you have to RESPOND IN CAPITAL LETTERS right after the question.
You have to keep saving your .Rmd file, and ONLY SUBMIT the .html version of your .Rmd file. Pay attention in class to know how to generate an html file from your .Rmd.
Setup title and name of your Workshop
Once you have created a new R Notebook, you will see a sample R Notebook document. You must DELETE all the lines of this sample document except the first lines related to title and output. As title, write the workshop # and course, and add a new line with your name. You have to end up with something like:
title: “Workshop 1, Financial Econometrics II”
author: YourName
output: html_notebook
Now you are ready to continue writing your first R Notebook.
You can start writing your own notes/explanations we cover in this workshop. When you need to write lines of R Code, you need to click Insert at the top of the RStudio Window and select R. Immediately a chunk of R code will be set up to start writing your R code. You can execute this piece of code by clicking in the play button (green triangle).
Note that you can open and edit several R Notebooks, which will appear as tabs at the top of the window. You can visualize the output (results) of your code in the console, located at the bottom of the window. Also, the created variables are listed in the environment, located in the top-right pane. The bottom-right pane shows the files, plots, installed packages, help, and viewer tabs.
Save your R Notebook file as W1-YourName.Rmd. Go to the File menu and select Save As.
To generate the .html file, you have knit your R Notebook. Pay attention how to do this in class.
In this section we will download time-series variables from Yahoo Finance and explore the time-series datasets, more specifically, we explore the xts-zoo R objects. xts stands for “eXtensible Time Series”, and zoo is an R class for general time-series datasets.
We start clearing our R environment:
rm(list=ls())
# To avoid scientific notation for numbers:
options(scipen=999)
In order to import and manage financial data in R, the quantmod package must be installed. This package contains the getSymbols() function, which creates an xts (extensible time series) object in the environment with the downloaded data from the Internet. In order to install packages in R, go to the Package tab in the bottom-right section of RStudio, select Install and then type quantmod, and the botton Install.
Once you install a package, this package will be in your computer forever. You might re-install a package in case there is a new version of the package.
Now, you have installed a package and it is not necessary to install it again in further occasions. It will stay in your computer. However, next time you want to use it, you have to load it using the library() function
library(quantmod)
The getSymbols() function enables its user to download online and up-to-date financial data, such as stock prices, market indexes, ETF prices, interest rates, exchange rates, etc. getSymbols() allows to download this data from multiple sources: Yahoo Finance, Google Finance, FRED and Oanda. These sources have thousands of finance and economic data series from many market exchanges and other macroeconomic variables around the world.
We download the main monthly market indexes of Mexico (the IPCyC) and the US (the S&P500) from Yahoo from 2011:
getSymbols(c("^MXX", "^GSPC"), from="2011-01-01", periodicity = "monthly", src = "yahoo")
## [1] "^MXX" "^GSPC"
This function will create 2 xts-zoo R objects with historical data for each market index in a chronological order. These xts-zoo R objects are actually datasets with the index historical values and with a time index. Each R object has at specific class. In this case, the class of these datasets is called xts-zoo. xts stands for extensible time-series. An xts-zoo object is designed to easily manipulate time series data.
For each period, Yahoo Finance keeps track of the open, high, low, close (OHLC) and adjusted prices. Also, it keeps track of volume that was traded (# of shares traded) in every specific period. The adjusted prices are used for stocks, not for market indexes. Adjusted prices consider dividend payments and stock splits. For the case of market indexes, the adjusted prices are always equal to the close prices.
Let’s see some of the benefits of using xts-zoo objects. We can, for example, select columns using any of the following functions, where x
represents a generic xts
zoo
object:
Op(x)
: Extract the Opening prices of the period.Hi(x)
: Extract the Highest price of the period.Lo(x)
: Extract the Lowest price of the period.Cl(x)
: Extract the closing prices of the period.Vo(x)
: Extract the volume traded of the period.Ad(x)
: Extract the Adjusted prices of the period.We can integrate xts-zoo R objects into one xts-zoo dataset using the merge function. In this case, we will only use the adjusted price, so we can also use the Ad function:
# We merge the datasets into a new R object called prices:
= merge(MXX,GSPC)
prices # We only keep the adjusted price columns:
= Ad(prices)
prices # We rename the columns with simpler names:
names(prices) = c("MXX","GSPC")
For each index we do a graph to visualize how the index moves over time. We can use the chartSeries function from the quantmod package:
chartSeries(MXX, theme=("white"))
chartSeries(GSPC, theme=("white"))
Respond to the following QUESTION:
WHAT YOU CAN SAY ABOUT THE TREND OF BOTH MARKET INDEXES? IS IT CONSTANTLY GROWING, OR DECLINING, OR THERE IS NO CLEAR TREND? BRIEFLY EXPLAIN
Generate a new dataset with the natural log of both indexes as follows:
= log(prices) lnprices
Now do a time plot for the log of the US index:
plot(lnprices$GSPC, main = "Log of the US Index over time")
QUESTION: COMPARED WITH THE PLOT OF THE US INDEX, WHAT DIFFERENCES DO YOU SEE IN THIS PLOT OF THE LOG OF THE US INDEX? BRIEFLY EXPLAIN
EXERCISE: Do the plot for the log of the Mexican market index
A financial simple return for a stock (\(R_{t}\)) is calculated as a percentage change of price from the previous period (t-1) to the present period (t):
\[ R_{t}=\frac{\left(Adjprice_{t}-Adjprice_{t-1}\right)}{Adjprice_{t-1}}=\frac{Adjprice_{t}}{Adjprice_{t-1}}-1 \] For example, if the adjusted price of a stock at the end of January 2021 was $100.00, and its previous (December 2020) adjusted price was $80.00, then the monthly simple return of the stock in January 2021 will be:
\[ R_{Jan2021}=\frac{Adprice_{Jan2021}}{Adprice_{Dec2020}}-1=\frac{100}{80}-1=0.25 \]
We can use returns in decimal or in percentage (multiplying by 100). We will keep using decimals.
In Finance it is very recommended to calculate continuously compounded returns (cc returns) and using cc returns instead of simple returns for data analysis, statistics and econometric models. cc returns are also called log returns.
One way to calculate cc returns is by subtracting the log of the current adjusted price (at t) minus the log of the previous adjusted price (at t-1):
\[ r_{t}=log(Adjprice_{t})-log(Adjprice_{t-1}) \] This is also called as the difference of the log of the price.
We can also calculate cc returns as the log of the current adjusted price (at t) divided by the previous adjusted price (at t-1):
\[ r_{t}=log\left(\frac{Adjprice_{t}}{Adjprice_{t-1}}\right) \]
cc returns are usually represented by small r, while simple returns are represented by capital R.
It is recommended to always use adjusted prices to calculate financial returns. In this example that we have market indexes, the adjusted price is exactly the same as the closing price since market indexes do not have stock splits nor dividend payments.
We can use the lag function to get past (lagged) values of a time-series dataset (or column). With this function we can get the price of the previous period to calculate the simple return. Let’s create a new dataset for the simple monthly returns of both indexes:
= prices / lag(prices,n=1) - 1 R
We can use the diff function to get the difference of a current value and a lagged value of a dataset (or a column). Let’s create a new dataset for the cc return of both indexes:
= diff(log(prices)) r
Remember that the continuously compounded returns can be calculated as the difference between the log of the price of today minus the log of the price of the previous period.
Now do a time graph for the cc returns of the Mexican index:
plot(r$MXX, col = "darkblue",
main = "cc return for the MXX index")
RESPOND TO THE FOLLOWING QUESTIONS:
(a) DOES THIS SERIES HAVE ABOUT THE SAME MEAN FOR ALL TIME PERIODS?
(b) DOES IT HAVE THE SAME STANDARD DEVIATION (VOLATILITY) FOR ALL TIME PERIODS?
Read/skim the note: “Introduction to time series”. With your own words:
a) EXPLAIN WHAT IS A STATIONARY SERIES.
b) WHICH ARE THE CONDITIONS OF A SERIES TO BE CONSIDERED AS A STATIONARY SERIES?
You will have access to ALL Datacamp courses (datacamp.com) for 6 months with no cost for you. If you have never used R/RStudio, I STRONGLY recommend the Introduction to R course.
Go to Canvas and respond Quiz 1. You will have 3 attempts. Questions in this Quiz are related to concepts of the readings related to this Workshop.
The grade of this Workshop will be the following:
Remember that you have to submit your .html file through Canvas BEFORE NEXT CLASS.