- Introduction to Quantitative Trading
- Quantitative Trading - Advantages and Disadvantages
- Types of Quantitative Trading Strategies
- Momentum Strategies
- Mean Reversion Strategies
- Market Making Strategies and Day Trading Strategies
- How to Generate Trading Ideas
- Designing A Trading Strategy For Profit
- Backtesting a Trading Strategy - Considerations
- Risk Management of a Trading Strategy
- Risk Indicators - VIX Index and TED Spread
- Plotting the VIX Index and TED Spread in R
- Introduction to Quantmod in R
- Downloading Data Using Quantmod Package in R
- Creating Charts with Quantmod
- Data Analysis with Quantmod in R
- Measuring Overall ETFs Performance
- Quantstrat Example in R - EMA Crossover Strategy
- Quantstrat - EMA Crossover Strategy - Performance and Risk Metrics
- Quantstrat Example in R - RSI Strategy
- Quantstrat Case Study - Multiple Symbol Portfolio
Plotting the VIX Index and TED Spread in R
In this lesson, will will look at how to create the two graphs in R. Before you start, it is important to setup your basic environment:
Setup Environment
Open RStudio and ensure that you have the basic setup in place.
- Create a new directory called ‘
quantative-trading-strategies-r
’ in your computer. We will use this director for all the exercises in this course. - Set this directory as your working directory using the
setwd()
command. You can also check the current working directory using thegetwd()
command in R console. - Use the command
CTRL+L
to clear the console (Use this whenever you feel the console is cluttered). - Create a new R script where we will be writing our R script.
- Install and load the
ggplot2
package by typing the following commands in the console:
#Install ggplot2
install.packages("ggplot2")
#Load ggplot2
library("ggplot2")
Plot the TED Spread
In order to plot the TED spread, you can follow the following instructions:
Step 1: Download Data
You can download the data for TED spread from the FRED® Economic Data website.
Get TED data from https://fred.stlouisfed.org/series/TEDRATE
For convenience, we have also provided the data files below.
You can download the data file and save it in your working directory.
Step 2: Load the data in your R Environment
To read a csv with R, the best way to do it is with the command read.csv
.
ted <- read.csv("TEDRATE.csv",stringsAsFactors = FALSE)
Step 3: Transform Data
The read.csv function interpret both columns as factors columns, so we need to change to the appropriate columns data types.
We will convert character columns to numeric and date columns, and remove NA values with complete.cases.
The complete.cases function returns a logical vector specifying which observations/rows have no missing values across the entire sequence.
ted$TEDRATE <- as.numeric(ted$TEDRATE)
ted$DATE <- as.Date(ted$DATE)
ted <- ted[complete.cases(ted),]
Finally, we have ‘ted’ as a dataframe containing our prepared data.
Step 4: Plot the Graph
Now, we can use ggplot2 library to plot the graph.
ggplot(ted, aes(DATE, TEDRATE)) + geom_line(color = "red") + xlab("") + ylab("Daily Prices")+ggtitle("TED Spread")
The complete script for creating the TED spread is provided below:
# Get TED data from https://fred.stlouisfed.org/series/TEDRATE
# Load the data using read.csv
ted <- read.csv("TEDRATE.csv",stringsAsFactors = FALSE)
# The read.csv function interpret both columns as factors columns, so we need to change
# to the appropriate columns data types. Convert character columns to numeric and date
# columns, and remove NA values with complete.cases
ted$TEDRATE <- as.numeric(ted$TEDRATE)
ted$DATE <- as.Date(ted$DATE)
ted <- ted[complete.cases(ted),]
# View the data
ted
# Plot the graph
ggplot(ted, aes(DATE, TEDRATE)) + geom_line(color = "red") + xlab("") + ylab("Daily Prices")+
ggtitle("TED Spread")
Plot the VIX Index
You can follow the exact same steps above to plot the VIX volatility index. The complete script for it is provided below:
# Get VIX data from https://fred.stlouisfed.org/series/VIXCLS
# Load the data using read.csv
VIX <- read.csv("VIXCLS.csv",stringsAsFactors = FALSE)
# Convert character columns to numeric and date columns, and
# remove NA values # with complete.cases
VIX$VIXCLS <- as.numeric(VIX$VIXCLS)
VIX$DATE <- as.Date(VIX$DATE)
VIX <- VIX[complete.cases(VIX),]
# View the data
VIX
# Plot the graph
ggplot(VIX, aes(DATE, VIXCLS)) + geom_line(color = "blue") + xlab("") + ylab("Daily Prices")+
ggtitle("VIX Prices")
Related Downloads
Data Science in Finance: 9-Book Bundle
Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.
What's Included:
- Getting Started with R
- R Programming for Data Science
- Data Visualization with R
- Financial Time Series Analysis with R
- Quantitative Trading Strategies with R
- Derivatives with R
- Credit Risk Modelling With R
- Python for Data Science
- Machine Learning in Finance using Python
Each book includes PDFs, explanations, instructions, data files, and R code for all examples.
Get the Bundle for $29 (Regular $57)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.