Technical Analysis

    Now that we picked some stocks from our dividend universe, by the way doesn’t necessarily have to be restricted to dividend stocks, we could acquire some shares and invest in them.  Once bought, a buy and hold strategy could be adopted.  Or alternately, one could follow some kind of strategy in the management of these shares,  In other words, it can be sold, then after determining that conditions warrant it, shares could be reacquired.  Some kind of definitive logic should undermine the positions one holds in such securities.  This really is what is commonly referred as trading strategies, or if applied to historic data, backtesting such a trading strategy.

    One would be normally be working market price data, i.e., daily prices.  Of course, one could also work with fundamental data, i.e., balance sheet, income statements and so on.  We will defer the fundamental analysis for later and instead concentrate on technical analysis.

   As expected, there are very good R packages available and we will be using quantmod and its dependencies.  Installing this will automatically install dependent packages as well, e,g, the TTR package which contains innumerable functions for coming up with various measures.  We will illustrate some of these here.  Another advantage of installing quantmod is that it has a function getSymbols, which can be used to get data on a real-time basis from some data providers.  Provided of course, that we have either subscribed to or registered api keys with them.

    There are a plethora of technical indicators available in the TTR package as documented in this reference guide  TTR

Some examples of charts using quantmod.  This is for symbol MMM.  We will plot the prices which shows in candlestick form, and to the basic price plot, we will add Bollinger Bands, Moving Average Convergence/Divergence (MACD) as well as the ADX also known as the Directional Movement Indicator.

  The quantmod package has various other options for charting, including several different color themes etc., which can all be explored at leisure.

Install packages

if(!is.element("quantmod", installed.packages()[,1]) )
install.packages("quantmod")
equire (quantmod)

# uses default theme may change via theme="white" etc., need to find hemes
chartSeries(MMM)
addBBands()
addADX()
addMACD()

basic

###   Zoom into period Oct 2017 April 2018
### Add the EMA indicator to previous plot
zoomChart(subset='2017-10::2018-04')

addEMA()

 

 

As mentioned above, the TTR – Technical Trading Rules package is a dependency which will automatically get installed.  This package has many functions for all the common indicators in use by the investment/trading community.  A few of these will be illustrated below.  A look at the package pdf or description will provide a good exposition of what is available.  Most functions operate on just one vector usually a price vector, which could be any of Open, High, Low or Close.  Some functions require 3 price vectors, for example the ADX or Directional Movement Indicator needs HLC (High, Low & Close).  Some need Volume as well.  Note that all of this data will automatically be downloaded by the getSymbols function. 

  A flavor of indicators produced shown below.  These will be immensely useful in developing trading strategies.

Thanx for reading

 

##############################
### Moving Averages
##############################

### SMA with default period of 10 days
tail(SMA(Cl(MMM)))
SMA
20190116 16:00:00 191.429
20190117 16:00:00 191.883
20190121 16:00:00 192.021
20190122 16:00:00 192.082
20190123 16:00:00 192.104
20190124 16:00:00 192.334

### SMA with custom period of 28 days
tail(SMA(Cl(MMM), n=28))
SMA
20190116 16:00:00 191.6079
20190117 16:00:00 191.5229
20190121 16:00:00 191.3064
20190122 16:00:00 191.1218
20190123 16:00:00 190.8614
20190124 16:00:00 190.6389
#################################
#################################
### EMA (Exponential Moving Average) with default period of 10 days
> tail(EMA(Cl(MMM)))
EMA
20190116 16:00:00 190.8644
20190117 16:00:00 191.7727
20190121 16:00:00 191.8613
20190122 16:00:00 191.9392
20190123 16:00:00 192.0448
20190124 16:00:00 192.7458
### EMA (Exponential Moving Average) with custom period of 28 days
tail(EMA(Cl(MMM), n=28))
EMA
20190116 16:00:00 192.3152
20190117 16:00:00 192.5597
20190121 16:00:00 192.5390
20190122 16:00:00 192.5219
20190123 16:00:00 192.5217
20190124 16:00:00 192.7547


##############################
### MACD
##############################
### MACD default fast=12, slow=26 days
### default is to use EMA
tail(MACD(Cl(MMM)))
macd signal
20190116 16:00:00 0.62472339 0.8862728
20190117 16:00:00 0.37167712 0.7833537
20190121 16:00:00 0.31919221 0.6905214
20190122 16:00:00 0.27318515 0.6070541
20190123 16:00:00 0.22446722 0.5305367
20190124 16:00:00 0.04353268 0.4331359

### MACD custom fast=14, slow=29 days; EMA
tail(MACD(Cl(MMM), nFast=14, nSlow=29))
macd signal
20190116 16:00:00 0.7531101 0.9830809
20190117 16:00:00 0.5330926 0.8930832
20190121 16:00:00 0.4757481 0.8096162
20190122 16:00:00 0.4240865 0.7325103
20190123 16:00:00 0.3705332 0.6601148
20190124 16:00:00 0.2066794 0.5694278

