Technical Analysis

Technical analysis is a method to predict price movement in the financial markets. Unlike the usual balance sheet analysis (fundamental investing), technical analysis estimates the value of a given stock using underlying trends of the price movement. The core assumption behind this method is that the fundamentals (information from financial statements) were factored into the price fluctuation, hence, detecting the patterns and signals from the fluctuation should provide sufficient indicator for future performance.

For this project, we are going to explore some of the common technical indicators using the General Electric stock.

library(quantmod)
library(data.table)
library(ggplot2)
library(tidyquant)  

Data Extraction & Manipulation

GE data is extracted using the getSymbols function from Yahoo Finance. Alternatively, the source can be replaced with FRED, MySQL, Google, or others. The data extracted should be in an ‘xts’ object - a format commonly used in time series analysis where observations are recorded in a matrix structure with an index of corresponding time.

Technically, analysis can be done in xts format alone, but for the sake of simplicity, data is converted into a standard dataframe and index column is renamed as "Date’. The end result is shown as below:

getSymbols('GE',src = 'yahoo')
## [1] "GE"
stock <- as.data.table(GE)
names(stock)[1] <- c('Date')

tail(stock)
##          Date GE.Open GE.High GE.Low GE.Close GE.Volume GE.Adjusted
## 1: 2020-09-16    6.10    6.81   6.05     6.75 255986500        6.75
## 2: 2020-09-17    6.81    7.18   6.67     7.05 261040300        7.05
## 3: 2020-09-18    6.92    7.14   6.87     6.88 145280800        6.88
## 4: 2020-09-21    6.63    6.65   6.23     6.35 162739600        6.35
## 5: 2020-09-22    6.40    6.58   6.21     6.25 140901000        6.25
## 6: 2020-09-23    6.28    6.42   6.10     6.11  88447700        6.11

Volume

One of the most common basic indicators traders examine is the trading volume. Trading volume is an indication for the ‘activeness’ of a financial instrument. Depending on the financial instruments, trading volume can be measured either using the number of stocks traded or number of contracts with changed ownerships. To put this in practice, if an increase in volume is observed with a steady increase in price, the instrument can be viewed as steady and strong. However, if volume and price are changing in different directions, a reversal might be happened.

ggplot(stock,aes(x=Date, y=GE.Volume/1000000))+
  geom_bar(aes(fill=GE.Volume/1000000),stat = 'identity')+
  labs(title = 'GE Volume chart (in million)', y = 'Volume in million')+
  theme(legend.position = 'None')

Price - Moving Average

In terms of trading price, traders often observed the trends based on the chars shape and cross in ways that form shapes - often times with weird names like ‘head and shoulder’, ’ reverse head and sholder’, ‘double top’, ‘golden cross’, etc. A golden cross indicates a long term bull market going forward, whereas the death cross is the exact opposite, indicating a potential long term bear market. Both of these refer to the confirmation of long term trend by the occurance of the overlapping of moving average lines as shown below.

ggplot(stock,aes(x=Date, y=GE.Close))+
  geom_line(aes(open = GE.Open, high = GE.High, low = GE.Low, close = GE.Close),size=1)+
  geom_ma(color = "blue",size = 1, n = 30, show.legend = TRUE)+
  geom_ma(color = "green",size = 1, n = 90, show.legend = TRUE)+
  labs(title = 'General Electric (GE) Bar Chart',
       subtitle = '30 and 90 Day SMA - 2019 till Current',
       y = 'Closing Price')+
  coord_x_date(xlim = c('2020-01-01','2020-09-23'))+
  ylim(c(5,15))

# Try geom_candlestick or geom_barchart for better visualisation on RStudio

Bollinger Band

Apart from the death and golden crosses, the moving average lines can be used to compute the Bollinger Band. A Bollinger Band is made up of 3 lines, the simple moving average, an upper boundary and a lower boundary. Simply put, this is a measure to tell if a financial instrument is overbought or oversold. The closer the prices move to the upper band, the more likely the financial instrument is overbought, which may lead to a reversal (market correction). Likewise, if the price is moving to the lower band, the market has a higher probability of being oversold, which will lead to a upward reversal.

ggplot(stock,aes(x=Date, y=GE.Close,
                 open = GE.Open, high = GE.High, low = GE.Low, close = GE.Close))+
  geom_line(size=1)+
  geom_bbands(ma_fun = SMA, sd = 2, n = 30, linetype = 5)+
  coord_x_date(xlim = c('2020-01-01','2020-09-23'))+
  ylim(c(5,15))

Relative Strength Index (RSI)

Similar to the Bollinger Band, RSI is a momentum indicator that evaluate the overbought and oversold condition of a financial instruments. The only major difference between these is the method of display. RSI is plotted as an oscillator with a range between 0 to 100 whereas the Bollinger Band are plotted along the movign average line of the price. Hence, a huge advantage of RSI over Bollinger Band is the ability to do cross comparison across multiple instruments.

Usually, RSI is evaluated using a 30-70 cutoffs. An RSI reading of 70 or above indicates an overbought or overvalued condition whereas an RSI of below 30 indicates a undervalued condition.

stock$rsi <- RSI(stock$GE.Close, n=14)


ggplot(stock,aes(x=Date, y=rsi))+
  geom_line()+
  geom_hline(yintercept= 30, linetype = 'dashed', color = 'red')+
  geom_hline(yintercept= 70, linetype = 'dashed', color = 'green')+
  labs(title = 'GE Relative Strength Index (RSI)')+
  coord_x_date(xlim = c('2020-01-01','2020-09-23'))

Other Technical Indicator

Apart from the few that was mentioned above, other indicators include rate of change, money flow index (MFI), MACD and etc.

Chart Series

ChartSeries provides an alternative way to summarise the plot. Instead of one plot for each indicator, all of them can now be compiled into a single figure, where indicators can be added to the bottom of the existing plot.

chartSeries(GE,
            subset='2020-01-01::2020-09-23',
            theme=chartTheme('white'))

addBBands(n=20,sd=2)

addRSI(n=14,maType="EMA")

addMACD(fast=12,slow=26,signal=9,type="EMA")