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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Apr 9 17:41:06 CEST 2012


Author: wotuzu17
Date: 2012-04-09 17:41:06 +0200 (Mon, 09 Apr 2012)
New Revision: 12

Added:
   pkg/R/CSPHammer.R
   pkg/R/CSPHangingMan.R
   pkg/R/TrendDetectionChannel.R
   pkg/R/TrendDetectionSMA.R
   pkg/man/CSPHammer.Rd
   pkg/man/CSPHangingMan.Rd
   pkg/man/TrendDetectionChannel.Rd
   pkg/man/TrendDetectionSMA.Rd
Modified:
   pkg/DESCRIPTION
   pkg/NAMESPACE
   pkg/man/CSPDarkCloudCover.Rd
Log:
added trend detection functions

Modified: pkg/DESCRIPTION
===================================================================
--- pkg/DESCRIPTION	2012-02-15 15:22:38 UTC (rev 11)
+++ pkg/DESCRIPTION	2012-04-09 15:41:06 UTC (rev 12)
@@ -1,8 +1,8 @@
 Package: candlesticks
 Type: Package
 Title: Candlestick Pattern Recognition
-Version: 0.1-9
-Date: 2012-02-13
+Version: 0.1-10
+Date: 2012-04-08
 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/NAMESPACE
===================================================================
--- pkg/NAMESPACE	2012-02-15 15:22:38 UTC (rev 11)
+++ pkg/NAMESPACE	2012-04-09 15:41:06 UTC (rev 12)
@@ -3,6 +3,8 @@
 export(CSPDoji)
 export(CSPEngulfing)
 export(CSPGap)
+export(CSPHammer)
+export(CSPHangingMan)
 export(CSPHarami)
 export(CSPInsideDay)
 export(CSPKicking)
@@ -28,6 +30,9 @@
 export(CSPThreeMethods)
 export(CSPThreeOutside)
 export(CSPThreeWhiteSoldiers)
+# trend detection functions
+export(TrendDetectionChannel)
+export(TrendDetectionSMA)
 # lag functions
 export(LagOHLC)
 export(LagOC)

Added: pkg/R/CSPHammer.R
===================================================================
--- pkg/R/CSPHammer.R	                        (rev 0)
+++ pkg/R/CSPHammer.R	2012-04-09 15:41:06 UTC (rev 12)
@@ -0,0 +1,16 @@
+CSPHammer <- function(TS, minlowershadowCL=2/3, maxuppershadowCL=.1, minbodyCL=.1) {
+  if (!is.OHLC(TS)) {
+    stop("Price series must contain Open, High, Low and Close.")
+  }
+  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))
+  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
+    abs (Cl(TS)-Op(TS)) > CL*minbodyCL)     # Body length greater than minbodyCL*CandleLength
+    ,TS)
+  colnames(Hammer) <- c("Hammer")
+  xtsAttributes(Hammer) <- list(bars=1)
+  return (Hammer)
+}

Added: pkg/R/CSPHangingMan.R
===================================================================
--- pkg/R/CSPHangingMan.R	                        (rev 0)
+++ pkg/R/CSPHangingMan.R	2012-04-09 15:41:06 UTC (rev 12)
@@ -0,0 +1,16 @@
+CSPHangingMan <- function(TS, minuppershadowCL=2/3, maxlowershadowCL=.1, minbodyCL=.1) {
+  if (!is.OHLC(TS)) {
+    stop("Price series must contain Open, High, Low and Close.")
+  }
+  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))
+  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
+    abs (Cl(TS)-Op(TS)) > CL*minbodyCL)      # Body length greater than minbodyCL*CandleLength
+    ,TS)
+  colnames(HangingMan) <- c("HangingMan")
+  xtsAttributes(HangingMan) <- list(bars=1)
+  return (HangingMan)
+}

