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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Apr 10 22:34:40 CEST 2012


Author: wotuzu17
Date: 2012-04-10 22:34:40 +0200 (Tue, 10 Apr 2012)
New Revision: 13

Added:
   pkg/R/DonchianChannel2.R
   pkg/man/DonchianChannel2.Rd
Modified:
   pkg/NAMESPACE
   pkg/R/CSPDoji.R
   pkg/R/CSPHammer.R
   pkg/R/CSPHangingMan.R
   pkg/R/CSPMarubozu.R
   pkg/R/TrendDetectionChannel.R
   pkg/man/CSPDoji.Rd
   pkg/man/CSPMarubozu.Rd
Log:
re-coded filter rules for 1 day patterns

Modified: pkg/NAMESPACE
===================================================================
--- pkg/NAMESPACE	2012-04-09 15:41:06 UTC (rev 12)
+++ pkg/NAMESPACE	2012-04-10 20:34:40 UTC (rev 13)
@@ -40,6 +40,7 @@
 export(addPriceInfo)
 export(CandleBodyLength)
 export(CandleLength)
+export(DonchianChannel2)
 export(is.HL)
 export(is.OC)
 

Modified: pkg/R/CSPDoji.R
===================================================================
--- pkg/R/CSPDoji.R	2012-04-09 15:41:06 UTC (rev 12)
+++ pkg/R/CSPDoji.R	2012-04-10 20:34:40 UTC (rev 13)
@@ -1,10 +1,14 @@
-CSPDoji <- function(TS, DojiBLRatio=.1) {
+CSPDoji <- function(TS, maxbodyCL=.1, maxshadowCL=.1) {
   if (!is.OHLC(TS)) {
     stop("Price series must contain Open, High, Low and Close.")
   }
-  Doji <- reclass(eval (abs(Op(TS)-Cl(TS))/(Hi(TS)-Lo(TS)) <= DojiBLRatio), TS)
-  DFDoji <- reclass(eval (Doji & (Op(TS)==Hi(TS) | Cl(TS)==Hi(TS))), TS)
-  GSDoji <- reclass(eval (Doji & (Op(TS)==Lo(TS) | Cl(TS)==Lo(TS))), TS)
+  BL <- abs(Cl(TS)-Op(TS))
+  CL <- Hi(TS)-Lo(TS)
+  BodyHi <- as.xts(apply(cbind(Op(TS),Cl(TS)),1,max))
+  BodyLo <- as.xts(apply(cbind(Op(TS),Cl(TS)),1,min))
+  Doji <- reclass(eval (BL < CL* maxbodyCL), TS)
+  DFDoji <- reclass(eval (Doji & (Hi(TS)-BodyHi <= CL* maxshadowCL)), TS)
+  GSDoji <- reclass(eval (Doji & (BodyLo-Lo(TS) <= CL* maxshadowCL)), TS)
   result <- cbind(Doji, DFDoji, GSDoji)
   colnames(result) <- c("Doji", "DragonflyDoji", "GravestoneDoji")
   xtsAttributes(result) <- list(bars=1)

Modified: pkg/R/CSPHammer.R
===================================================================
--- pkg/R/CSPHammer.R	2012-04-09 15:41:06 UTC (rev 12)
+++ pkg/R/CSPHammer.R	2012-04-10 20:34:40 UTC (rev 13)
@@ -7,7 +7,7 @@
   BodyLo <- as.xts(apply(cbind(Op(TS),Cl(TS)),1,min))
   Hammer <- reclass( eval (
     BodyLo-Lo(TS) > CL*minlowershadowCL &   # lower shadow greater than lowershadeCL*CandleLength
-    Hi(TS)- BodyHi < CL*maxuppershadowCL &  # upper shadow missing or very short
+    Hi(TS)- BodyHi <= CL*maxuppershadowCL & # upper shadow missing or very short
     abs (Cl(TS)-Op(TS)) > CL*minbodyCL)     # Body length greater than minbodyCL*CandleLength
     ,TS)
   colnames(Hammer) <- c("Hammer")

Modified: pkg/R/CSPHangingMan.R
===================================================================
--- pkg/R/CSPHangingMan.R	2012-04-09 15:41:06 UTC (rev 12)
+++ pkg/R/CSPHangingMan.R	2012-04-10 20:34:40 UTC (rev 13)
@@ -7,7 +7,7 @@
   BodyLo <- as.xts(apply(cbind(Op(TS),Cl(TS)),1,min))
   HangingMan <- reclass( eval (
     Hi(TS)- BodyHi > CL*minuppershadowCL &   # upper shadow greater than lowershadeCL*CandleLength
-    BodyLo- Lo(TS) < CL*maxlowershadowCL &   # lower shadow missing or very short
+    BodyLo- Lo(TS) <= CL*maxlowershadowCL &  # lower shadow missing or very short
     abs (Cl(TS)-Op(TS)) > CL*minbodyCL)      # Body length greater than minbodyCL*CandleLength
     ,TS)
   colnames(HangingMan) <- c("HangingMan")

Modified: pkg/R/CSPMarubozu.R
===================================================================
--- pkg/R/CSPMarubozu.R	2012-04-09 15:41:06 UTC (rev 12)
+++ pkg/R/CSPMarubozu.R	2012-04-10 20:34:40 UTC (rev 13)
@@ -1,18 +1,17 @@
-CSPMarubozu <- function(TS, n=20, threshold=1.5) {
+CSPMarubozu <- function(TS, n=20, ATRFactor=1, maxuppershadowCL=.1, maxlowershadowCL=.1) {
   if (!is.OHLC(TS)) {
     stop("Price series must contain Open, High, Low and Close.")
   }
-  LCB <- CSPLongCandleBody(TS, n=n, threshold=threshold)
-  WhiteMarubozu <- reclass(eval( LCB[,"LongWhiteCandleBody"] & Op(TS)==Lo(TS) & Cl(TS)==Hi(TS) ), TS)
-  WhiteOpeningMarubozu <- reclass(eval( LCB[,"LongWhiteCandleBody"] & Op(TS)==Lo(TS) & Cl(TS)<Hi(TS) ), TS)
-  WhiteClosingMarubozu <- reclass(eval( LCB[,"LongWhiteCandleBody"] & Op(TS)>Lo(TS) & Cl(TS)==Hi(TS) ), TS)
-  BlackMarubozu <- reclass(eval( LCB[,"LongBlackCandleBody"] & Op(TS)==Hi(TS) & Cl(TS)==Lo(TS) ), TS)
-  BlackOpeningMarubozu <- reclass(eval( LCB[,"LongBlackCandleBody"] & Op(TS)==Hi(TS) & Cl(TS)>Lo(TS) ), TS)
-  BlackClosingMarubozu <- reclass(eval( LCB[,"LongBlackCandleBody"] & Op(TS)<Hi(TS) & Cl(TS)==Lo(TS) ), TS)
-  result <- cbind(WhiteMarubozu, WhiteOpeningMarubozu, WhiteClosingMarubozu, 
-                  BlackMarubozu, BlackOpeningMarubozu, BlackClosingMarubozu)
-  colnames(result) <- c("WhiteMarubozu", "WhiteOpeningMarubozu", "WhiteClosingMarubozu", 
-                        "BlackMarubozu", "BlackOpeningMarubozu", "BlackClosingMarubozu")
+  LongCandle = eval(CandleBodyLength(TS)[,"absCandleBodyLength"] > 
+    ATR(cbind(Hi(TS), Lo(TS), Cl(TS)), n=n, maType="SMA")[,"atr"]*ATRFactor)
+  CL <- Hi(TS)-Lo(TS)
+  BodyHi <- as.xts(apply(cbind(Op(TS),Cl(TS)),1,max))
+  BodyLo <- as.xts(apply(cbind(Op(TS),Cl(TS)),1,min))
+  ShortShadow = eval(Hi(TS)-BodyHi <= CL*maxuppershadowCL & BodyLo-Lo(TS) <= CL*maxlowershadowCL)
+  WhiteMarubozu <- reclass(eval( LongCandle & ShortShadow & Op(TS) < Cl(TS)), TS)
+  BlackMarubozu <- reclass(eval( LongCandle & ShortShadow & Op(TS) > Cl(TS)), TS)
+  result <- cbind(WhiteMarubozu, BlackMarubozu)
+  colnames(result) <- c("WhiteMarubozu", "BlackMarubozu")
   xtsAttributes(result) <- list(bars=1)
   return(result)
 }
\ No newline at end of file

Added: pkg/R/DonchianChannel2.R
===================================================================
--- pkg/R/DonchianChannel2.R	                        (rev 0)
+++ pkg/R/DonchianChannel2.R	2012-04-10 20:34:40 UTC (rev 13)
@@ -0,0 +1,17 @@
+# DonchianChannel function in TTR lacks a 1 period-lag of the result
+# hence, this modified (corrected) version is used in package candlesticks
+DonchianChannel2 <- function(HL, n = 10) {
+    if (NCOL(HL) > 1 && (!has.Lo(HL) || !has.Hi(HL))) {
+      stop("Price series must either contain High and Low, or be univariate.")
+    }
+    if (NCOL(HL) > 1) {
+      hi <- Hi(HL)[, 1]
+      lo <- Lo(HL)[, 1]
+    } else hi <- lo <- HL
+    high <- runMax(hi, n)
+    low <- runMin(lo, n)
+    mid <- (high + low)/2
+    result <- lag(cbind(high, mid, low))
+    colnames(result) <- c("high", "mid", "low")
+    return(result)
+  }
\ No newline at end of file

Modified: pkg/R/TrendDetectionChannel.R
===================================================================
--- pkg/R/TrendDetectionChannel.R	2012-04-09 15:41:06 UTC (rev 12)
+++ pkg/R/TrendDetectionChannel.R	2012-04-10 20:34:40 UTC (rev 13)
@@ -2,7 +2,7 @@
   if (!is.OHLC(TS)) {
     stop("Price series must contain Open, High, Low and Close.")
   }
-  Channel <- lag(DonchianChannel(cbind(Hi(TS),Lo(TS)), n=n), k=1)
+  Channel <- DonchianChannel2(cbind(Hi(TS),Lo(TS)), n=n)
   UpTrend <- eval(Cl(TS) > Lo(Channel)+(Hi(Channel)-Lo(Channel))*(1-DCSector))
   DownTrend <- eval(Cl(TS) < Lo(Channel)+(Hi(Channel)-Lo(Channel))*DCSector)
   NoTrend <- eval(!(UpTrend | DownTrend))

Modified: pkg/man/CSPDoji.Rd
===================================================================
--- pkg/man/CSPDoji.Rd	2012-04-09 15:41:06 UTC (rev 12)
+++ pkg/man/CSPDoji.Rd	2012-04-10 20:34:40 UTC (rev 13)
@@ -3,10 +3,11 @@
 \alias{Doji}
 \title{Doji Candlestick Pattern}
 \description{Look for Doji Pattern in a OHLC price series}
-\usage{CSPDoji(TS, DojiBLRatio=.1)}
+\usage{CSPDoji(TS, maxbodyCL=.1, maxshadowCL=.1)}
 \arguments{
   \item{TS}{xts Time Series containing Open and Close Prices}
-  \item{DojiBLRatio}{maximum body to lenght ratio, default = .1}
+  \item{maxbodyCL}{maximum body to length ratio, default = .1}
+  \item{maxshadowCL}{maximum tolerated upper (lower) shadow to identify a dragonfly (gravestone) doji}
 }
 \details{
 Number of candle lines: \bold{1}\cr
@@ -26,15 +27,20 @@
   \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 candles that look like dojis, without considering the current trend direction. If only doji patterns in a uptrend should be filtered, a external trend detection function must be used. See examples.}
 \seealso{}
 \examples{
 \dontrun{
   getSymbols('YHOO',adjust=TRUE)
+  
   CSPDoji(YHOO)
-  CSPDoji(YHOO, DojiBLRatio=0) # filter for doji patterns 
-                               # that have exactly equal 
-                               # open and close prices
+  
+  # filter for doji patterns that have exactly equal 
+  # open and close prices
+  CSPDoji(YHOO, DojiBLRatio=0)
+  
+  # filter for gravestone doji patterns that occur in uptrends
+  eval(CSPDoji(YHOO)[,"GravestoneDoji"] & TrendDetectionChannel(YHOO)[,"UpTrend"])
 }
 }
 \keyword{}

Modified: pkg/man/CSPMarubozu.Rd
===================================================================
--- pkg/man/CSPMarubozu.Rd	2012-04-09 15:41:06 UTC (rev 12)
+++ pkg/man/CSPMarubozu.Rd	2012-04-10 20:34:40 UTC (rev 13)
@@ -3,35 +3,25 @@
 \alias{Marubozu}
 \title{Marubozu Candlestick Pattern}
 \description{Look for Marubozu Candlestick Patterns in a OHLC price series}
-\usage{CSPMarubozu(TS, n=20, threshold=1.5)}
+\usage{CSPMarubozu(TS, n=20, ATRFactor=1, maxuppershadowCL=.1, maxlowershadowCL=.1)}
 \arguments{
   \item{TS}{xts Time Series containing OHLC prices}
-  \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{n}{number of preceding candles to calculate Average True Range}
+  \item{ATRFactor}{minimum size of candle body compared to the ATR}
+  \item{maxuppershadowCL}{maximum tolerated upper shadow to candle length ratio}
+  \item{maxlowershadowCL}{maximum tolerated lower shadow to candle length ratio}
 }
 \details{
 Number of candle lines: \bold{1}\cr\cr
 \emph{White Marubozu:}\cr
-A long white candle that has no shadows on either end. Op=Lo, Cl=Hi.\cr\cr
-\emph{White Opening Marubozu:}\cr
-A long white candle body with an upper shadow but no lower shadow. Op=Lo.\cr\cr
-\emph{White Closing Marubozu:}\cr
-A long white candle body with a lower shadow but no upper shadow. Cl=Hi.\cr\cr
+A long white candle that has no shadows or only small shadows on either end.\cr\cr
 \emph{Black Marubozu:}\cr
-A long black candle that has no shadows on either end. Op=Hi, Cl=Lo.\cr\cr
-\emph{Black Opening Marubozu:}\cr
-A long black candle body with a lower shadow but no upper shadow. Op=Hi.\cr\cr
-\emph{Black Closing Marubozu:}\cr
-A long black candle body with a upper shadow but no lower shadow. Cl=Lo.\cr\cr
+A long black candle that has no shadows or only small shadows on either end.\cr\cr
 }
 \value{
   A xts object containing the columns:
   \item{WhiteMarubozu}{TRUE if pattern detected}
-  \item{WhiteOpeningMarubozu}{TRUE if pattern detected}
-  \item{WhiteClosingMarubozu}{TRUE if pattern detected}
   \item{BlackMarubozu}{TRUE if pattern detected}
-  \item{BlackOpeningMarubozu}{TRUE if pattern detected}
-  \item{BlackClosingMarubozu}{TRUE if pattern detected}
 }
 \author{Andreas Voellenklee}
 \references{
@@ -39,13 +29,24 @@
   \url{http://www.candlesticker.com/Bullish.asp}\cr
   \url{http://www.candlesticker.com/Bearish.asp}
 }
-\note{}
-\seealso{\code{\link{CSPLongCandle}}}
+\note{
+In default settings, the candle \emph{body} length must be greater than the average true range of last \code{n} periods. The threshold can be varied by \code{ATRFactor}.
+}
+\seealso{
+\code{\link{CandleBodyLength}}
+\code{\link{ATR}}
+}
 \examples{
 \dontrun{
   getSymbols('YHOO',adjust=TRUE)
+
   CSPMarubozu(YHOO)
-  CSPMarubozu(YHOO, threshold=2) # filter for very long marubozus
+  
+  # include not-so-long-marubozus
+  CSPMarubozu(YHOO, ATRFactor=.8)
+  
+  # filter for white closing marubozus (Cl(TS)=Hi(TS))
+  CSPMarubozu(YHOO, maxuppershadowCL=0)[,"WhiteMarubozu"]
 }
 }
 \keyword{}

Added: pkg/man/DonchianChannel2.Rd
===================================================================
--- pkg/man/DonchianChannel2.Rd	                        (rev 0)
+++ pkg/man/DonchianChannel2.Rd	2012-04-10 20:34:40 UTC (rev 13)
@@ -0,0 +1,46 @@
+\name{DonchianChannel2}
+\alias{DonchianChannel2}
+\title{ Donchian Channel }
+\description{
+  Donchian Channels were created by Richard Donchian and were used to
+  generate buy and sell signals for the Turtle Trading system.
+}
+\usage{
+  DonchianChannel2(HL, n=10)
+}
+\arguments{
+  \item{HL}{ Object that is coercible to xts or matrix and contains High and Low prices or Close prices. }
+  \item{n}{ Number of periods to include in price band calculation }
+}
+\details{
+  Donchian Channels consist of two (sometimes three) lines:
+
+  The top line is the highest high of the past \code{n} periods.
+  The bottom line is the lowest low of the past \code{n} periods.
+  The middle line is the average of the top and bottom lines.
+}
+\value{
+  A object of the same class as \code{HL} or a matrix (if \code{try.xts}
+  fails) containing the columns:
+  \item{ high }{ The highest high series. }
+  \item{ mid }{ The average of \code{high} and \code{low}. }
+  \item{ low }{ The lowest low series. }
+}
+\note{
+The DonchianChannel function in the package \code{TTR} includes the current price bar
+for the channel calculation. In contrary, this DonchianChannel2 function calculates
+the price bands on the \emph{previous} \code{n} price bars.
+}
+\author{ G See }
+\references{
+  The following site(s) were used to code/document this indicator:\cr
+  \url{http://www.linnsoft.com/tour/techind/donch.htm}\cr
+}
+\seealso{
+  See \code{\link{DonchianChannel}}.
+}
+\examples{
+  getSymbols(YHOO, adjust=TRUE)
+  dc <- DonchianChannel2(YHOO)
+}
+\keyword{ ts }


Property changes on: pkg/man/DonchianChannel2.Rd
___________________________________________________________________
Added: svn:executable
   + *



More information about the Candlesticks-commits mailing list