Skip to content
Prev 7263 / 15274 Next

Blotter example by kafka from R-bloggers

Hi

One thing that I want to understand is the effect of stop-loss activity 
on results and employing more complex rules.  The two examples I am 
looking at have fairly simple rules like:

# three days higher close, high and open than on previous day

#one day before
lag1<-lag((SPY),1)

#two days defore
lag2<-lag((SPY),2)

signal<-ifelse( (Cl(lag2)>Cl(lag1) & Cl(lag1)>Cl(SPY))&
             (Hi(lag2)>Hi(lag1) & Hi(lag1)>Hi(SPY)) &
             (Op(lag2)>Op(lag1) & Op(lag1)>Op(SPY)),
             1,0
)

and

# if today's low is higher than yesterday's close 1, else 0

signal<-ifelse(Lo(SPY)>Cl(tmp),1,0)
signal[1]<-0

First on more complex rules: I have tried looking at vector operations 
but trying to write a rule for spreads like this:

[rule for opening]
if !not_open(yesterday(last_spread > 2 * standard deviation) and 
today(last_spread < 2 * standard deviation)) -> open short spread )(and 
vica versa)

[rule for stop loss]
if open(last_spread > opening_spread * 1.05 [stop loss]) -> close short 
(and vica versa)

[rule for closing]

if open(last_spread < moving average) -> close short (and vica versa)

defeated me and I ending up writing some code like this (notice that I 
haven't got the stop loss rule in it):

i <- 1
long = 0
short = 0
for (i in seq(from=1,to=length(spread.data$Close),by=1)) {
     # lets get the data in more usable names
     close.today <- spread.data[i,1]
     close.yesterday <- spread.data[i-1,1]
     # just to deal with the first period when there is no yesterday
     if(i == 1) close.yesterday <- close.today
     mean.today <- spread.data[i,2]
     mean.yesterday <- spread.data[i-1,2]
     # just to deal with the first period when there is no yesterday
     if(i == 1) mean.yesterday <- mean.today
     upper.boundary.today <- spread.data[i,3]
     upper.boundary.yesterday <- spread.data[i-1,3]
     # just to deal with the first period when there is no yesterday
     if(i == 1) upper.boundary.yesterday <- upper.boundary.today
     lower.boundary.today <- spread.data[i,4]
     lower.boundary.yesterday <- spread.data[i-1,4]
     # just to deal with the first period when there is no yesterday
     if(i == 1) lower.boundary.yesterday <- lower.boundary.today
     # lets try and find if we have a long signal
         #print(c(i, 
-close.yesterday,lower.boundary.yesterday,close.today,lower.boundary.today))
         ################## RULES FROM HERE ##################
         # spread$Close - spread$Close.1
         ####### FIRST FOR A LONG #####
         ####### first find lower boundary crossings #####
         if(long == 0) position = 0
         if(close.yesterday <= lower.boundary.yesterday && close.today > 
lower.boundary.today) long = 1
         ####### find mean crossings #####
     if (long == 1 && close.today > mean.today) long = 0
     sigup[i] <- long
     #print(c(i, long, 
close.yesterday,lower.boundary.yesterday,close.today,lower.boundary.today))
     ####### THEN FOR A SHORT #####
     ####### first find upper boundary crossings #####
     if(close.yesterday >= upper.boundary.yesterday && close.today < 
upper.boundary.today) short = -1
     ####### find mean crossings #####
     if (short == -1 && close.today < mean.today) short = 0
     sigdn[i] <- short

}

So I put it all in a loop and carry forward my positions/triggers from 
one day to the next which is sort of the way I would normally program.

Can you write rules such as I am trying to using vector operations and 
does blotter lend itself to this?

Second: on  more general note this whole question of stop loss is very 
significant to results.  I find that most back testing is based upon not 
adopting such a policy, but prudence would almost always insist on one 
doing so.   If you have no real option but to adopt a stop loss policy 
then the most important question is what is the correct level of 
protection.  I get very annoyed when my strategy works without a stop 
loss and then the first time I take a position I get closed out by my 
stop loss and lose money and then the next day or the day after I find 
the figures put me back in the black.   Anyway, I guess this is just an 
iterative process using a binary search but, again, are there any useful 
ideas about how one can go about this sort of optimisation re-using 
existing packages/code?

Stephen Choularton Ph.D., FIoD
On 29/12/2010 7:18 AM, Brian G. Peterson wrote: