« Fitting mixed models to categorical data in R | Main | A tutorial on Support Vector Regression »

May 14, 2009

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

David (and Bryan),

This is awesome! Thanks for sharing the code!

One (trivial?) comment:
I was a bit confused about where the INTC and IEF data came from until I got to the end of the post. I would have expected to see a "load('Vhayu.Rdata')" command before the chartSeries call.

great post, but is there not a lagging issue here? don't you need to lag the signal by a day to make sure you are not capturing the return on the day that the macd signals (i.e. removing a current day look forward?)

there's also an error in the current code.

The line

return (long*(s>0) + benchmark*(s<=0))

should be

return(rbind(long[s > 0],benchmark[s <= 0]))

@Brian, I *think* the code is correct. (Going by memory here -- I haven't rerun the code lately.) It's unusual R syntax, but it's returning either the value "long" or "benchmark" according to the value of "s" -- treated as numeric, (s>0) is a sequence of 1's and 0's.

I completely understand what you were getting at, and I remember running this example when you first posted it.

I needed to make the indicated modification to get it to run on now-current versions of everything. Hopefully it will help someone searching old posts.

Regards, Brian

can this be run on a windows machine

Hello and thank you for this post. I have the following problem maybe you could help me out with it?

1. I get this error which seems to be telling me that the "simpleRule" which defines the signal and orders it using "z" needs an appropriate time-based object. SEE BELOW

#### CODE HERE
R <- simpleRule(z$INTC, fast=12, slow=26, signal=9, long=Ra, benchmark=Rb)
#### ERROR RETURNED
Error in xts(position, order.by = index(z)) :
order.by requires an appropriate time-based object

I have no idea why I'm getting this error. If I check the str(z) it is an xts object.

Thank you,
Douglas

It looks as if can skip the s <- xts(position,order.by = index(z))
I made it a comment by adding # before it.
Then when applying the rule I past the "position" into the rule! I maybe missing something but the position is and xts object and needs not be converted?
return(long*(position>0)+benchmark*(position<=0))
Below is the code and how I changed it. Any feedback would be great! Also, I think you have a lag issue? While I'm asking questions, what would be the quickest way to subset the data to test different time frames?
Thank you,
Douglas

simpleRule <- function(z, fast=12, slow=26, signal=9, long, benchmark)
{
x <- MACD(z, nFast=fast, nSlow=slow, nSig=signal, maType="EMA")
position <- sign(x[,1]-x[,-2])
#s <- xts(position,order.by = index(z))
return(long*(position>0)+benchmark*(position<=0))
}

#####USE THE RULE NOW
R <- simpleRule(z$INTC, fast=12, slow=26, signal=9, long=Ra, benchmark=Rb)
chart.CumReturns(R, main="Nfast=12 NSlow=26 NSig=9")

The comments to this entry are closed.

Search Revolutions Blog




Got comments or suggestions for the blog editor?
Email David Smith.
Follow revodavid on Twitter Follow David on Twitter: @revodavid
Get this blog via email with Blogtrottr