1

I would like to use R in combination with quantmod to generate trading signals based on candlestick chart patterns. My plan is to write a user defined function which calculates a signal per time period based on OHLC data. What I have so far is:

getSymbols("IBM", src="google")
candle = function(data, ...)
{
    if (abs(Op(data)-Cl(data)) < (Op(data)*0.0005))
        doji = "doji"
    else
        doji = "non-doji"   
    return(doji)
}
apply.daily(IBM, FUN=candle)

This functions returns a value for every day in the xts object. Now I would like to add some calculations in the function candle that are based on previous or next values. How can I access the adjacent values?

The object data I have in the function candle seems to be only one row (at least that's what I get when I call nrow). I tried using lag but I always get NA (presumably because my xts object is only one row long).

Any help would be appreciated. Also I'd be happy about pointers where to learn more about quantmod. There seems to be some kind of workflow that is hinted at on the website but no real documentation that I could find.

EDIT:

I'd like to clarify my actual goal:

I will take fine grained OHLC data and aggregate it to some time period (e.g. hourly). So every hour represents one candle-stick in a candle stick chart.

Now I am going through these data-points looking for certain patterns (e.g. if one candle stick with properties x is followed by two more of the same and then one with properties y).

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
styx
  • 421
  • 8
  • 15
  • Try this to get an idea of how `apply.daily` and family are meant to be used: `example(apply.daily)` – GSee Mar 30 '13 at 02:59

1 Answers1

1

apply.daily is meant to be used on intraday data. It splits the data by day and applies a function to each day. Since you have daily data, there is only one row of data in each day.

For what you've shown so far, there's no need to use apply.daily.

data(sample_matrix)
dat <- as.xts(sample_matrix)

# Create an object with the same index as dat, and fill with "non-doji"
doji <- xts(rep("non-doji", nrow(dat)), index(dat))
# now, anywhere your condition is true, replace "non-doji" with "doji"
# This takes advantage of the vectorized nature of R
doji[abs(Op(dat) - Cl(dat)) < (Op(dat) * 0.0005)] <- "doji"
tail(doji)
#           [,1]      
#2007-06-25 "non-doji"
#2007-06-26 "non-doji"
#2007-06-27 "doji"    
#2007-06-28 "non-doji"
#2007-06-29 "non-doji"
#2007-06-30 "non-doji"

Although, I would probably just add another column called "doji" to dat and give it a value of 1 where when your condition is met, or zero otherwise. (note that all data an xts object must be of the same type).

dat$doji <- 0 # create a column called "doji" and fill it with zeros
# now set "doji" to 1 anywhere your condition is true
dat$doji[abs(Op(dat) - Cl(dat)) < (Op(dat) * 0.0005)] <- 1

R> tail(dat)
               Open     High      Low    Close doji
2007-06-25 47.20471 47.42772 47.13405 47.42772    0
2007-06-26 47.44300 47.61611 47.44300 47.61611    0
2007-06-27 47.62323 47.71673 47.60015 47.62769    1
2007-06-28 47.67604 47.70460 47.57241 47.60716    0
2007-06-29 47.63629 47.77563 47.61733 47.66471    0
2007-06-30 47.67468 47.94127 47.67468 47.76719    0
GSee
  • 48,880
  • 13
  • 125
  • 145
  • I realize this doesn't show you how to access the next or previous value within your candle function, but you haven't said what you want to do, so it's hard to give an answer. Most likely the answer will not involve that candle function though. – GSee Mar 30 '13 at 03:11
  • It appears as though i could use your second approach with something like `dat$doji[abs(Next(Op(dat)) - Next(Cl(dat))) < (Next(Op(dat))*0.0005)] = 1` to reference adjacent values. I'm still pretty lost on how to approach my problem in general though. – styx Mar 30 '13 at 17:27
  • @styx, If you gave a concrete example, I'm sure someone could show you how to do it. Otherwise, I think someone may have already done the work for you; see if this package is useful: https://r-forge.r-project.org/R/?group_id=1317 – GSee Mar 30 '13 at 17:37
  • I will try and give a more concrete example: Given an xts of OHLC data i want to test a strategy. The strategy says to buy a security when a certain pattern arises and sell it when it goes up or down x%. The pattern is calculated only from the Open, High, Low and Close data of the last 5 periods. Lets say Open < Close on 5 consecutive days. The actual pattern is more complex but this seems to be the easy part. What i (as a R-newbie) don't understand is how i can approach this in general. – styx Mar 30 '13 at 19:36