10.2 Technical Charts

Here we will use the quantmod and TTR package to generate some Technical Charts. The data will be downloaded from Yahoo Finance.

10.2.1 Candlesticks and OHLC chart

  • As demonstrated in the previous chapter, R package quantmod can be used to download daily OHLC data (Open, High, Low and Close) with volume.

  • quantmod (https://www.quantmod.com/) also allows to import data from csv files.

# Run the following to download and save the data, same as the
# regression chapter. Only run if data requires update of different
# assets are used
library(quantmod)
library(pander)
# download stock
BHP = getSymbols("BHP.AX", from = "2019-01-01", to = "2021-07-31", auto.assign = FALSE)
# download index
ASX = getSymbols("^AXJO", from = "2019-01-01", to = "2021-07-31", auto.assign = FALSE)
# save both in rds (to be used in the TA chapter)
saveRDS(BHP, file = "data/bhp_prices.rds")
saveRDS(ASX, file = "data/asx200.rds")
library(quantmod)
library(pander)
BHP = readRDS("data/bhp_prices.rds")
ASX = readRDS("data/asx200.rds")
pander(head(BHP), caption = "OHLC Data", split.table = Inf)
OHLC Data
Period BHP.AX.Open BHP.AX.High BHP.AX.Low BHP.AX.Close BHP.AX.Volume BHP.AX.Adjusted
2019/01/02 12:00:00 AM 34.28 34.55 33.65 33.68 7290354 26.68
2019/01/03 12:00:00 AM 33.88 34.13 33.56 33.68 8473568 26.68
2019/01/04 12:00:00 AM 33.1 33.39 32.96 33.38 9314513 26.44
2019/01/07 12:00:00 AM 34.5 34.55 34.28 34.39 8553510 27.24
2019/01/08 12:00:00 AM 34.6 34.68 34.43 34.43 8685056 27.28
2019/01/09 12:00:00 AM 34.5 34.59 34.19 34.3 9459525 27.17
chartSeries(BHP, theme = "white")
BHP Line Chart with Volume

Figure 10.1: BHP Line Chart with Volume

addTA(ASX$AXJO.Close, col = "darkblue")
BHP and ASX

Figure 10.2: BHP and ASX

10.2.2 Line chart

chartSeries(BHP, type = "line", subset = "2020", theme = "white")
Line Chart

Figure 5.2: Line Chart

10.2.3 Candlestick chart

chartSeries(BHP, type = "candle", subset = "last 1 month", theme = "white")
Candle Stick chart

Figure 5.3: Candle Stick chart

10.2.4 Add technical indicators

chartSeries(BHP, type = "candle", subset = "last 6 month", theme = "white",
    TA = c(addSMA(5), addEMA(10)))
SMA and EMA

Figure 5.4: SMA and EMA

10.2.5 Adding indicators sequentially

chartSeries(BHP, type = "candle", subset = "last 6 months", theme = "white")
CandleStick

Figure 10.3: CandleStick

addBBands()
BOllinger Bands

Figure 10.4: BOllinger Bands

addMomentum(n = 5)
Momentum

Figure 10.5: Momentum