knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) library(quantmod) library(CandleStickPattern) #getSymbols("MSFT")
How can you recongize candle stick patterns? This R package allows you to recognized them based on formulas.
To install this package, it is the easist to use devtools:
#install.packages("devtools") library(devtools)
Then we can use the install_github
to get the package directly:
#install_github("kochiuyu/CandleStickPattern") library(CandleStickPattern)
To start, let us review the basic of candle stick.
Candle Stick visuall display the OHLC data. OHLC means open (Op), high (Hi), low (Li) and close (Cl) prices.
First, the rectangle body tells close and open prices. The color of the rectangle would tell whether the higher end of body is open or close price:
Two ends tells high and low prices. The line between two ends to the candle are called shadow:
Size of candle body (rectangle) and the lenght of two shadows are important for the pattern recognition. We first define them first:
Upper shadow length: $US(t)=Hi(t)-\max{Op(t),Cl(t)}$
Lower shadow length: $LS(t)=\min{Op(t),Cl(t)}-Lo(t)$
Whole candle length: $WC(t)=Hi(t)-Lo(t)$
Body length: $BL(t)= |Op(t)-Cl(t)|$
Compare with whole candle length:
small body: $\delta WC(t) > BL(t)$ for small $\delta=0.1$
large body: $\delta WC(t) < BL(t)$ for large $\delta=0.9$
Compare with whole candle length:
short upper shadow: $\delta WC(t) > US(t)$ for small $\delta=0.1$
long upper shadow: $\delta WC(t) < US(t)$ for large $\delta=0.9$
Compare with historical whole candle lengths:
small body: $WC(t) < \delta \times median{WC(t-1),...,WC(t-n-1)}$ for parameter $\delta=1$
large body: $WC(t) > \delta \times median{WC(t-1),...,WC(t-n-1)}$ for parameter $\delta=1$
Similar ideas can be extended to body length and shadow sizes.
Daily
Up: $U(t)=1 \text{ if } Cl(t)>Op(t)$ and 0 otherwise
Down: $D(t)=1 \text{ if } Cl(t)<Op(t)$ and 0 otherwise
Inter-day Gap
Gap up: candle body Day 2 is higher than that of Day 1. $$GU(t) =1 \text{ if } \min{Op(t),Cl(t)}>\max{Op(t-1),Cl(t-1)}$$
Gap down: candle body Day 2 is lower than that of Day 1. $$GD(t) =1 \text{ if } \max{Op(t),Cl(t)}>\min{Op(t-1),Cl(t-1)}$$
Indecision signal: up and down forces are balanced
Let see an example:
price <- MSFT['2011-07-08/2011-07-19'] candleChart(price,theme='white')
Now let us see whether our code can recongize it. The synatx is doji()
:
doji(price)
Bullish signal: resist downward pressure
price <- MSFT['2014-10-28/2014-11-03'] candleChart(price,theme='white')
Now let us see whether our code can recongize it. The synatx is dragonfly.doji()
:
dragonfly.doji(price)
Bullish signal : resist downward pressure
price <-MSFT['2015-04-28/2015-05-01'] candleChart(price, theme='white')
Now let us see whether our code can recongize it. The synatx is graveston.doji()
:
gravestone.doji(price)
Reversal signal:
Long lower shadow
Harmer: Bullish signal in a downward trend
price <- MSFT['2011-10-18/2011-10-25'] candleChart(price, theme='white')
Now let us see whether our code can recongize it. The synatx is hammer()
:
hammer(price)
Reversal signal:
Short lower shadow
Inverted Harmer: Bullish signal in a downward trend
price <- MSFT['2011-11-20/2011-11-30'] candleChart(price, theme='white')
Now let us see whether our code can recongize it. The synatx is inverted.hammer()
:
inverted.hammer(price)
Bullish signal as bullish force dominates:
price <- MSFT['2011-11-02/2011-11-08'] candleChart(price, theme='white')
Now let us see whether our code can recongize it. The synatx is bullish.engulf()
:
bullish.engulf(price)
Bearish signal as bearish force dominates:
price <-MSFT['2011-12-15/2011-12-20'] candleChart(price, theme='white')
Now let us see whether our code can recongize it. The synatx is bearish.engulf()
:
bearish.engulf(price)
Bullish signal as bullish force fights back:
price <- MSFT['2011-12-27/2011-12-30'] candleChart(price, theme='white')
Now let us see whether our code can recongize it. The synatx is bullish.harami()
:
bullish.harami(price)
Bearish signal as bearish force fights back
price <- MSFT['2011-11-02/2011-11-07'] candleChart(price, theme='white')
Now let us see whether our code can recongize it. The synatx is bearish.harami()
:
bearish.harami(price)
Bullish signal
price <-MSFT['2011-07-26/2011-07-30'] candleChart(price, theme='white')
Now let us see whether our code can recongize it. The synatx is piercing.line()
:
piercing.line(price)
Bullish signal
price <- MSFT['2011-09-16/2011-09-21'] candleChart(price, theme='white')
Now let us see whether our code can recongize it. The synatx is dark.cloud.cover()
:
dark.cloud.cover(price)
Bullish signal:
price <-MSFT['2011-11-08/2011-11-14'] candleChart(price, theme='white')
Now let us see whether our code can recongize it. The synatx is kick.up()
:
kick.up(price)
Bearish signal:
price <- MSFT['2010-08-16/2010-08-19'] candleChart(price, theme='white')
Now let us see whether our code can recongize it. The synatx is kick.down()
:
kick.down(price)
Bullish signal:
price <-MSFT['2007-05-20/2007-06-01'] candleChart(price, theme='white')
Now let us see whether our code can recongize it. The synatx is three.white.soldiers()
:
TWS <- three.white.soldiers(MSFT) TWS['2007-05-20/2007-06-01']
Bearish signal:
price <-MSFT['2010-01-15/2010-02-09'] candleChart(price, theme='white')
Now let us see whether our code can recongize it. The synatx is three.black.crows()
:
TBC <- three.black.crows(MSFT) TBC['2007-05-20/2007-06-01']
Bullish signal:
price <- MSFT['2013-06-09/2013-06-18'] candleChart(MSFT['2013-06-09/2013-06-18'], theme='white')
Now let us see whether our code can recongize it. The synatx is morning.star()
:
MS <- morning.star(MSFT) MS['2013-06-09/2013-06-18']
Bearish Signal:
price <-MSFT['2011-11-01/2011-11-11'] candleChart(price, theme='white')
Now let us see whether our code can recongize it. The synatx is evening.star()
:
ES <- evening.star(MSFT) ES['2011-11-01/2011-11-11']
Bullish signal:
price <-MSFT['2012-01-10/2012-01-22'] candleChart(price, theme='white')
Now let us see whether our code can recongize it. The synatx is rising.three()
:
RT <- rising.three(MSFT) RT['2012-01-10/2012-01-22']
Bearish signal:
The synatx is falling.three()
.
This part is less related to candle stick pattern but nevertheless sometimes candle chart pattern requires the usage of trend. This package provides a very simple way to determine trend using exponential moving average (EMA.)
We compare a short-run EMA with a long-run EMA to determine trend.
If the short-run EMA is bigger than long-run EMA, then it is upward trend.
The synatx is up.trend(price, S, L, delta)
where S
and L
are the number of periods for short-run and long-run EMA, and delta
is sensitivity parameter.
If the short-run EMA is smaller than long-run EMA, then it is upward trend.
The synatx is down.trend(price, S, L, delta)
where S
and L
are the number of periods for short-run and long-run EMA, and delta
is sensitivity parameter.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.