[Candlesticks-commits] r16 - in pkg: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Apr 19 17:38:26 CEST 2012


Author: wotuzu17
Date: 2012-04-19 17:38:26 +0200 (Thu, 19 Apr 2012)
New Revision: 16

Modified:
   pkg/DESCRIPTION
   pkg/R/CSPHarami.R
   pkg/R/CSPLongCandle.R
   pkg/R/CSPNLongCandles.R
   pkg/R/CSPStar.R
   pkg/R/CSPThreeWhiteSoldiers.R
   pkg/man/CSPDarkCloudCover.Rd
   pkg/man/CSPHarami.Rd
   pkg/man/CSPLongCandle.Rd
   pkg/man/CSPNLongWhiteCandles.Rd
   pkg/man/CSPStar.Rd
   pkg/man/CSPThreeBlackCrows.Rd
   pkg/man/CSPThreeWhiteSoldiers.Rd
Log:
re-worked Harami, TWS, TBC, MorningStar, EveningStar

Modified: pkg/DESCRIPTION
===================================================================
--- pkg/DESCRIPTION	2012-04-17 12:53:14 UTC (rev 15)
+++ pkg/DESCRIPTION	2012-04-19 15:38:26 UTC (rev 16)
@@ -1,8 +1,8 @@
 Package: candlesticks
 Type: Package
 Title: Candlestick Pattern Recognition
-Version: 0.1-11
-Date: 2012-04-11
+Version: 0.1-12
+Date: 2012-04-19
 Author: Andreas Voellenklee
 Maintainer: Andreas Voellenklee <wotuzu17 at gmail.com>
 Depends: R (>= 2.13), xts (>= 0.8-2), quantmod (>= 0.3-17), TTR (>= 0.21-0)

