Saturday, January 26, 2019

Material covered in Dr. Krause’s FinTech Topics course at Marquette University (Week 2, Winter/Spring semester)



Learning to use “Financial R Packages” including QuantMod and TTR in FINA 4931: Topics in FinTech at Marquette University

While Martin Luther King Jr. Holiday and some significant winter weather limited the class meeting time during the second week of the semester for FINA 4931: Topics in Financial Technologies, the students were introduced to two financial R packages: QuantMod and TTR. They have several R coding assignments due at the end of the week utilizing these packages.
The QuantMod package was originally designed to assist the quantitative securities trader in the development, testing, and deployment of statistically based trading models.
Getting data: The most useful function in QuantMod is getSymbol(), which allows to conveniently load data from several websites like YahooFinance, GoogleFinance, FRED, etc. While Apple (AAPL) is used as the example below, any security with a ticker recognized by YahooFinance could be used. The basic R code follows:
> library(QuantMod)

> getSymbols(c("AAPL", "GOOG"), from = "2018-07-01", to = "2019-1-26")
#[1] "AAPL" "GOOG"

> str(AAPL)
#An ‘xts’ object on 2018-07-02/2019-01-25 containing:
# Data: num [1:139, 1:6] 184 188 185 185 190 ...
# - attr(*, "dimnames")=List of 2
#  ..$ : NULL
#  ..$ : chr [1:6] "AAPL.Open" "AAPL.High" "AAPL.Low" "AAPL.Close" ...
#  Indexed by objects of class: [Date] TZ: UTC
#  xts Attributes: 
# List of 2
# $ src    : chr "yahoo"
# $ updated: POSIXct[1:1], format: "2019-01-25 18:11:21"
 
> head(AAPL)
#           AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
#2018-07-02    183.82    187.30   183.42     187.18    17731300      185.8773
#2018-07-03    187.79    187.95   183.54     183.92    13954800      182.6400
#2018-07-05    185.26    186.41   184.28     185.40    16604200      184.1096
#2018-07-06    185.42    188.43   185.20     187.97    17485200      186.6618
#2018-07-09    189.50    190.68   189.30     190.58    19756600      189.2536
#2018-07-10    190.71    191.28   190.18     190.35    15939100      189.0252


Charting with QuantMod: The function chartSeries() is a nice tool to visualize financial time series in a way that many practicioners are familiar with–line charts, as well as OHLC bar and candle charts. There are convenience wrappers to these different styles (lineChart()barChart(), and candleChart()), though chartSeries() does quite a bit to automatically handle data in the most appropriate way.

> chartSeries(AAPL["2018-8/2019-1"], name = "AAPL")


 
# Add multi-coloring and change background to white 
> candleChart(AAPL["2018-8/2019-1"], multi.col = TRUE, theme = "white", name = "AAPL")


# Now weekly with custom color candles using the QuantMod function to.weekly 
> chartSeries(to.weekly(AAPL), up.col = "green", dn.col = "red", theme = "white",name = "AAPL")


#Technical analysis charting tools: One can add technical analysis studies from package TTR to the above charts:
> chartSeries(AAPL["2018-8/2019-1"], name = "AAPL",theme = "white",
              TA = "addMACD(); addBBands()")
 
 
 
 
> chartSeries(AAPL["2018-8/2019-1"], name = "AAPL", theme = "white",
            TA = "addMomentum(); addEMA(); addRSI()")


> reChart(subset = "2018-11/2019-1", theme = "white", type = "candles")


The package TTR (Technical Trading Rules) was designed for traditional technical analysis and charting.
Moving averages: One can easily compute moving averages.
> library(TTR)
# simple moving average
> sma10 <- l="" n="10)<o:p" sma="">
> head(sma10, 20)
               SMA
2018-07-11      NA
2018-07-12      NA
2018-07-13      NA
2018-07-16 188.655
2018-07-17 189.082
2018-07-18 189.730
2018-07-19 190.378
2018-07-20 190.725
2018-07-23 190.828
2018-07-24 191.093
2018-07-25 191.787
2018-07-26 192.105
2018-07-27 192.070
2018-07-30 191.970
 
#Bollinger Bands:
>   bb20 <- bbands="" sd="2.0)<o:p">
str(bb20)
#An ‘xts’ object on 2018-07-02/2019-01-18 containing:
#  Data: num [1:139, 1:4] NA NA NA NA NA NA NA NA NA NA ...
# - attr(*, "dimnames")=List of 2
#  ..$ : NULL
#  ..$ : chr [1:4] "dn" "mavg" "up" "pctB"
#  Indexed by objects of class: [POSIXct,POSIXt] TZ: UTC
#  xts Attributes:  
#List of 2
# $ src    : chr "yahoo"
# $ updated: POSIXct[1:1], format: "2019-01-21 18:11:21"
> plot(bb20)

#RSI – Relative Strength Indicator:
>   rsi14 <- l="" n="14)<o:p" rsi="">
> plot(cbind(Cl(AAPL), rsi14), legend.loc = "topleft")


#MACD: moving average convergence/divergence
> macd = MACD(Cl(AAPL), nFast = 12, nSlow = 26, nSig = 9, maType = SMA)
> str(macd)
#An ‘xts’ object on 2018-07-02/2019-01-18 containing:
#  Data: num [1:139, 1:2] NA NA NA NA NA NA NA NA NA NA ...
# - attr(*, "dimnames")=List of 2
#  ..$ : NULL
#  ..$ : chr [1:2] "macd" "signal"
#  Indexed by objects of class: [POSIXct,POSIXt] TZ: UTC
#  xts Attributes:  
#List of 2
# $ src    : chr "yahoo"
# $ updated: POSIXct[1:1], format: "2019-01-21 18:11:21"
 
> plot(cbind(Cl(AAPL), macd), legend.loc = "topleft")