top of page

Technical Stock Charts using Quantmod and R

Updated: Sep 23, 2020

In R, you can visualize to do technical analysis of stocks by using quantmod and other relevant packages. There are many technical indicators for evaluating stocks, in this post I will use first basic graphs to visualize stock's fluctuation for a given period then step by step add Bollinger Band, RSI,MACD, exponential moving average (EMA)to the technical stock charts.


First install/load necessary packages


library(tidyverse)
library(quantmod)
library(PerformanceAnalytics)
library(RColorBrewer)
library(tseries) 
library(lubridate)
library(Quandl) 
Quandl.api_key("zrcB2Ejv9UmvhPCUsy2_")

To get started, I will pull down Microsoft's stock data from Yahoo Finance using Quantmod's 'getsymbols' function.


msft<-getSymbols("MSFT", auto.assign = F)

Charting

To visualize the situation of Microsoft's stock prices, several types of graphes can be used like line graph, bar graph or candlestick graph. The good news is syntax for making these plots is nearly same so after you understand one type, others will not be a problem.

So let's get charting!

For this particular example, I choose to investigate Microsoft's stocks (MSFT) in 2008.

 

chartSeries

"Charting tool to create standard financial charts given a time series like object. Serves as the base function for future technical analysis additions. Possible chart styles include candles, matches, bars, and lines. Chart may have white or black background."[1]
 

Line Graph

chartSeries(msft,
            type="line",
            subset="2008",
            theme = chartTheme("black"))

In this example msft is our object which is in xts class, but note that this function can also be used with any object that is time-series like zoo, timeSeries, its, ts, irts and so on.


subset can be used to filter the desired time period, in my case it is 2008.




Bar Graph


chartSeries(msft,
            type="bar",
            subset="2008",
            theme = chartTheme("black"))

Candlestick



chartSeries(msft,
            type="candlestick",
            subset="2008",
            theme = chartTheme("white"))



Bollinger Bands

chartSeries(msft,
            subset = "2007",
            TA="addBBands(n=20, sd=2)",        
            theme=chartTheme("white"))

A Bollinger Band is a technical analysis tool defined by a set of lines plotted two standard deviations (95% confidence interval) away from a simple moving average (SMA).

In this example TA refers to vector of technical indicators.


Interpretation of Bollinger Bands

Since the width between upper( +2 sd) and lower bound (-2sd) reflects market volatility, we can use this information to detect the patterns and interpret the future fluctuations by using that pattern.


For example, when we observe tighten bands during a period of low volatility, it raises the likelihood of a sharp price move in either direction. This may begin a trending move. Watch out for a false move in opposite direction which reverses before the proper trend begins.



Whether Overbought or Oversold?


Relative strength index (RSI) is a technical indicator used in stock analysis and is intended to chart the current and historical strength or weakness of a stock or market based on the closing prices of a recent trading period. It is usually used to detect oversold or undervalued conditions

chartSeries(msft,
            type = "line",
            subset = "2007",
            TA=c(addBBands(n=20, sd=2),addRSI()),
            theme=chartTheme("white"))


Traditional interpretation and usage of the RSI are that values of 70 or above indicate that a security is becoming overbought or overvalued and may be primed for a trend reversal or corrective pullback in price. An RSI reading of 30 or below indicates an oversold or undervalued condition. [3]

The Moving Average Convergence Divergence (MACD)

chartSeries(msft,
            type = "line",
            subset = "2007",
            TA=c(addBBands(n=20, sd=2),addRSI(),addEMA(n=30),addMACD()),
            theme=chartTheme("white"))

MACD is a trend-following momentum indicator that shows the relationship between two moving averages of a security’s price. [3]

Traders may buy the security when the MACD crosses above its signal line and sell, or short, the security when the MACD crosses below the signal line.


Although both RSI and MACD measures the momentum of an asset, their results may differ since MACD measures the relationship between two exponential moving averages but RSI considers recent price highs and lows as a measurement factor.


Hence while doing technical analysis of a stock, charting is only a starting step and should be supported by knowledge about trading signals and techniques for better interpratation.




46 views0 comments

Recent Posts

See All
Never Miss a Post. Subscribe Now!

Thanks for submitting!

  • Grey Twitter Icon
bottom of page