Trying to add custom indicators to chart.Posn
On Sat, Sep 28, 2013 at 8:49 PM, Ilya Kipnis <ilya.kipnis at gmail.com> wrote:
So my question is this: I'm running a trading strategy in quantstrat with
custom indicators (replicating the ichimoku system, for those that have
heard of it), and I'd like to get a visual bead on the system by overlaying
my custom indicators directly on top of the position chart.
My data is QQQ from the start of 2007 to the end of August of 2013.
Here's my indicator code:
minAndMax <- function(twoNums) {
ret <- c(min(twoNums), max(twoNums))
return(ret)
}
No need to re-write the range function...
# for those that find the terminology weird, here's a basic rundown
# tenkanSen is similar to a fast SMA, except instead,
# it's the mean of the highest high and lowest low of the past nFast periods
# kijunSen is the same as above, except at nMed frequency
# senkou A is the average of the two above, projected nMed periods ahead
(lag nMed)
# senkou B is similar to the first two, except at an nSlow frequency,
# also projected nMed periods ahead
# Chicou span in the original system is the current close projected nMed
terms into the past
# however, with computers, I instead opted to move the past nMed terms
forward.
ichimoku <- function(HLC, nFast=9, nMed=26, nSlow=52) {
tenkanSen <- (runMax(Hi(HLC), nFast) + runMin(Lo(HLC), nFast))/2
kijunSen <- (runMax(Hi(HLC), nMed) + runMin(Lo(HLC), nMed))/2
senkouA <- lag(((tenkanSen+kijunSen)/2), nMed)
senkouB <- lag((runMax(Hi(HLC), nSlow) + runMin(Lo(HLC), nSlow))/2, nMed)
modChicouClose <- lag(Cl(HLC), nMed)
modChicouHi <- lag(Hi(HLC),nMed)
modChicouLo <- lag(Lo(HLC),nMed)
kumoMinMax <- apply(cbind(senkouA,senkouB),1,minAndMax)
indicators <- cbind(tenkanSen, kijunSen, senkouA, senkouB,
modChicouClose,
modChicouHi, modChicouLo, t(kumoMinMax))
colnames(indicators) <-
c("tenkanSen","kijunSen","senkouA","senkouB","modChicouClose",
"modChicouHi","modChicouLo","kumoMin","kumoMax")
reclass(indicators, HLC)
}
However, when I try to do this: (one of my instruments is QQQ), I get the
following error:
plot(add_TA(ichimoku(QQQ)))
Error in `[.xts`(x, is.finite(x <- as.numeric(x))) :
'i' or 'j' out of range
It looks like add_TA doesn't like multi-column objects. That's easily
avoided by calling it for each column in a loop:
ind <- ichimoku(QQQ))
chart.Posn("portfolioName", Symbol="QQQ")
for(i in 1:9) {
plot(add_TA(ind[,i],on=1)
}
And furthermore, the chart.Posn plot creates a new window for the new indicator, which sort of defeats the purpose of the entire point of plotting, as I took this exercise to try and replicate a "flesh-and-blood" technical trading strategy in quantstrat.
Use the "on=" argument, as I did above.
So, any help would be much appreciated. Thanks so much. Sincerely, Ilya Kipnis
Best, -- Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com