Modified: pkg/R/CSPHarami.R
===================================================================
--- pkg/R/CSPHarami.R	2012-04-17 12:53:14 UTC (rev 15)
+++ pkg/R/CSPHarami.R	2012-04-19 15:38:26 UTC (rev 16)
@@ -1,22 +1,19 @@
-CSPHarami <- function(TS, excludeDoji=FALSE, DojiBLRatio=.1) {
-  if (!is.OHLC(TS)) {
+CSPHarami <- function(TS, n=20, minbodysizeMedian=1) {
+  if (!is.OC(TS)) {
     stop("Price series must contain Open, High, Low and Close.")
   }
-  LAGTS <- LagOHLC(TS, k=1)
-  BullHarami <- reclass(eval( 
-    Op(LAGTS)>Cl(LAGTS) & Cl(TS)>Op(TS)
-    & Op(LAGTS)>Cl(TS) & Cl(LAGTS)<Op(TS) 
-    & Hi(LAGTS)>=Hi(TS) & Lo(LAGTS)<=Lo(TS) ), TS)
-  BearHarami <- reclass(eval( 
-    Cl(LAGTS)>Op(LAGTS) & Op(TS)>Cl(TS) 
-    & Cl(LAGTS)>Op(TS) & Op(LAGTS)<Cl(TS) 
-    & Hi(LAGTS)>=Hi(TS) & Lo(LAGTS)<=Lo(TS) ), TS)
-  # some don't accept the second candle being a doji
-  if (excludeDoji==TRUE) {
-    Doji <- CSPDoji(TS, DojiBLRatio)
-    BullHarami <- reclass(eval(BullHarami & !Doji[,1]), TS)
-    BearHarami <- reclass(eval(BearHarami & !Doji[,1]), TS)
-  }
+  LAGTS <- LagOC(TS, k=1)
+  LongCandleBody <- CSPLongCandleBody(LAGTS, n=n, threshold=minbodysizeMedian)
+  BullHarami <- reclass(eval(
+    LongCandleBody[,2] &                     # body of mother candle is black and longer than average
+    Cl(TS)>Op(TS) &                          # second candle is white
+    Op(LAGTS)>Cl(TS) & Cl(LAGTS)<Op(TS)      # second body is within first body
+    ), TS)
+  BearHarami <- reclass(eval(
+    LongCandleBody[,1] &                     # body of mother candle is white and longer than average
+    Cl(TS)<Op(TS) &                          # second candle is white
+    Op(LAGTS)<Cl(TS) & Cl(LAGTS)>Op(TS)      # second body is within first body
+    ), TS)
   result <- cbind(BullHarami, BearHarami)
   colnames(result) <- c("Bull.Harami", "Bear.Harami")
   xtsAttributes(result) <- list(bars=2)

Modified: pkg/R/CSPLongCandle.R
===================================================================
--- pkg/R/CSPLongCandle.R	2012-04-17 12:53:14 UTC (rev 15)
+++ pkg/R/CSPLongCandle.R	2012-04-19 15:38:26 UTC (rev 16)
@@ -1,4 +1,4 @@
-CSPLongCandle <- function(TS, n=20, threshold=1.5) {
+CSPLongCandle <- function(TS, n=20, threshold=1) {
   if (!is.OHLC (TS)) {
     stop("Price series must contain Open, High, Low and Close.")
   }
@@ -12,7 +12,7 @@
   return (result)
 }
 
-CSPLongCandleBody <- function(TS, n=20, threshold=1.5) {
+CSPLongCandleBody <- function(TS, n=20, threshold=1) {
   if (!is.OC (TS)) {
     stop("Price series must contain Open and Close.")
   }

Modified: pkg/R/CSPNLongCandles.R
===================================================================
--- pkg/R/CSPNLongCandles.R	2012-04-17 12:53:14 UTC (rev 15)
+++ pkg/R/CSPNLongCandles.R	2012-04-19 15:38:26 UTC (rev 16)
@@ -1,4 +1,4 @@
-CSPNLongWhiteCandles <- function(TS, N=2, n=20, threshold=1.5) {
+CSPNLongWhiteCandles <- function(TS, N=2, n=20, threshold=1) {
   if (!is.OHLC(TS)) {
     stop("Price series must contain Open, High, Low and Close.")
   }
@@ -12,7 +12,7 @@
   return (result)
 }
 
-CSPNLongBlackCandles <- function(TS, N=2, n=20, threshold=1.5) {
+CSPNLongBlackCandles <- function(TS, N=2, n=20, threshold=1) {
   if (!is.OHLC(TS)) {
     stop("Price series must contain Open, High, Low and Close.")
   }
@@ -26,7 +26,7 @@
   return (result)
 }
 
-CSPNLongWhiteCandleBodies <- function(TS, N=2, n=20, threshold=1.5) {
+CSPNLongWhiteCandleBodies <- function(TS, N=2, n=20, threshold=1) {
   if (!is.OC(TS)) {
     stop("Price series must contain Open and Close.")
   }
@@ -40,7 +40,7 @@
   return (result)
 }
 
-CSPNLongBlackCandleBodies <- function(TS, N=2, n=20, threshold=1.5) {
+CSPNLongBlackCandleBodies <- function(TS, N=2, n=20, threshold=1) {
   if (!is.OC(TS)) {
     stop("Price series must contain Open and Close.")
   }

Modified: pkg/R/CSPStar.R
===================================================================
--- pkg/R/CSPStar.R	2012-04-17 12:53:14 UTC (rev 15)
+++ pkg/R/CSPStar.R	2012-04-19 15:38:26 UTC (rev 16)
@@ -1,22 +1,24 @@
-CSPStar <- function (TS, n=20, lthreshold=1.5, sthreshold=1) {
-  if (!is.OHLC(TS)) {
-    stop("Price series must contain Open, High, Low and Close.")
+CSPStar <- function (TS, n=20, minbodysizeMedian=1, maxbodysizeMedian=1) {
+  if (!is.OC(TS)) {
+    stop("Price series must contain Open and Close.")
   }
-  LAG2TS <- LagOHLC(TS, k=2)
-  LAG1TS <- LagOHLC(TS, k=1)
-  LCB2 <- CSPLongCandleBody (LAG2TS, n=n, threshold=lthreshold)
-  GAP1 <- CSPGap(LAG1TS, ignoreShadows=TRUE)
-  SCB1 <- CSPShortCandleBody (LAG1TS, n=n, threshold=sthreshold)
+  LAG2TS <- LagOC(TS, k=2)
+  LAG1TS <- LagOC(TS, k=1)  
+  LCB2 <- CSPLongCandleBody (LAG2TS, n=n, threshold=minbodysizeMedian) # first candle body is longer than average
+  GAP1 <- CSPGap(LAG1TS, ignoreShadows=TRUE) # gap between the first and second candle
+  SCB1 <- CSPShortCandleBody (LAG1TS, n=n, threshold=maxbodysizeMedian)# second candle body is shorter than average
   MorningStar <- reclass(
     eval(LCB2[,2] &                # 1st candle: long black candle body
     GAP1[,2] &                     # gap down from 1st to 2nd candle
     (SCB1[,1] | SCB1[,2]) &        # 2nd candle: short black or white candle body
+    Cl(TS) > Op(TS) &              # 3rd candle is white
     Cl(TS) > (Op(LAG2TS)+Cl(LAG2TS))/2), # 3rd candle closes above middle of 1st candle body
     TS)
   EveningStar <- reclass(
     eval(LCB2[,1] &                # 1st candle: long white candle body
     GAP1[,1] &                     # gap up from 1st to 2nd candle
     (SCB1[,1] | SCB1[,2]) &        # 2nd candle: short black or white candle body
+    Cl(TS) < Op(TS) &              # third candle is white
     Cl(TS) < (Op(LAG2TS)+Cl(LAG2TS))/2), # 3rd candle closes below middle of 1st candle body
     TS)
   result <- cbind(MorningStar, EveningStar)

Modified: pkg/R/CSPThreeWhiteSoldiers.R
===================================================================
--- pkg/R/CSPThreeWhiteSoldiers.R	2012-04-17 12:53:14 UTC (rev 15)
+++ pkg/R/CSPThreeWhiteSoldiers.R	2012-04-19 15:38:26 UTC (rev 16)
@@ -1,31 +1,31 @@
-CSPThreeWhiteSoldiers <- function (TS, strict=TRUE, n=20, threshold=1.5) {
+CSPThreeWhiteSoldiers <- function (TS, strict=TRUE, n=20,  minbodysizeMedian=1) {
   if (!is.OC(TS)) {
     stop("Price series must contain Open and Close.")
   }
-  THREELWCB <- CSPNLongWhiteCandleBodies(TS, N=3, n=n, threshold=threshold)
-  LAGTS <- LagOC(TS,k=0:2)
+  THREELWCB <- CSPNLongWhiteCandleBodies(TS, N=3, n=n, threshold=minbodysizeMedian)
+  LAGTS <- LagOC(TS,k=0:2)  # lags 0, 1, 2 periods
   result <- reclass(eval(THREELWCB[,1] & 
-    Op(LAGTS)[,1] > Op(LAGTS)[,2] &
-    Op(LAGTS)[,2] > Op(LAGTS)[,3] &
-    Cl(LAGTS)[,1] > Cl(LAGTS)[,2] &
-    Cl(LAGTS)[,2] > Cl(LAGTS)[,3]), TS)
+    Op(LAGTS)[,1] > Op(LAGTS)[,2] & # third open higher than second
+    Op(LAGTS)[,2] > Op(LAGTS)[,3] & # second open higher than first
+    Cl(LAGTS)[,1] > Cl(LAGTS)[,2] & # third close higher than second
+    Cl(LAGTS)[,2] > Cl(LAGTS)[,3]), TS) # second close higher than first
   # in strict mode the candles should open within the previous
   # candle's body
   if (strict==TRUE) {
     result <- reclass(eval(result &
-      Op(LAGTS)[,1] <= Cl(LAGTS)[,2] &
-      Op(LAGTS)[,2] <= Cl(LAGTS)[,3]), TS)
+      Op(LAGTS)[,1] <= Cl(LAGTS)[,2] & # third open within second candle body
+      Op(LAGTS)[,2] <= Cl(LAGTS)[,3]), TS) # second open within first candle body
   }
   colnames(result) <- c("ThreeWhiteSoldiers")
   xtsAttributes(result) <- list(bars=3)
   return (result)
 }
 
-CSPThreeBlackCrows <- function (TS, strict=TRUE, n=20, threshold=1.5) {
+CSPThreeBlackCrows <- function (TS, strict=TRUE, n=20,  minbodysizeMedian=1) {
   if (!is.OC(TS)) {
     stop("Price series must contain Open and Close.")
   }
-  THREELBCB <- CSPNLongBlackCandleBodies(TS, N=3, n=n, threshold=threshold)
+  THREELBCB <- CSPNLongBlackCandleBodies(TS, N=3, n=n, threshold=minbodysizeMedian)
   LAGTS <- LagOC(TS,k=0:2)
   result <- reclass(eval(THREELBCB[,1] & 
     Op(LAGTS)[,1] < Op(LAGTS)[,2] &

Modified: pkg/man/CSPDarkCloudCover.Rd
===================================================================
--- pkg/man/CSPDarkCloudCover.Rd	2012-04-17 12:53:14 UTC (rev 15)
+++ pkg/man/CSPDarkCloudCover.Rd	2012-04-19 15:38:26 UTC (rev 16)
@@ -34,7 +34,8 @@
   # the lag of 2 periods of the time series for trend detection
   # ensures that the uptrend is active \emph{before} the
   # dark cloud cover occurs.
-  eval(CSPDarkCloudCover(YHOO) & TrendDetectionChannel(lag(YHOO,k=2))[,"UpTrend"])
+  eval(CSPDarkCloudCover(YHOO) & 
+    TrendDetectionChannel(lag(YHOO,k=2))[,"UpTrend"])
 }
 }
 \keyword{}

Modified: pkg/man/CSPHarami.Rd
===================================================================
--- pkg/man/CSPHarami.Rd	2012-04-17 12:53:14 UTC (rev 15)
+++ pkg/man/CSPHarami.Rd	2012-04-19 15:38:26 UTC (rev 16)
@@ -3,11 +3,11 @@
 \alias{Harami}
 \title{Harami Candlestick Pattern}
 \description{Look for Bullish/Bearish Harami Pattern in a OHLC price series}
-\usage{CSPHarami(TS, excludeDoji=FALSE, DojiBLRatio=.1)}
+\usage{CSPHarami(TS, n=20, minbodysizeMedian=1)}
 \arguments{
-  \item{TS}{xts Time Series containing Open, High, Low and Close Prices}
-  \item{excludeDoji}{if TRUE the second candle must not be a doji}
-  \item{DojiBLRatio}{maximum body to lenght ratio to identify the second candle as doji, default = .1}
+  \item{TS}{xts Time Series containing Open and Close Prices}
+  \item{n}{number of preceding candles to calculate median candle body length}
+  \item{minbodysizeMedian}{Minimum body length of the first candle (mother candle) relative to the median of the past \code{n} body sizes}
 }
 \details{
   Number of candle lines: \bold{2}\cr\cr
@@ -27,12 +27,18 @@
   \url{http://www.candlesticker.com/Bullish.asp}\cr
   \url{http://www.candlesticker.com/Bearish.asp}
 }
-\note{Trend detection prior to the formation is not implemented yet}
-\seealso{}
+\note{The function filters patterns that look like haramis, without considering the current trend direction. If only pattern in uptrends should be filtered, a external trend detection function must be used. See examples.}
+\seealso{
+ \code{\link{CSPLongCandleBody}}
+}
 \examples{
 \dontrun{
   getSymbols('YHOO',adjust=TRUE)
   CSPHarami(YHOO)
+  
+  # filter for bullish harami that occour in downtrends
+  BullHarami <- eval(CSPHarami(YHOO)[,"Bull.Harami"] & 
+    TrendDetectionChannel(lag(YHOO,k=2))[,"DownTrend"])
 }
 }
 \keyword{}

Modified: pkg/man/CSPLongCandle.Rd
===================================================================
--- pkg/man/CSPLongCandle.Rd	2012-04-17 12:53:14 UTC (rev 15)
+++ pkg/man/CSPLongCandle.Rd	2012-04-19 15:38:26 UTC (rev 16)
@@ -10,9 +10,9 @@
 \title{Long/Short Candlestick (Body) Pattern}
 \description{Look for long/short candlesticks in a OHLC price series}
 \usage{
-CSPLongCandle(TS, n=20, threshold=1.5)
+CSPLongCandle(TS, n=20, threshold=1)
 CSPShortCandle(TS, n=20, threshold=1)
-CSPLongCandleBody(TS, n=20, threshold=1.5)
+CSPLongCandleBody(TS, n=20, threshold=1)
 CSPShortCandleBody(TS, n=20, threshold=1)}
 \arguments{
   \item{TS}{xts Time Series containing OHLC prices}

Modified: pkg/man/CSPNLongWhiteCandles.Rd
===================================================================
--- pkg/man/CSPNLongWhiteCandles.Rd	2012-04-17 12:53:14 UTC (rev 15)
+++ pkg/man/CSPNLongWhiteCandles.Rd	2012-04-19 15:38:26 UTC (rev 16)
@@ -7,10 +7,10 @@
 \title{N consecutive long candles / bodies of same color}
 \description{look for N consecutive long candles / long candle bodies of same color}
 \usage{
-CSPNLongWhiteCandles(TS, N=2, n=20, threshold=1.5)
-CSPNLongBlackCandles(TS, N=2, n=20, threshold=1.5)
-CSPNLongWhiteCandleBodies(TS, N=2, n=20, threshold=1.5)
-CSPNLongBlackCandleBodies(TS, N=2, n=20, threshold=1.5)
+CSPNLongWhiteCandles(TS, N=2, n=20, threshold=1)
+CSPNLongBlackCandles(TS, N=2, n=20, threshold=1)
+CSPNLongWhiteCandleBodies(TS, N=2, n=20, threshold=1)
+CSPNLongBlackCandleBodies(TS, N=2, n=20, threshold=1)
 }
 \arguments{
   \item{TS}{xts Time Series containing OHLC prices}

Modified: pkg/man/CSPStar.Rd
===================================================================
--- pkg/man/CSPStar.Rd	2012-04-17 12:53:14 UTC (rev 15)
+++ pkg/man/CSPStar.Rd	2012-04-19 15:38:26 UTC (rev 16)
@@ -5,13 +5,13 @@
 \title{Morning/Evening Star Candlestick Pattern}
 \description{Look for Morning/Evening Star Patterns in a OHLC price series}
 \usage{
-CSPStar(TS, n=20, lthreshold=1.5, sthreshold=1)
+CSPStar(TS, n=20, minbodysizeMedian=1, maxbodysizeMedian=1)
 }
 \arguments{
   \item{TS}{xts Time Series containing OHLC prices}
   \item{n}{number of preceding candles to calculate median candle length}
-  \item{lthreshold}{minimum candle length in relation to the median candle length of the formation's \emph{first} candle}
-  \item{sthreshold}{maximum candle length in relation to the median candle length of the formation's \emph{second} candle}
+  \item{minbodysizeMedian}{minimum candle length in relation to the median candle length of the formation's \emph{first} candle}
+  \item{maxbodysizeMedian}{maximum candle length in relation to the median candle length of the formation's \emph{second} candle}
 }
 \details{
   Number of candle lines: \bold{3}\cr\cr
@@ -31,13 +31,21 @@
   \url{http://www.candlesticker.com/Bullish.asp}\cr
   \url{http://www.candlesticker.com/Bearish.asp}
 }
-\note{Trend detection prior to the formation is not implemented yet}
-\seealso{}
+\note{The function filters patterns that look like morning/evening stars, without considering the current trend direction. If only pattern in uptrends should be filtered, a external trend detection function must be used. See examples.}
+\seealso{
+ \code{\link{CSPLongCandleBody}}
+}
 \examples{
 \dontrun{
   getSymbols('YHOO',adjust=TRUE)
   CSPStar(YHOO)
-  CSPStar(YHOO, sthreshold=.5) # allow only a small second candle body
+  
+  # allow only a small second candle body
+  CSPStar(YHOO, maxbodysizeMedian=.5)
+  
+  # filter for morning stars that occour in downtrends
+  MorningStar <- eval(CSPStar(YHOO)[,"MorningStar"] & 
+    TrendDetectionChannel(lag(YHOO,k=3))[,"DownTrend"])
 }
 }
 \keyword{}

Modified: pkg/man/CSPThreeBlackCrows.Rd
===================================================================
--- pkg/man/CSPThreeBlackCrows.Rd	2012-04-17 12:53:14 UTC (rev 15)
+++ pkg/man/CSPThreeBlackCrows.Rd	2012-04-19 15:38:26 UTC (rev 16)
@@ -4,13 +4,13 @@
 \title{Three Black Crows Candlestick Pattern}
 \description{Look for Three Black Crows in a Open/Close price series}
 \usage{
-  CSPThreeBlackCrows(TS, strict=TRUE, n=20, threshold=1.5)
+  CSPThreeBlackCrows(TS, strict=TRUE, n=20,  minbodysizeMedian=1)
 }
 \arguments{
   \item{TS}{xts Time Series containing Open and Close prices}
   \item{strict}{if FALSE, use less strict conditions to detect pattern. See details.}
   \item{n}{number of preceding candles to calculate median candle length}
-  \item{threshold}{minimum candle length in relation to the median candle length of \code{n} preceding candles}  
+  \item{minbodysizeMedian}{minimum candle length in relation to the median candle length of \code{n} preceding candles}  
 }
 \details{
   Number of candle lines: \bold{3}\cr\cr
@@ -26,7 +26,7 @@
   \url{http://www.candlesticker.com/Bullish.asp}\cr
   \url{http://www.candlesticker.com/Bearish.asp}
 }
-\note{Trend detection prior to the formation is not implemented yet}
+\note{The function filters patterns that look like three black crows, without considering the current trend direction. If only pattern in uptrends should be filtered, a external trend detection function must be used. See examples.}
 \seealso{
   \code{\link{ThreeWhiteSoldiers}}
   \code{\link{CSPNLongBlackCandles}}
@@ -37,6 +37,10 @@
   getSymbols('YHOO',adjust=TRUE)
   CSPThreeBlackCrows(YHOO)
   CSPThreeBlackCrows(YHOO, strict=FALSE)
+  
+  # filter for three black crows that occur in uptrends
+  ThreeBlackCrows <- eval(CSPThreeBlackCrows(YHOO) & 
+    TrendDetectionChannel(lag(YHOO,k=3))[,"UpTrend"])
 }
 }
 \keyword{}

Modified: pkg/man/CSPThreeWhiteSoldiers.Rd
===================================================================
--- pkg/man/CSPThreeWhiteSoldiers.Rd	2012-04-17 12:53:14 UTC (rev 15)
+++ pkg/man/CSPThreeWhiteSoldiers.Rd	2012-04-19 15:38:26 UTC (rev 16)
@@ -4,13 +4,13 @@
 \title{Three White Soldiers Candlestick Pattern}
 \description{Look for Three White Soldiers in a Open/Close price series}
 \usage{
-  CSPThreeWhiteSoldiers(TS, strict=TRUE, n=20, threshold=1.5)
+  CSPThreeWhiteSoldiers(TS, strict=TRUE, n=20,  minbodysizeMedian=1)
 }
 \arguments{
   \item{TS}{xts Time Series containing Open and Close prices}
   \item{strict}{if FALSE, use less strict conditions to detect pattern. See details.}
   \item{n}{number of preceding candles to calculate median candle length}
-  \item{threshold}{minimum candle length in relation to the median candle length of \code{n} preceding candles}  
+  \item{minbodysizeMedian}{minimum candle length in relation to the median candle length of \code{n} preceding candles}  
 }
 \details{
   Number of candle lines: \bold{3}\cr\cr
@@ -26,7 +26,7 @@
   \url{http://www.candlesticker.com/Bullish.asp}\cr
   \url{http://www.candlesticker.com/Bearish.asp}
 }
-\note{Trend detection prior to the formation is not implemented yet}
+\note{The function filters patterns that look like three white soldiers, without considering the current trend direction. If only pattern in downtrends should be filtered, a external trend detection function must be used. See examples.}
 \seealso{
   \code{\link{ThreeBlackCrows}}
   \code{\link{CSPNLongWhiteCandles}}
@@ -37,6 +37,10 @@
   getSymbols('YHOO',adjust=TRUE)
   CSPThreeWhiteSoldiers(YHOO)
   CSPThreeWhiteSoldiers(YHOO, strict=FALSE)
+  
+  # filter for three white soldiers that occur in downtrends
+  ThreeWhiteSoldiers <- eval(CSPThreeWhiteSoldiers(YHOO) & 
+    TrendDetectionChannel(lag(YHOO,k=3))[,"DownTrend"])
 }
 }
 \keyword{}



More information about the Candlesticks-commits mailing list