Financial Time Series Data

Welcome to this course on financial time series analysis using R. In this course, we will learn about financial time series data analysis in R. You will learn about how to explore and build time series data, calculate its key statistics, and plot time series charts. You will also learn about how to use the important time series models such as White Noise, Random Walk, Autoregression and Moving Average, learn how to simulate these models, fit these models into financial time series data and use the models to predict the future. So, let's get started...

What is Time Series?

Time series refers to a series of data in a chronological order. A lot of data in this world is recorded sequentially, over time, in the form of time series. Some common examples include the weather in a city over time, the prices of a listed stock, the commodity prices and so on. While studying financial assets, the asset prices as well as asset returns are represented as time series. Investors generally prefer to use asset returns, over asset prices, in their analysis. This is primarily for two reasons: 1) the asset returns provide a complete and scale-free summary of asset returns and 2) the asset returns are easier to analyze compared to asset prices because of their statistical properties. In our lessons, we will use examples of both asset prices and asset returns.

Financial Time Series Data

There are plenty of financial time series data sources on the Internet. One popular source is Quandl, which contains thousands of datasets including financial and economic datasets.

For the purpose of this course, I would suggest you to signup for a free account on Quandl.com. Once you signup, you will get an API key that you can use to fetch data directly in R from Quandl.

Install Quandl R Package

In order to work with Quandl datasets in R, you need to install and load the Quandl package.

install.packages("Quandl")
library(Quandl)

Load Sample Financial Data

Now that you have your Quandl API key and have loaded Quandl package in your R session, you can download some financial data. In the following example, I am loading the daily closing prices of the Microsoft stock (Symbol: MSFT), for the year 2016 from the WIKI/PRICES dataset in Quandl.

In the following code, add your own api key and then run it in your R console. The code will create an R time series object containing Microsoft stock prices for the year 2016. If you don't understand what's happening in this code, don't worry about it. We will be studying in detail about how to create time series objects in the coming lessons.

Quandl.api_key("YOUR API KEY")
msft_data <- Quandl.datatable("WIKI/PRICES" ,
                           qopts.columns=c("date", "close"),
                           ticker=c("MSFT"),
                           date.gte=c("2016-01-01"),
                           date.lte=c("2016-12-31"))
ZOO <- zoo(msft_data$close, order.by=as.Date(as.character(msft_data$date), format='%Y-%m-%d'))
msft_ts <- ts(ZOO)

View Time Series

The easiest way to explore a time series is to view or print it as shown below:

< print(msft_ts)
Time Series:
Start = 1
End = 252
Frequency = 1
  [1] 54.800 55.050 54.050 52.170 52.330 52.300 52.780 51.640 53.110 50.990 50.560 50.790 50.480
 [14] 52.290 51.790 52.170 51.220 52.055 55.090 54.710 53.000 52.160 52.000 50.160 49.410 49.280
 [27] 49.710 49.690 50.500 51.090 52.420 52.190 51.820 52.650 51.180 51.360 52.100 51.300 50.880
 [40] 52.580 52.950 52.350 52.030 51.030 51.650 52.840 52.050 53.070 53.170 53.590 54.350 54.660
 [53] 53.490 53.860 54.070 53.970 54.210 53.540 54.710 55.050 55.230 55.570 55.430 54.560 55.120
 [66] 54.460 54.420 54.310 54.650 55.350 55.360 55.650 56.460 56.390 55.590 55.780 51.780 52.110
 ...
 ...
 ...

As you can see, the result explicitly tells us that it is a time series, which starts with 1 and ends at 252, i.e., there are a total of 252 observations with a frequency of 1.

In the next lesson, we will learn more about exploring the time series.

install.packages("readr") 
library(readr) 
msft_data <- read_csv("msft_data.csv") 
View(msft_data)

Lesson Resources

All Users

Related Downloads

Membership
Learn the skills required to excel in data science and data analytics covering R, Python, machine learning, and AI.
I WANT TO JOIN
JOIN 30,000 DATA PROFESSIONALS

Free Guides - Getting Started with R and Python

Enter your name and email address below and we will email you the guides for R programming and Python.

Saylient AI Logo

Take the Next Step in Your Data Career

Join our membership for lifetime unlimited access to all our data analytics and data science learning content and resources.