Added: pkg/R/TrendDetectionChannel.R
===================================================================
--- pkg/R/TrendDetectionChannel.R	                        (rev 0)
+++ pkg/R/TrendDetectionChannel.R	2012-04-09 15:41:06 UTC (rev 12)
@@ -0,0 +1,13 @@
+TrendDetectionChannel<- function(TS, n=20, DCSector=1/3) {
+  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)
+  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))
+  Trend <- UpTrend+ DownTrend*(-1)
+  result <- cbind(UpTrend, NoTrend, DownTrend, Trend)
+  colnames(result) <- c("UpTrend", "NoTrend", "DownTrend", "Trend")
+  return(result)
+}
\ No newline at end of file

Added: pkg/R/TrendDetectionSMA.R
===================================================================
--- pkg/R/TrendDetectionSMA.R	                        (rev 0)
+++ pkg/R/TrendDetectionSMA.R	2012-04-09 15:41:06 UTC (rev 12)
@@ -0,0 +1,14 @@
+TrendDetectionSMA<- function(TS, n=20) {
+  if (!has.Cl(TS)) {
+    stop("Price series must contain Close prices")
+  }
+  Close <- Cl(TS)
+  CloseToSMA <- Close/SMA(Close,n=n)
+  UpTrend <- eval(CloseToSMA[,1] > 1)
+  NoTrend <- eval(CloseToSMA[,1] == 1)
+  DownTrend <- eval(CloseToSMA[,1] < 1)
+  Trend <- UpTrend+ DownTrend*(-1)
+  result <- cbind(UpTrend, NoTrend, DownTrend, Trend)
+  colnames(result) <- c("UpTrend", "NoTrend", "DownTrend", "Trend")
+  return(result)
+}
\ No newline at end of file

