Last Update: February 6, 2020
Stock technical indicators are calculated by applying certain formula to stock prices and volume data. They are used to alert on the need to study stock price action with greater detail, confirm other technical indicators’ signals or predict future stock prices direction.
This topic is part of Stock Technical Analysis with R course. Feel free to take a look at Course Curriculum.
This tutorial has an educational and informational purpose and doesn’t constitute any type of trading or investment advice. All content, including code and data, is presented for personal educational use exclusively and with no guarantee of exactness of completeness. Past performance doesn’t guarantee future results. Please read full Disclaimer.
An example of stock technical indicators is moving averages convergence/divergence MACD  which consists of centered oscillator that measures a stock price momentum and identifies trends. Twelve days are commonly used for short-term smoothing, twenty-six days for long-term smoothing and nine days for signal.
1. Technical indicator calculation.
1.1. Short-term and long-term smoothing calculation.
Where = current period stock close prices, = current period close prices periods exponential moving average, = inital close prices periods simple moving average.
1.2. Moving averages convergence/divergence MACD stock technical indicator, signal and histogram calculation.
Where = current period close prices MACD, = current period close prices MACD signal, = current period close prices MACD histogram.
2. R script code example.
2.1. Load R packages .
2.2. MACD stock technical indicator data reading.
- Data: S&P 500® index replicating ETF (ticker symbol: SPY) daily open, high, low, close, adjusted close prices and volume (2016).
SPY <- read.csv(‘MACD-Stock-Technical-Indicator-Data.txt’,header=T) SPY <- xts(SPY[,2:7],order.by=as.Date(SPY[,1]))
2.3. MACD stock technical indicator calculation and chart.
- MACD technical indicator, signal and histogram number of periods not fixed and only included for educational purposes.
macd <- MACD(Cl(SPY),nFast=12,nSlow=26,nSig=9) barChart(SPY,theme=chartTheme('white')) addMACD(fast=12,slow=26,signal=9)
 Gerald Appel. “Technical Analysis: Powerful Tools for Active Investors”. FT Press. 2005.
 Joshua Ulrich. “TTR: Technical Trading Rules”. R package version 0.23-5. 2019.
Jeffrey A. Ryan and Joshua M. Ulrich. “quantmod: Quantitative Financial Modelling Framework”. R package version 0.4-15. 2019.