### MACD custom fast=14, slow=29 days; but using SMA instead
tail(MACD(Cl(MMM), nFast=14, nSlow=29, maType=SMA))
macd signal
20190116 16:00:00 0.70484350 2.3024035
20190117 16:00:00 0.34131289 1.9666689
20190121 16:00:00 0.16988862 1.6169683
20190122 16:00:00 0.01137941 1.2548252
20190123 16:00:00 0.40502470 0.8747751
20190124 16:00:00 0.64713536 0.5107954

##############################
### Bollinger Bands default is 20 days
##############################
### BBands default with SMA
tail(BBands(Cl(MMM), maType=SMA))
dn mavg up pctB
20190116 16:00:00 182.1021 189.279 196.4559 0.6693618
20190117 16:00:00 181.8455 189.574 197.3025 0.9066771
20190121 16:00:00 182.0739 189.818 197.5621 0.6576677
20190122 16:00:00 182.9580 190.245 197.5320 0.6403184
20190123 16:00:00 185.9225 190.940 195.9575 0.6574491
20190124 16:00:00 186.4435 191.422 196.4005 0.9497309

### BBands default with EMA
tail(BBands(Cl(MMM), maType=EMA))
dn mavg up pctB
20190116 16:00:00 184.2810 191.4579 198.6349 0.5175615
20190117 16:00:00 184.1487 191.8772 199.6057 0.7576720
20190121 16:00:00 184.1695 191.9136 199.6578 0.5223634
20190122 16:00:00 184.6625 191.9495 199.2365 0.5233653
20190123 16:00:00 186.9863 192.0038 197.0213 0.5514391
20190124 16:00:00 187.3963 192.3749 197.3534 0.8540325

##############################
### ADX default is 14 days
##############################
### ADX default with SMA
tail(ADX(HLC(MMM), maType=SMA))
DIp DIn DX ADX
20190116 16:00:00 17.84749 22.60394 11.758419 16.38654
20190117 16:00:00 21.88853 20.99520 2.083128 15.44425
20190121 16:00:00 20.10448 22.18096 4.910626 14.69196
20190122 16:00:00 18.96265 21.15753 5.470746 13.66614
20190123 16:00:00 18.46098 20.59779 5.470746 12.03442
20190124 16:00:00 23.46411 19.07419 10.319909 11.66226
### ADX default with EMA
tail(ADX(HLC(MMM), maType=EMA))
DIp DIn DX ADX
20190116 16:00:00 17.84749 22.60394 11.758419 17.44629
20190117 16:00:00 21.88853 20.99520 2.083128 15.39787
20190121 16:00:00 20.10448 22.18096 4.910626 13.99957
20190122 16:00:00 18.96265 21.15753 5.470746 12.86239
20190123 16:00:00 18.46098 20.59779 5.470746 11.87684
20190124 16:00:00 23.46411 19.07419 10.319909 11.66925

##############################
### stoch default is fastK=14, fastD=3, slowD=3 days
##############################
### stoch default with SMA
> tail(stoch(HLC(MMM), maType=SMA))
fastK fastD slowD
20190116 16:00:00 0.7860963 0.6913079 0.7474000
20190117 16:00:00 0.9600296 0.7779458 0.7314511
20190121 16:00:00 0.6935603 0.8132287 0.7608275
20190122 16:00:00 0.6957809 0.7831236 0.7914327
20190123 16:00:00 0.6258438 0.6717283 0.7560269
20190124 16:00:00 0.8645161 0.7287136 0.7278552

### stoch default with EMA
tail(stoch(HLC(MMM), maType=EMA))
fastK fastD slowD
20190116 16:00:00 0.7860963 0.7365102 0.7421787
20190117 16:00:00 0.9600296 0.8482699 0.7952243
20190121 16:00:00 0.6935603 0.7709151 0.7830697
20190122 16:00:00 0.6957809 0.7333480 0.7582089
20190123 16:00:00 0.6258438 0.6795959 0.7189024
20190124 16:00:00 0.8645161 0.7720560 0.7454792

 

3 Comments on “Technical Analysis”

  1. 👉 $5,000 FREE EXCHANGE BONUSES BELOW 📈 👉 PlaseFuture FREE $3,000 BONUS + 0% Maker Fees 📈 + PROMOCODE FOR NEWS USERS OF THE EXCHANGE 👉 [M0345IHZFN] — 0.01 BTC 👉 site: https://buycrypto.in.net Our site is a secure platform that makes it easy to buy, sell, and store cryptocurrency like Bitcoin, Ethereum, and More. We are available in over 30 countries worldwide.

Comments are closed.