Modified: pkg/man/CSPDarkCloudCover.Rd
===================================================================
--- pkg/man/CSPDarkCloudCover.Rd	2012-02-15 15:22:38 UTC (rev 11)
+++ pkg/man/CSPDarkCloudCover.Rd	2012-04-09 15:41:06 UTC (rev 12)
@@ -5,7 +5,7 @@
 \description{Look for Dark Cloud Covers in a Open/Close price series}
 \usage{CSPDarkCloudCover(TS)}
 \arguments{
-  \item{TS}{xts Time Series containing Open and Close Prices}
+  \item{TS}{xts Time Series containing Open and Close prices}
 }
 \details{
 A white candlestick is followed by a gap higher during the next day while the market is in uptrend. The day ends up as a black candlestick, which closes more than halfway into the prior black candlestick's real body.

Added: pkg/man/CSPHammer.Rd
===================================================================
--- pkg/man/CSPHammer.Rd	                        (rev 0)
+++ pkg/man/CSPHammer.Rd	2012-04-09 15:41:06 UTC (rev 12)
@@ -0,0 +1,44 @@
+\name{CSPHammer}
+\alias{CSPHammer}
+\alias{Hammer}
+\title{Hammer Candlestick Pattern}
+\description{Look for Hammer Pattern in a OHLC price series}
+\usage{CSPHammer(TS, minlowershadowCL=2/3, maxuppershadowCL=.1, minbodyCL=.1)}
+\arguments{
+  \item{TS}{xts Time Series containing Open, High, Low and Close Prices}
+  \item{minlowershadowCL}{minimum lower shadow to candle length ratio}
+  \item{maxuppershadowCL}{maximum tolerated upper shadow to candle length ratio}
+  \item{minbodyCL}{minimum body to candle length ratio}
+}
+\details{
+Number of candle lines: \bold{1}\cr\cr
+The hammer is a one-day formation. This patter is expected to be a early sign for the reversal of a downtrend into an uptrend. It has got a long lower shadow, a small body at the top of the candle, and no or only a very short upper shadow. The color of the body is not important. The counterpart of this pattern is \code{\link{HangingMan}}.
+}
+\value{
+  A xts object containing the column:
+  \item{Hammer}{TRUE if hammer pattern detected}
+}
+\author{Andreas Voellenklee}
+\references{
+The following site(s) were used to code/document this candlestick pattern:\cr
+  \url{http://www.candlesticker.com/Cs18.asp}\cr
+}
+\note{The function filters candles that look like hammers, without considering the current trend direction. If only hammer patterns in a downtrend should be filtered, a external trend detection function must be used. See examples.}
+\seealso{
+\code{\link{CSPDoji}}
+\code{\link{CSPHangingMan}}
+\code{\link{TrendDetectionChannel}}
+\code{\link{TrendDetectionSMA}}
+}
+\examples{
+\dontrun{
+  getSymbols('YHOO',adjust=TRUE)
+  
+  # filter for hammer patterns
+  CSPHammer(YHOO
+  
+  # filter for hammer patterns that occur in downtrends
+  eval(CSPHammer(YHOO) & TrendDetectionChannel(YHOO)[,"DownTrend"])
+}
+}
+\keyword{}

Added: pkg/man/CSPHangingMan.Rd
===================================================================
--- pkg/man/CSPHangingMan.Rd	                        (rev 0)
+++ pkg/man/CSPHangingMan.Rd	2012-04-09 15:41:06 UTC (rev 12)
@@ -0,0 +1,44 @@
+\name{CSPHangingMan}
+\alias{CSPHangingMan}
+\alias{HangingMan}
+\title{Hanging Man Candlestick Pattern}
+\description{Look for Hanging Man Pattern in a OHLC price series}
+\usage{CSPHangingMan(TS, minuppershadowCL=2/3, maxlowershadowCL=.1, minbodyCL=.1)}
+\arguments{
+  \item{TS}{xts Time Series containing Open, High, Low and Close Prices}
+  \item{minuppershadowCL}{minimum upper shadow to candle length ratio}
+  \item{maxlowershadowCL}{maximum tolerated lower shadow to candle length ratio}
+  \item{minbodyCL}{minimum body to candle length ratio}
+}
+\details{
+Number of candle lines: \bold{1}\cr\cr
+The hanging man is a one-day formation. This patter is expected to be a early sign for the reversal of a uptrend into an downtrend. It has got a long upper shadow, a small body at the bottom of the candle, and no or only a very short lower shadow. The color of the body is not important. The counterpart of this pattern is \code{\link{Hammer}}.
+}
+\value{
+  A xts object containing the column:
+  \item{HangingMan}{TRUE if hammer pattern detected}
+}
+\author{Andreas Voellenklee}
+\references{
+The following site(s) were used to code/document this candlestick pattern:\cr
+  \url{http://www.candlesticker.com/Cs26.asp}\cr
+}
+\note{The function filters candles that look like hanging mans, without considering the current trend direction. If only hanging man patterns in a uptrend should be filtered, a external trend detection function must be used. See examples.}
+\seealso{
+\code{\link{CSPDoji}}
+\code{\link{CSPHammer}}
+\code{\link{TrendDetectionChannel}}
+\code{\link{TrendDetectionSMA}}
+}
+\examples{
+\dontrun{
+  getSymbols('YHOO',adjust=TRUE)
+  
+  # filter for hanging man patterns
+  CSPHangingMan(YHOO)
+  
+  # filter for hanging man patterns that occur in uptrends
+  eval(CSPHangingMan(YHOO) & TrendDetectionChannel(YHOO)[,"UpTrend"])
+}
+}
+\keyword{}

Added: pkg/man/TrendDetectionChannel.Rd
===================================================================
--- pkg/man/TrendDetectionChannel.Rd	                        (rev 0)
+++ pkg/man/TrendDetectionChannel.Rd	2012-04-09 15:41:06 UTC (rev 12)
@@ -0,0 +1,37 @@
+\name{TrendDetectionChannel}
+\alias{TrendDetectionChannel}
+\title{Current Trend Detection using Donchian Channel}
+\description{Uses the Donchian Channel of \code{n} periods to determine whether a price series is in uptrend, sideward trend or downtrend}
+\usage{TrendDetectionChannel(TS, n=20, DCSector=1/3)}
+\arguments{
+  \item{TS}{xts Time Series containing OHLC prices}
+  \item{n}{number of periods to calculate the high and low band of the Donchian Channel}
+  \item{DCSector}{sector of Donchian Channel to determine uptrend / downtrend}
+}
+\details{
+This function assumes that a price series is in uptrend when a period's price closes above the upper third of the Donchian Channel of \code{n} periods. If the price close below the lower third of the channel, a downtrend is detected. If the price closes within the middle third of the channel, a sideward trend is detected. The parameter DCSector can be used to widen/narrow the threshold of up/downtrend detection. E.g. a value of .25 anticipates the price for a uptrend to be above the highest \emph{quarter} of the donchian channel
+}
+\value{
+  A xts object containing the columns:
+  \item{UpTrend}{TRUE if uptrend detected}
+  \item{NoTrend}{TRUE if sideward trend detected}
+  \item{DownTrend}{TRUE if downtrend detected}
+  \item{Trend}{+1 for uptrend, 0 for sideward trend, -1 for downtrend}
+}
+\author{Andreas Voellenklee}
+\references{
+}
+\note{}
+\seealso{\code{\link{TrendDetectionSMA}}}
+\examples{
+\dontrun{
+  getSymbols("YHOO", adjust=TRUE)
+  # create chart of YAHOO
+  chartSeries(YHOO, subset="last 1 year", TA=NULL)
+  # visualize the result of trend detection in a indicator box
+  addTA(TrendDetectionChannel(YHOO)[,4])
+  # filter YHOO for Hammer Candlestick Patterns that occur in downtrends
+  eval(CSPHammer(TS) & TrendDetectionChannel(TS)[,"DownTrend"])
+}
+}
+\keyword{}

Added: pkg/man/TrendDetectionSMA.Rd
===================================================================
--- pkg/man/TrendDetectionSMA.Rd	                        (rev 0)
+++ pkg/man/TrendDetectionSMA.Rd	2012-04-09 15:41:06 UTC (rev 12)
@@ -0,0 +1,38 @@
+\name{TrendDetectionSMA}
+\alias{TrendDetectionSMA}
+\title{Current Trend Detection using Simple Moving Average}
+\description{Uses the SMA of \code{n} periods to determine whether a price series is in uptrend or downtrend}
+\usage{TrendDetectionSMA(TS, n=20)}
+\arguments{
+  \item{TS}{xts Time Series containing OHLC prices}
+  \item{n}{number of periods for the SMA to average over}
+}
+\details{
+This function assumes that a price series is in uptrend (downtrend) when a period's price closes above (below) the simple moving average of \code{n} periods.
+}
+\value{
+  A xts object containing the columns:
+  \item{UpTrend}{TRUE if uptrend detected (Close > SMA(n))}
+  \item{NoTrend}{TRUE if sideward trend detected (Close == SMA(n))}
+  \item{DownTrend}{TRUE if downtrend detected (Close < SMA(n))}
+  \item{Trend}{+1 for uptrend, 0 for sideward trend, -1 for downtrend}
+}
+\author{Andreas Voellenklee}
+\references{
+}
+\note{}
+\seealso{
+\code{\link{TrendDetectionChannel}}
+}
+\examples{
+\dontrun{
+  getSymbols("YHOO", adjust=TRUE)
+  # create chart of YAHOO
+  chartSeries(YHOO, subset="last 1 year", TA=NULL)
+  # visualize the result of trend detection in a indicator box
+  addTA(TrendDetectionSMA(YHOO)[,4])
+  # filter YHOO for Hammer Candlestick Patterns that occur in downtrends
+  eval(CSPHammer(TS) & TrendDetectionSMA(TS)[,"DownTrend"])
+}
+}
+\keyword{}



More information about the Candlesticks-commits mailing list