[R-sig-Finance] Fwd: Testing technical indicators

Whit Armstrong whit at twinfieldscapital.com
Fri Jun 2 17:39:33 CEST 2006


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.

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



More information about the R-SIG-Finance mailing list