R Programming
for Quantitative Finance
(Jerry Wu)
(R & RStudio)
quantmod forecast Rbitcoin igraph plyr
What is R
• R is one of the most popular platforms for data analysis
and visualization currently available.
dsjerry2017@gmail.com
Some news about R
http://spectrum.ieee.org/computing/software/the-2016-top-programming-languages
dsjerry2017@gmail.com
A brief history of R
Time Topic
1993 Research	project	in	Auckland,	NewZealand
Ross	Ihaka and	Robert	Gentlemen
1995 Released	as	open-source	software
Generally	compatible	with	the	‘S’	language
1997 R	core	group	formed
2000 R	1.0.0	released
2004 First	international	user	conference	in Vienna
2012 R	2.15.2	released
2013 R	3.0.2	released
The	CRAN	package	repository	features	5,026	available	packages.
2017 R	3.4.1	released
The	CRAN	package	repository	features	10,875available	packages.dsjerry2017@gmail.com
Data analysts captivated by R’s
Power
dsjerry2017@gmail.com
Why “R”? Not “SAS”? Or others?
• 2009
• R (lingua franca)
• Google, Microsoft, Facebook, Johnson & Johnson,Pfizer, Merck, BOA,
InterContinental Hotels and Shell…, including New York Times now use it !
•
•
•
• To give researchers of all stripes an accessible data
analysis tool.
• 10,875 (2017)
dsjerry2017@gmail.com
R may be the disruptor !
dsjerry2017@gmail.com
The Tools We’re Using – Ranked by
Usage
• R is the tool used by the most data miners (70%)
Rexer Analyticsdsjerry2017@gmail.com
The Popularity of R Software is
Skyrocketing
• 70% R
• 24% R
dsjerry2017@gmail.com
Discussion lists and sites
dsjerry2017@gmail.com
KDnuggets
Four of the top five packages were open source
dsjerry2017@gmail.com
Hits on Google Scholar
• Google R
dsjerry2017@gmail.com
Why use R
• (Free/open source software)
•
•
•
•
•
dsjerry2017@gmail.com
Task Views
Bayesian
ChemPhys ,
ClinicalTrials
Cluster
DifferentialEquations
Distributions
Econometrics
Environmetrics
ExperimentalDesign
Finance
dsjerry2017@gmail.com
Task Views
Genetics
Graphics
HighPerformanceComputing
MachineLearning
MedicalImaging
MetaAnalysis
Multivariate
NaturalLanguageProcessing
NumericalMathematics
OfficialStatistics
Optimization dsjerry2017@gmail.com
Task Views
Pharmacokinetics
Phylogenetics
Psychometrics
ReproducibleResearch
Robust
SocialSciences
Spatial
SpatioTemporal
Survival
TimeSeries
WebTechnologies
gR dsjerry2017@gmail.com
Outline
• The Basics
• Vectors and Matrices, Data Frames, Factors, Lists
• Exploratory time series data analysis
• Stock Trading
• Quantitative Analysis
• Correlation analysis
• MA Model Estimation and Forecasting
• Querying the Bitcoin blockchain with R
dsjerry2017@gmail.com
Stock Returns
• $90 worth of Foxconn Stock
• 10% return in March
• 110% = 100% + 10%
• 1.10 = 1 + 0.10
• 99 = 90 * 1.10
+
-
Chapter1_stock_returns.R dsjerry2017@gmail.com
Stock Returns – Multiple periods
• $90 worth of Foxconn Stock
• 10% return in March
• 110% = 100% + 10%
• 1.10 = 1 + 0.10
• 99 = 90 * 1.10
• 5% return in April
• 110% = 100% + 5%
• 1.05 = 1 + 0.05
• 94.5 = 90 * 1.05
• 103.95 = 90 * 1.10 * 1.05
Chapter1_stock_returns.R dsjerry2017@gmail.com
Fun Time
• $100 worth of Stock
• 10% return in March
• 5% return in April
dsjerry2017@gmail.com
The Basics
• Basic Data Types
• Vectors and Matrices
• Data Frames
• Factors
• Lists
dsjerry2017@gmail.com
Basic Data Types
• Numeric
• Character
• Logical
Chapter1_stock_returns.R dsjerry2017@gmail.com
Basic Data Types
• Variables and data types
• Class()
Chapter1_stock_returns.R dsjerry2017@gmail.com
The Basics
• Basic Data Types
• Vectors and Matrices
• Data Frames
• Factors
• Lists
dsjerry2017@gmail.com
Vector – Vectors and stock prices
• Vectors and stock prices
• Vector names()
Chapter2_vectors_and_matrices.R
dsjerry2017@gmail.com
Vector manipulation
Chapter2_vectors_and_matrices.R
dsjerry2017@gmail.com
Fun Time
• Try
• + – * /
Chapter2_vectors_and_matrices.R
dsjerry2017@gmail.com
A 2D vector
Row
Col
Chapter2_vectors_and_matrices.R
dsjerry2017@gmail.com
cbind() and rbind()
Chapter2_vectors_and_matrices.R
dsjerry2017@gmail.com
Subsetting
Chapter2_vectors_and_matrices.R
dsjerry2017@gmail.com
Fun Time –
What’s incorrect about subsetting
• Tsmc and foxconn columns
A. M_tsmc_foxconn[,c(“tsmc”, “foxconn”)]
B. M_tsmc_foxconn[c(“tsmc”, “foxconn”)]
C. M_tsmc_foxconn[,]
dsjerry2017@gmail.com
Cor()relation
• +1 perfect positive linear relationship
• -1 perfect negative linear relationship
• 0 no linear relationship
Chapter2_vectors_and_matrices.R
dsjerry2017@gmail.com
Visualize your matrix
Chapter2_vectors_and_matrices.R
dsjerry2017@gmail.com
Visualize your matrix (Time Series)
Chapter2_vectors_and_matrices.R
dsjerry2017@gmail.com
The Basics
• Basic Data Types
• Vectors and Matrices
• Data Frames
• Factors
• Lists
dsjerry2017@gmail.com
Data Frames and Name that frame
Chapter3_data_frame.R
dsjerry2017@gmail.com
Data frame subsets and subset()
Chapter3_data_frame.R
dsjerry2017@gmail.com
Present value( )
•
Cash / (1 + X /100) ^ Periods( )
Cash * (1 + X / 100) ^ -Periods( )
Chapter3_data_frame.R
dsjerry2017@gmail.com
1000 6%
839.62 [1000	/	(1	+	6%)^3]
X	=
Present value( )
Chapter3_data_frame.R
dsjerry2017@gmail.com
Present value( )
• Fun Time ( 5%) 4
Chapter3_data_frame.R
dsjerry2017@gmail.com
The Basics
• Basic Data Types
• Vectors and Matrices
• Data Frames
• Factors
• Lists
dsjerry2017@gmail.com
Stock and Bond
Chapter4_factor.R
dsjerry2017@gmail.com
Cut()
Chapter4_factor.R
dsjerry2017@gmail.com
Plot factor
Chapter4_factor.R
dsjerry2017@gmail.com
Order()
Chapter4_factor.R
dsjerry2017@gmail.com
Subsets
Chapter4_factor.R
dsjerry2017@gmail.com
The Basics
• Basic Data Types
• Vectors and Matrices
• Data Frames
• Factors
• Lists
dsjerry2017@gmail.com
List
Chapter5_list.R
dsjerry2017@gmail.com
Split()
Chapter5_list.R
dsjerry2017@gmail.com
Fun time
• 1 1000 ?
Chapter5_list.R
dsjerry2017@gmail.com
Fun time
List
DF
Chapter5_list.R
dsjerry2017@gmail.com
Attributes
Chapter5_list.R
dsjerry2017@gmail.com
Data Input and output
Chapter6_io.R
dsjerry2017@gmail.com
Outline
• The Basics
• Vectors and Matrices, Data Frames, Factors, Lists
• Exploratory time series data analysis
• Stock Trading
• Quantitative Analysis
• Correlation analysis
• MA Model Estimation and Forecasting
• Querying the Bitcoin blockchain with R
dsjerry2017@gmail.com
Exploratory time series data analysis
• (Time Series)
• GDP ..
dsjerry2017@gmail.com
Exploratory time series data analysis
•
•
dsjerry2017@gmail.com
Exploratory time series data analysis
•
•
dsjerry2017@gmail.com
Exploratory time series data analysis
• : , ,
• (Data Granularity)
• : , ,
•
dsjerry2017@gmail.com
Exploratory time series data analysis
•
• {yt }
• (state space) S
• (Discrete)
• (Continuous)
dsjerry2017@gmail.com
Exploratory time series data analysis
• (index set)
•
• (Discrete) T={0,1,2……}
•
• (Continuous) T=[0, ∞)
dsjerry2017@gmail.com
Exploratory time series data analysis
Chapter7_EDA.R
dsjerry2017@gmail.com
Exploratory time series data analysis
0501
0501
Chapter7_EDA.R
dsjerry2017@gmail.com
Fun Time
Chapter7_EDA.R
dsjerry2017@gmail.com
Exploratory time series data analysis
Chapter7_EDA.R
dsjerry2017@gmail.com
Exploratory time series data analysis
Chapter7_EDA.R
dsjerry2017@gmail.com
Exploratory time series data analysis
• Moving Average Convergence / Divergence, MACD
DIF-MACD
MACD
DIF( )
Chapter7_EDA.R
dsjerry2017@gmail.com
Fun time
addADX
addATR
addBBands
addCCI
addCMF
addCMO
addDEMA
addDPO
addEMA
addEnvelope
addEVWMA
addExpiry
addMACD
addMomentum
addROC
addRSI
addSAR
addSMA
addSMI
addTRIX
addVo
addWMA
addZLEMAdsjerry2017@gmail.com
Basic Time Series Objects
Chapter7_EDA.R
dsjerry2017@gmail.com
Basic Time Series Objects
Chapter7_EDA.R
dsjerry2017@gmail.com
Outline
• The Basics
• Vectors and Matrices, Data Frames, Factors, Lists
• Exploratory time series data analysis
• Stock Trading
• Quantitative Analysis
• Correlation analysis
• MA Model Estimation and Forecasting
• Querying the Bitcoin blockchain with R
dsjerry2017@gmail.com
Stock Trading
Chapter7_EDA.R
dsjerry2017@gmail.com
Stock Trading
• 20ma 60ma
• 20ma 60ma ( )
Lag
ROC log( / ) *	Strategy (0,1)
Cl OHLC
cumsum
exp
Chapter7_EDA.R
dsjerry2017@gmail.com
Fun time
•
dsjerry2017@gmail.com
Outline
• The Basics
• Vectors and Matrices, Data Frames, Factors, Lists
• Exploratory time series data analysis
• Stock Trading
• Quantitative Analysis
• Correlation analysis
• MA Model Estimation and Forecasting
• Querying the Bitcoin blockchain with R
dsjerry2017@gmail.com
Correlation analysis
dsjerry2017@gmail.com
Correlation analysis
dsjerry2017@gmail.com
Correlation analysis
• Rapid Growth
dsjerry2017@gmail.com
Correlation analysis
• Periodic
dsjerry2017@gmail.com
Correlation analysis
• (linearized)
dsjerry2017@gmail.com
Correlation analysis
• (Periodic)
dsjerry2017@gmail.com
Correlation analysis
Diff( )
Log( )(linearized)
(Periodic)
Chapter8_QA.R
Diff 12 x 13 y =1+(y-x)/x
dsjerry2017@gmail.com
Correlation analysis - Scatterplots
Chapter8_QA.R
dsjerry2017@gmail.com
Correlation analysis – correlation coefficient
http://www.stat.nuk.edu.tw/prost/content2/statics_6.htm
+1	
0	
-1	
dsjerry2017@gmail.com
Correlation analysis – correlation coefficient
P-value 0.05
df
Chapter8_QA.R
dsjerry2017@gmail.com
Fun time
•
•
dsjerry2017@gmail.com
Outline
• The Basics
• Vectors and Matrices, Data Frames, Factors, Lists
• Exploratory time series data analysis
• Stock Trading
• Quantitative Analysis
• Correlation analysis
• MA Model Estimation and Forecasting
• Querying the Bitcoin blockchain with R
dsjerry2017@gmail.com
MA Model Estimation and Forecasting
• ARIMA Autoregressive
Integrated Moving Average model,
)
• ARIMA p d q
• p
• d TS
• q
• ARIMA ACF
PACF p,d,q
• ACF(Sample Autocorrelation Function,
)
• PACF (Sample Partial Autocorrelation
Function, )
ACF
PACF
Chapter8_QA.R
dsjerry2017@gmail.com
MA Model Estimation and Forecasting
Lag=1
Lag=3
arima(x,	order	=	c(0,0,1))
arima(x,	order	=	c(3,0,1))
AIC( )
(Goodness	of	Fit)
AICChapter8_QA.R
dsjerry2017@gmail.com
MA Model Estimation and
Forecasting(Non-Auto ARIMA)
Chapter8_QA.R
dsjerry2017@gmail.com
MA Model Estimation and
Forecasting(Non-Auto ARIMA)
Chapter8_QA.R
dsjerry2017@gmail.com
MA Model Estimation and
Forecasting(Auto ARIMA)
Chapter8_QA.R
dsjerry2017@gmail.com
MA Model Estimation and Forecasting
dsjerry2017@gmail.com
Outline
• The Basics
• Vectors and Matrices, Data Frames, Factors, Lists
• Exploratory time series data analysis
• Stock Trading
• Quantitative Analysis
• Correlation analysis
• MA Model Estimation and Forecasting
• Querying the Bitcoin blockchain with R
dsjerry2017@gmail.com
Bitcoin
dsjerry2017@gmail.com
Bitcoin
• (Blockchain)
•
• 0.00000001 1
• 2140 2100
• = 90,375
https://zh.wikipedia.org/wiki/
dsjerry2017@gmail.com
Blockchain
Blockchain is	a distributed	database that	is	used	to	maintain	a	
continuously	growing	list	of records,	called blocks.	Each	block	contains	
a timestamp and	a	link	to	a	previous	block.
A	blockchain is	typically	managed	by	a	peer-to-peer network	collectively	
adhering	to	a	protocol	for	validating	new	blocks.
https://medium.com/@micheledaliessi/how-does-the-blockchain-work-98c8cd01d2ae
dsjerry2017@gmail.com
Querying the Bitcoin blockchain with R
Chapter9_Blockchain.R
dsjerry2017@gmail.com
Querying the Bitcoin blockchain with R
Chapter9_Blockchain.R
dsjerry2017@gmail.com
Conclusion
• Concept of R basic
• Exploratory time series data
• Learn stock trading with R
• Quantitative analysis with forecasting
• Querying the Bitcoin Blockchain Visualization with R
dsjerry2017@gmail.com
dsjerry2017@gmail.com
jerry@mail.ntust.edu.tw

廣宣學堂: R programming for_quantitative_finance_0623