Professional Documents
Culture Documents
SSRN Id2317650 PDF
SSRN Id2317650 PDF
1–10
Abstract. Four financial technical analysis tools are provided including mov-
ing averages, Bollinger bands, moving average convergence divergence (MACD)
and the relative strength index (RSI). The tftools command is used with four
subcommands, each referring to a technical analysis tool: bollingerbands, macd,
movingaverage, and rsi. Examples are provided. tftools allows researchers to
backtest their own investment strategies, and will be of interest to investors, re-
searcher and students of finance.
1 Introduction
Technical financial analysis is part of the standard investment decision making process.
Many brokerage companies and public websites provide free tools to calculate various
technical analysis statistics. However, investors often need to customize and back-test
their analysis tools. The technical analysis tools provided with this study allow investors
and researchers to perform some common technical analyses.
One command with four different subcommands are provided as part of this study:
tftools bollingerbands calculates the Bollinger bands; tftools macd calculates the
moving average convergence and divergence (MACD); tftools movingaverage calcu-
lates simple and exponential moving averages; and tftools rsi calculates the relative
strength index (RSI).
Being able to easily calculate these statistics allows investors to better optimize their
portfolios, and researchers to test their financial hypotheses. For example, Chong and
Ng (2008) find that the MACD and RSI trading rules generate better returns than a
simple buy-and-hold strategy. Investigating the Singapore Stock Exchange, Wong et al.
(2003) finds that RSI-based trading was quite profitable. Leung and Chong (2003) find
that the Bollinger Bands perform similar to the “Moving Average Envelopes.” For
c yyyy StataCorp LP st0001
a review of these and other common technical financial indicators, see Cahen (2001),
Kirkpatrick II and Dahlquist (2010), and Stevens (2002) among others.
2.2 Syntax
2.3 Description
Bollinger bands were developed by John Bollinger in the 1980s. Bollinger bands consist
of an upper and lower band, defining an envelope of usual trading. If a stock price
moves outside of this envelope, this triggers an investment decision. The upper band is
calculated as K standard deviations above the stock’s moving average. The lower band
is K standard deviations below the moving average. By construction, the center of this
envelope is the original moving average. Bollinger bands mimic the idea that random
variables tend to stay within plus or minus two standard deviations from their mean.
To this insight, Bollinger bands add that the financial data might not be stationary, so
that the means and standard deviations are updated on a continual basis.
The usual trading system using Bollinger bands is to sell when the price reaches its
upper band, as a reversion back to the mean is expected. A contrarian Bollinger trading
system reverses this logic: buy when the price has crossed its upper band.
The tftools bollingerbands command calculates the Bollinger bands for a single
time-series variable. The size of the window for the standard deviation and simple
moving average can be specified by the user. The command creates three new variables:
middle band, upper band and lower band. The data must first be tsset.
2.4 Options
symbol(variable) is the variable that the Bollinger band calculation is based upon
(usually the stock symbol that contains the daily prices).
generate(newvar) is the new variable prefix for the calculated Bollinger band values.
period(string) is the window for which standard deviation and simple moving average
2.6 Usage
freduse SP500, clear
drop if SP500==.
drop date
rename daten date
gen obs=_n
tsset obs
tftools bollingerbands if year(date)>2015, symbol(SP500) generate(SP500_BB)
twoway (line SP500 date) (line SP500_BB_upper_band date, lwidth(thick)) ///
(line SP500_BB_lower_band date, lwidth(thick)) if year(date)>2015
Figure 1 shows the daily S&P-500 index along with the upper and lower Bollinger
bands using the default two standard deviations. It is usually interpreted as support
and resistance levels. As the index breaks above the upper band (resistance line) then a
downward move would be expected. As the index break below the lower band (support
4 Technical Financial Analysis Tools for Stata
Figure 2 shows the same Bollinger bands as the Figure 1 however with four standard
deviations (instead of the two). While with the two standard deviations the index line
frequently breaks above and below the resistance and support lines, buy and sell signals
are more rare—and presumably stronger—if a stock breaks through these much wider
bands.
3.2 Syntax
3.3 Description
The MACD (“Mack-D”) is a momentum indicator commonly used by technical analysts.
The MACD is equal to the difference between two exponential moving averages (EMAs)
of different lengths: (1) a long-term EMA of usually 26 days, and (2) a short-term EMA
of usually 12 days. The MACD is defined as the short-term EMA minus the long-term
EMA. A third component is the so-called “signal line” which is, usually, a 9 day EMA
of the MACD.
The convention is to plot the signal line over the MACD. If the signal line crosses
above the MACD, this is considered a bullish indicator. If the signal line crosses below
the MACD, this is a bearish sign.
The MACD-Histogram is equal to the MACD less the signal line. As such, it os-
cillates above and below zero. When the histogram becomes positive, this is a bullish
signal; when negative, a bearish signal. The histogram is used as an indicator of mo-
mentum. When the histogram is positive and increasing, the stock price is said to have
increasing positive momentum. A negative and decreasing histogram implies steeper
downside momentum.
If the actual stock price diverges significantly from the MACD, this is often viewed
as signaling the end of a current trend. Finally, if the MACD rises steeply, this is
because the short-term EMA (usually 12 days) is rising faster than the long-term EMA
(usually 26 days). That is, prices have moved dramatically upward recently. Analysts
sometimes infer that the current price has moved away from its equilibrium value and
they expect a downward correction.
The tftools macd command calculates the moving average convergence and di-
vergence (MACD) for a single time-series variable. tftools macd creates three new
variables: MACD line, signal line and MACD histogram. The data must first be tsset.
3.4 Options
symbol(variable) is the variable that the MACD calculation is based upon (usually
the stock symbol that contains the daily prices).
generate(newvar) is the new variable prefix for the calculated MACD values.
3.6 Usage
freduse SP500, clear
drop if SP500==.
drop date
rename daten date
gen obs=_n
tsset obs
tftools macd if year(date)>2015, symbol(SP500) generate(SP500)
twoway (line SP500_MACD_line date, lpattern(dash)) (line SP500_signal_line date) ///
if year(date)>2015
Figure 3 shows the daily S&P-500 index moving average convergence and divergence.
The usual interpretation for this technical analysis took is based on the MACD line and
the signal line. It is usually interpreted as down signal (bearish) if the MACD line falls
below the signal line. Similarly, it is usually interpreted as up signal (bullish) if the
MACD line goes above the signal line.
While Figures 1 and 2 have no up or down signal (the most recent day’s index values
were in the middle of the upper and lower bands), Figure 3 shows a down signal where
the MACD line is below the signal line.
Dicle and Levendis 7
4.2 Syntax
4.3 Description
tftools movingaverage calculates the moving average for a single time-series variable.
The window for the moving averaging can be specified as well as the type of averaging
(i.e. simple or exponential). You can also calculate moving standard deviation as well as
moving maximum, minimum and sum. tftools movingaverage creates a new variable.
The data must first be tsset.
4.4 Options
symbol(variable) is the variable that the moving average calculation is based upon
(usually the stock symbol that contains the daily prices).
generate(newvar) is the new variable prefix for the calculated moving average values.
period(integer) is the size of the moving average window, expressed as an integer
number of time periods.
ma type is the moving average type: sma (simple moving average), ema (exponential
moving average), sd (moving standard deviation), sum (moving sum), min (moving
minimum) and max (moving maximum).
4.6 Usage
freduse SP500, clear
drop if SP500==.
drop date
rename daten date
gen obs=_n
8 Technical Financial Analysis Tools for Stata
tsset obs
tftools movingaverage if year(date)>2015, symbol(SP500) generate(SP500) period(100) ma_type(sma)
twoway (line SP500 date) (line SP500_sma_100 date) if year(date)>2015
Figure 4: Daily S&P-500 index and 100 days simple moving average
Figure 4 shows the simple moving average for the daily S&P-500 index. Moving averages
are the building blocks of the financial technical analysis. It is common to use 20 and
50 days for short-term moving averages as well as 100 and 200 days as the long-term
moving averages.
Figure 5 shows a short-term (50 days) and a long-term (200 days) moving averages.
These two, in combination, may signal two of the most common technical analysis
signals namely the so-called “golden cross” (up trend signal) and “death cross” (down
trend signal). Figure 5 shows a golden cross during 2016 for S&P-500 index.
While MACD analysis in Figure 3 shows a down trend, the golden cross in Figure
5 shows an up trend. The conflicting technical analysis signals are common as none of
these analysis tools will ever provide a consistently reliable signal.
Dicle and Levendis 9
Figure 5: Daily S&P-500 index, 50 days and 200 days simple moving averages
5.2 Syntax
5.3 Description
The tftools rsi command calculates the relative strength index (RSI) for a single
time-series variable. tftools rsi creates a new variable using the newvar variable
name of the generate option. The data must first be tsset.
RSI = 100 − (100/(1 + RS)), where RS = (average of upward returns over x days ) /
(average of downward returns over x days). Downward returns are expressed as positive
numbers. For this reason, the RSI is bounded between 0 and 100. The convention is
that an RSI of 70 or above indicates that a stock is overbought and that a downward
correction can be expected. An RSI of 30 or below indicates that a stock is oversold
and that investors can look forward to an upward correction.
10 Technical Financial Analysis Tools for Stata
5.4 Options
symbol(variable) is the variable that the RSI calculation is based upon (usually the
stock symbol that contains the daily prices).
generate(newvar) is the new variable prefix for the calculated RSI values.
5.6 Usage
freduse SP500, clear
drop if SP500==.
drop date
rename daten date
gen obs=_n
tsset obs
tftools rsi if year(date)>2015, symbol(SP500) generate(SP500)
twoway (line SP500_RSI date) if year(date)>2015
Figure 6 shows the relative strength index (RSI) for daily S&P-500 index. RSI is usually
interpreted to provide a up trend signal below 30 (because it may be oversold and a
reversal is in order) and a down trend signal above 70 (because it may be overbought).
Dicle and Levendis 11
Figure 6 shows a period in 2017 where the RSI levels was above 70 and quickly reversed
down.
6 Conclusion
Financial technical analysis is used by academics as well as professionals. Our suggested
commands, tftools, provide four subcommands to calculate the most popular tools
including: moving averages, Bollinger bands, moving average convergence divergence
(MACD) and the relative strength index (RSI). The moving average is at the core
of many technical analysis tools, with analysts calculating the moving averages, not
only of stock prices, but also for the daily minimum, maximum, standard deviation,
daily high and daily low values, etc. Using these moving averages, Bollinger bands are
calculated to provide a support and resistance levels for security prices. While lower, and
more common, standard deviations will provide more immediate support and resistance
levels, higher standard deviations will provide more consistent ones. Based on mean
reversion, the MACD provides a tool to evaluate how much the shorter-term moving
average of a security’s price is converging or diverging to its longer term mean. Relative
strength index provides a momentum index that compliments the moving average based
other financial technical analysis tools. While technical analysis in finance may not
provide consistently reliable trading signals, it provides guidance that can complement
fundamental analysis. Also, while market efficiency theory in finance argues against the
long term success of technical analysis, self-fulfilling prophesies in the markets make
them important part of academic and professional domain.
7 References
Cahen, P. 2001. Dynamic technical analysis. John Wiley and Sons.
Chong, T. T.-L., and W.-K. Ng. 2008. Technical analysis and the London stock ex-
change: testing the MACD and RSI rules using the FT30. Applied Economics Letters
15(14): 1111–1114.
Kirkpatrick II, C. D., and J. A. Dahlquist. 2010. Technical analysis: the complete
resource for financial market technicians. FT press.
Leung, J. M.-J., and T. T.-L. Chong. 2003. An empirical comparison of moving average
envelopes and Bollinger Bands. Applied Economics Letters 10(6): 339–341.
Stevens, L. 2002. Essential technical analysis: tools and techniques to spot market
trends, vol. 162. John Wiley & Sons.
Wong, W.-K., M. Manzur, and B.-K. Chew. 2003. How rewarding is technical analysis?
Evidence from Singapore stock market. Applied Financial Economics 13(7): 543–551.