Professional Documents
Culture Documents
Liang C. Zhang
in Finance
Introduction to R and Its Applications in Finance
Slides of this presentation: https://goo.gl/4oPIja
R script for this presentation: https://goo.gl/2YKQpq
0.0
-0.2
Drawdowns
-0.4
CBA.AX.Adjusted
-0.6
ANZ.AX.Adjusted
1 Jan/2000 Jan/2001 Jan/2002 Jan/2003 Jan/2004 Jan/2005 Jan/2006 Jan/2007 Jan/2008 Jan/2009 Jan/2010 Jan/2011 Jan/2012 Jan/2013 Jan/2014 Jan/2015 Jan/2016
1
2
Road Map of R in Finance
2
2016/5/13
2016/5/13
Disclaimer
This workshop contains R tools for Technical trading &
Performance analysis that might assist investment decisions.
All analysis are used for demonstrating R only.
In other words,
You are responsible for your own investment decisions if you
use these tools to manage your assets.
3 in Finance
@LiangCZhang
3
2016/5/13
4 in Finance
@LiangCZhang
4
2016/5/13
What is R
Open-source programming language and software
A platform to build their own functions/packages/products
The source code is open for inspection
Free of charge
History
1993: A research project initiated by
Prof. Ross Ihaka and Robert Gentleman
2000: After the hard work of computer
scientists and statisticians R version
1.0.0
Number of packages (Add-ins) grows
exponentially
Source: https://gist.github.com/daroczig/3cf06d6db4be2bbe3368
5 in Finance
@LiangCZhang
5
2016/5/13
Why R in Finance?
Trend over the last decade.
Are you
following or
falling behind
the trend?
Data: Google
trends over the
past 10 years
It measures how
often people
search for the
given term.
Source: Google Trend, http://goo.gl/vO6LhA
6 in Finance
@LiangCZhang
6
2016/5/13
Why R in Finance?
Credit risk analysis Reporting
Popular in industry
7 in Finance
@LiangCZhang
7
2016/5/13
Use R!
Setting up R working environment
Install R – Source code Install RStudio – Interface
https://www.r-project.org/ https://www.rstudio.com/products/RStudio/
in Finance
@LiangCZhang
8
2016/5/13
Use R!
Setting up R working environment (cont.)
Put all files (data, script…)
in the same folder
Always start with opening
the script: EX: Open R in
Finance.R
For setting up working
directory
See whether all files in the
tab of the bottom right pane
Install packages for this
event
Need internet connection
Select the lines of code and
press Ctrl+Enter
in Finance
@LiangCZhang
9
2016/5/13
Use R!
What is an R function?
Recall
You plug any value in x
and you get the results y
An R function follows
exactly the same
mathematical structure.
But 1
∑
Results could be statistics, S
!"
data sets, plots, table…
x is called arguments in R
10 in Finance
@LiangCZhang
10
2016/5/13
Use R!
Functions & R Structure
Build-in Functions Functions from packages R Structure
and loaded first
whenever you install.packages("quantmod")
library(quantmod)
open R
Know how to use these
functions
Know how to use Download manual of the
package
a function EX, quantmod
?functionName, EX https://cran.r-
project.org/web/packages/quantmod/index.
html
?install.packages
11 in Finance
@LiangCZhang
11
2016/5/13
Use R!
R in Finance at a Glance
Beginner Expert
12
2016/5/13
13 in Finance
@LiangCZhang
13
2016/5/13
14 in Finance
@LiangCZhang
14
2016/5/13
15 in Finance
@LiangCZhang
15
2016/5/13
16 in Finance
@LiangCZhang
16
2016/5/13
17
2016/5/13
18 in Finance
@LiangCZhang
18
2016/5/13
Data cleansing
Missing values
Plotting
dimensions
Weird statistics
No zero prices: no zero for the minimum
19 in Finance
@LiangCZhang
19
2016/5/13
Data cleansing
Look for missing values by plotting
A plot without missing values Man-made missing values
CBA.AX$CBA.AX.Adjusted CBA.AX$CBA.AX.Adjusted[-20:-30]
82
82
R code for this figure
> plot(S.CBA.AX$CBA.AX.Adjusted)
80
80
78
78
76
76
74
72 74
72
70
70
Jan 01 2016 Jan 25 2016 Feb 15 2016 Mar 07 2016 Mar 28 2016 Jan 01 2016 Jan 25 2016 Feb 15 2016 Mar 07 2016 Mar 28 2016
20 in Finance
@LiangCZhang
20
2016/5/13
S.CBA.AX [2016-01-01/2016-03-31]
Last 74.92
Technical trading
85
Candelstick chart 80
Recall
Source: Probe-meteo.com
75
70
chartSeries(S.CBA.AX, theme = 60
chartTheme("white",up.col = 40
"green",dn.col = "red")) 20
arguments: Jan 01 2016 Jan 25 2016 Feb 15 2016 Mar 07 2016 Mar 28 2016
21 in Finance
@LiangCZhang
21
2016/5/13
22 in Finance
@LiangCZhang
22
2016/5/13
S.CBA.AX [2016-01-01/2016-03-31]
Last 74.92
Candelstick chart 80
average to be used 40
R code 20
23 in Finance
@LiangCZhang
23
2016/5/13
S.CBA.AX [2016-01-01/2016-03-31]
Last 74.92
Technical trading
85
Bollinger Bands (20,2) [Upper/Lower]: 78.302/73.511
Candelstick chart 80
Arguments
n = periods to average 70
average line 40
20
R code 0
addSMA(n=10,col = "blue") Jan 01 2016 Jan 25 2016 Feb 15 2016 Mar 07 2016 Mar 28 2016
24 in Finance
@LiangCZhang
24
2016/5/13
S.CBA.AX [2016-01-01/2016-03-31]
Last 74.92
Bollinger Bands (20,2) [Upper/Low er]: 78.302/73.511 85
Technical trading
80
Candelstick chart
Add Moving Average
Convergence Divergence line 75
to the chart
Arguments 70
fast=fast period 80
Volume (100,000s):
3,039,100
slow=slow period 60
40
signal=signal period 20
R code 1
Signal: 0.037
25 in Finance
Jan 01 2016 Jan 18 2016 Feb 01 2016 Feb 15 2016 Feb 29 2016 @LiangCZhang
Mar 14 2016 Mar 28 2016
25
2016/5/13
Technical trading
Candelstick chart
Still want to add more
lines to the chart?
Look for any function
starting with add in the
quantmod manual
26 in Finance
@LiangCZhang
26
2016/5/13
Data Transformation
Change frequency R code
M.CBA.AX <- to.period(CBA.AX,period = "months")
Daily to monthly M.ANZ.AX <- to.period(ANZ.AX,period = "months")
them
difference between
Find out the
27 in Finance
@LiangCZhang
27
2016/5/13
Data Transformation
Convert prices to returns R code
price.CBA.AX <- M.CBA.AX[,6]
Adjusted closing price
Return.CBA.AX <-
incorporates the effects of diff(log(price.CBA.AX))[-1]
dividends price.ANZ.AX <- M.ANZ.AX[,6]
Log returns (Yt) Return.ANZ.AX <-
diff(log(price.ANZ.AX))[-1]
28 in Finance
@LiangCZhang
28
2016/5/13
Performance Analysis
Downside risk measures Manual
Be care of the default values
EX: Value at Risk (VaR)
This could be easily got by running
this
VaR(Return.CBA.AX)
But
Significance level or confidence level?
Which method to calculate this
measure?
It turns out that you are using modified
method which might not be what you
want
29 in Finance
@LiangCZhang
29
2016/5/13
Performance Analysis
Density plot CBA.AX.Adjusted
95% VaR
95 % ModVaR
15
Arguments
method = add what line to
the plot
10
break = a single number
Density
giving the number of cells
for the histogram
5
R code
chart.Histogram(Return.CBA.AX, breaks =
50,method=(c("add.rug","add.normal","ad
d.risk","add.density")))
rug(Return.CBA.AX,col = "red")
0
#change rugs' color -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15
Returns
30 in Finance
@LiangCZhang
30
2016/5/13
CBA.AX.Adjusted
0.15 ANZ.AX.Adjusted
0.10
0.05
Returns
0.00
-0.05
-0.10
-0.15
Jan-2001
Jan-2002
Jan-2003
Jan-2004
Jan-2005
Jan-2006
Jan-2007
Jan-2008
Jan-2009
Jan-2010
Jan-2011
Jan-2012
Jan-2013
Jan-2014
Jan-2015
Jan-2016
Mar-2016
31 in Finance
@LiangCZhang
31
2016/5/13
Performance Analysis
Drawdown Plot
Drawdown
summary Drawdown Plot
0.0
Measuring loss in percentage between the peak and the trough
-0.1
-0.2
Drawdowns
chart.Drawdown(
cbind(Return.CBA.AX,Return.ANZ.AX),
-0.3 legend.loc="bottomleft",
ylab="Drawdowns",
date.format= "%b/%Y",
-0.4
main ="Drawdown Plot",
las = 2,
period.areas = c("2007-01-31::2008-12-
-0.5 31","2015-01-31::2016-03-31"),
CBA.AX.Adjusted
ANZ.AX.Adjusted period.color = c("gray"))
Feb/2000
Jan/2001
Jan/2002
Jan/2003
Jan/2004
Jan/2005
Jan/2006
Jan/2007
Jan/2008
Jan/2009
Jan/2010
Jan/2011
Jan/2012
Jan/2013
Jan/2014
Jan/2015
Jan/2016
Mar/2016
32 in Finance
@LiangCZhang
32
2016/5/13
33 in Finance
@LiangCZhang
33
2016/5/13
Source:http://52.2.13.97:3838/fama_french/
34 in Finance
@LiangCZhang
34
2016/5/13
Conclusion:
Where to find the future of finance: Data
35 in Finance
@LiangCZhang
35
2016/5/13
Conclusion:
Embrace innovation, embrace challenge, and embrace R
Software Your field of knowledge
How to learn R by yourself: Always start with something your are familiar with
Excel: Finance
R Through Excel (Heiberger & Analyzing financial data and
Neuwirth, 2009) implementing financial
SAS and SPSS: models using R (Ang, 2015)
R for SAS and SPSS Users An introduction to analysis of
(Muenchen, 2011)
financial data with R (Tsay,
STATA: 2014)
R for Stata Users (Muenchen & Financial risk forecasting
Hilbe, 2010)
(Daníelsson, 2011)
MATLAB:
Computational finance
R and MATLAB (Hiebeler, 2015) (Arratia, 2014)
36 in Finance
@LiangCZhang
36
2016/5/13
References
Ang, C. S. (2015). Analyzing financial data and implementing financial models using R. New York, NY: Springer.
Arratia, A. (2014). Computational finance: An introductory course with R. New York, NY: Springer
Daníelsson, J. (2011). Financial risk forecasting: The theory and practice of forecasting market risk with implementation in R and Matlab. Hoboken, NJ:
John Wiley & Sons.
Muenchen, R. A., & Hilbe, J. (2010). R for Stata users. New York, NY: Springer.
Muenchen, R. A. (2011). R for SAS and SPSS users. New York, NY: Springer.
Hiebeler, D. E. (2015). R and MATLAB: CRC Press.
Heiberger, R. M., & Neuwirth, E. (2009). R through Excel: A spreadsheet interface for statistics, data analysis, and graphics. New York, NY: Springer.
Ryan, J. A., & Ulrich, J. M. (2014). xts: eXtensible Time Series. Retrieved from https://cran.r-project.org/web/packages/xts/index.html
Peterson, B. G., & Carl, P. (2014). PerformanceAnalytics: Econometric tools for performance and risk analysis. Retrieved from http://CRAN.R-
project.org/package=PerformanceAnalytics
The R Trader. (2013). Financial data accessible from R –Part III. Retrieved from http://www.thertrader.com/2013/11/08/financial-data-accessible-
from-r-part-iii/
Tsay, R. S. (2014). An introduction to analysis of financial data with R. New York, NY: John Wiley & Sons.
Ryan, J. A., Ulrich, J. M., & Thielen, W. (2015). quantmod: Quantitative financial modelling framework. R package version 0.3-5. URL http://www.
quantmod. com URL http://r-forge. r-project. org/projects/quantmod. Retrieved from https://cran.r-project.org/web/packages/quantmod/index.html
Zou, J., & Zhang, H. (2014, 27-30 Oct. 2014). High-frequency financial statistics with parallel R and Intel Xeon Phi coprocessor. Paper presented at the
Big Data (Big Data), 2014 IEEE International Conference.
Note: Some references are not here since they are shown with the web links.
37 in Finance
@LiangCZhang
37
2016/5/13
in Finance
@LiangCZhang
38