Skip to content

Testing technical indicators

8 messages · BBands, Whit Armstrong, Dirk Eddelbuettel +2 more

#
I too have been down the backtesting road in R.  I would have
participated more in the discussion, but the CFA exam is this Saturday
(level 3 this time).  John, I can see you have been down that road too,
so you know how much time is involved in preparation.
We have a package which allows users to write rules in R which are
evaluated by C++ routines via callbacks to R via eval.  It's messy messy
messy.  This package worked fine on daily data.  However, when we
started testing with 10 minute bars or 1 minute bars it became
impossibly slow.

I have never used Python, but I am curious.  Do you find that it works
well with higher frequency data.  Do you use it with intraday data at
all?

Thanks,
Whit

Here is an example script for a simple rsi divergence system.  The big
advantage of writing the rules in R is that you can make calls to
browser in your scripts which I find helps a lot in debugging.

library(tslib)
library(pl)

var.grid <-
expand.grid(rnk.win=seq(10,20,5),rsi.win=seq(14,50,20),p.target=seq(0.5,
2.5,0.5),p.stop=seq(0.5,2.5,0.5))
#var.grid <-
expand.grid(rnk.win=seq(10,40,5),rsi.win=14,p.target=1.0,p.stop=1.0)

rsi.system <- function(mkt) {

    # for position size
    notional <- 100*10^6
    risk.pct <- 0.001
    risk <- notional*risk.pct
    mkt.atr <- moving.avg(true.range(ps(mkt)),60)
    trade.size <- round(risk/(mkt.atr*com.factor(mkt)),0)
    #trade.size <- 100

    mkt.ps <- ps(mkt)
    mkt.ds.close <- ds(mkt)[,"close"]

    rsi.buff=2
    mkt.rsi <- rsi(mkt.ps,rsi.win)
    mkt.rsi.rnk <- rnk(mkt.rsi,rnk.win)

    new.high <- rnk(mkt.ps[,"high"],rnk.win)<2
    new.low <- rnk(mkt.ps[,"low"],rnk.win)>(rnk.win-1)

    # new high & not an rsi new high
    sell.sig <- new.high & mkt.rsi.rnk > (1 + rsi.buff)

    buy.sig <- new.low & mkt.rsi.rnk < (rnk.win - 1 - rsi.buff)

    buy.entry <- function() {
        #if(is.na(pl.pos()) || is.na(pl.value(sys.buy)))
        #    browser()

        if(pl.pos() <= 0 && pl.value(buy.sig)) {
            pl.new.trade(trade.size)
 
set.target(pl.value(mkt.ds.close)+pl.value(mkt.atr)*p.target)
            set.stop(pl.value(mkt.ds.close)-pl.value(mkt.atr)*p.stop)
        }
    }
   sell.entry <- function() {
        #if(is.na(pl.pos()) || is.na(pl.value(sys.buy)))
        #   browser()

        if(pl.pos() >= 0 && pl.value(sell.sig)) {
            pl.new.trade(-trade.size)
 
set.target(pl.value(mkt.ds.close)-pl.value(mkt.atr)*p.target)
            set.stop(pl.value(mkt.ds.close)+pl.value(mkt.atr)*p.stop)
        }
    }

    buy.rule <- list(entry=buy.entry)
    sell.rule <- list(entry=sell.entry)

    ans <- list(buy.rule=buy.rule,sell.rule=sell.rule)
    class(ans) <- "pl.system"
    ans
}

mkts <- scan("/home/whit/.std.mkt.list",what="")[-c(1:9)]

mkt.list <- lapply(mkts,lim.com)

sys.reports <- do.system(mkt.list,rsi.system,100,grid=var.grid)
#
On 2 June 2006 at 07:26, BBands wrote:
| I'll have a look for a copy--maybe Dirk still has one, but Crusher was not
| ready for prime time. At version 0.0.4 it had not been seriously debugged
| and I am sure that there were calc errors that had not been fixed yet. I had
| planned for basic usability at 0.1.0, but we didn't get that far. If I can't
| find it, I'll post some Python TA indicator code.

But Crusher comes from the pre-Rmetrics days. I'd suspect that you find as
much if not more code in the TA examples in Rmetrics.  Most of what I
contributed to Crusher was the Bollingerbands plotting function (now also on
the excellent R Graph gallery) I wrote to convince John to drop the fugly
gnuplot :)
 
| For me it goes like this. I use R as a calculation engine rather than a
| programming environment. So any time I have a calc that R seems like a
| natural resource for, I use R. That include regressions, t tests, Chi
| Square, etc... I've found no need to go to C or C++ as with a decent
| computer and a modicum of memory R and Python are fast enough for my needs.

I think Gabor once noted on one of the lists that he found that he, over
time, converged to R for the other tasks previously done outside R.  That's
true for me as well. My very first exposure to S-Plus involved piping to it
from Perl which talked to DBs etc pp.  These days I rarely have to go outside
of R --- other than to compiled code for performance reasons, and even then I
try to glue that code back to R in order to control its rich environment.

Dirk
#
On 6/2/06, Dirk Eddelbuettel <edd at debian.org> wrote:
Not sure what 'fugly' means, but I am sure that I love gnuplot. ;-)

      jab
#
On 6/2/06, Dirk Eddelbuettel <edd at debian.org> wrote:
Not true. Rmetrics is many things, but a TA package it is not, nor do
I think it was intended to be one. There are several TA indicators
included in fMultivar, so perhaps it is a beginning, but if it is a
beginning, it has a long ways to go before anything serious could be
done with it TA-wise. Perhaps I have missed something?

(Nothing in the above may be construed in any way as a criticism of
Mr. Wuertz's work.)

    jab
#
On 6/2/06, Jeff Ryan <jeff.a.ryan at gmail.com> wrote:
What an interesting idea! Please keep me posted if you undertake this project.

       jab
#
There is a tutorial on interfacing to C here:

http://genetics.agrsci.dk/~sorenh/misc/Rdocs/Load-C-from-R.pdf

Info on creating packages is in the R Writing Extensions manual
and some people have written tutorials found via googling:
creating R package

Also see:
?package.skeleton

and one can download the source of some packages and look at them.
On 6/2/06, Jeff Ryan <jeff.a.ryan at gmail.com> wrote: