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).