[R-SIG-Finance] quantmod tradeModel function

John Poirier poirier at jhmi.edu
Sat Jan 17 05:00:41 CET 2009


I'm starting to familiarize myself with using the quantmod package,  
coming from a limited background in differential gene expression  
analysis. I thought I would try the simplest code I could think of to  
understand how a typical workflow might work using an example from the  
quantmod manual. I am having some trouble getting this code to work. I  
think the issue might be with not understanding the requirements of  
tradeModel very well. It's not clear to me in the manual how this  
should work. I always learn best by example, if possible.

Ultimately, I want to learn to use these tools because they are also  
applicable to my basic research (we use machine learning code for  
disease classification) and also because I would like to be able to  
manage my personal portfolio using more informed technical indicators  
and evaluate different trading strategies using these models and  
performanceanalytics. Also it's fun. So if you can share code, that  
would be extremely enlightening for me.

Cheers,

John

rm(list=ls())

library(quantmod)

getSymbols('QQQQ',src='yahoo')
q.model = specifyModel(Next(OpCl(QQQQ)) ~ Lag(OpHi(QQQQ),0:3))
test<- 
buildModel 
(q.model,method='lm',training.per=c('2007-01-03','2008-01-03'))
tradeModel(test)
traceback()
sessionInfo()

Results:

 > rm(list=ls())
 >
 > library(quantmod)
 >
 > getSymbols('QQQQ',src='yahoo')
[1] "QQQQ"
 > q.model = specifyModel(Next(OpCl(QQQQ)) ~ Lag(OpHi(QQQQ),0:3))
 > test<- 
buildModel 
(q.model,method='lm',training.per=c('2007-01-03','2008-01-03'))
 > tradeModel(test)
Error in endpoints(xx, on = on.opts[[period]], ...) :
   unused argument(s) (indexAt = "endof")
 > traceback()
7: endpoints(xx, on = on.opts[[period]], ...)
6: periodReturn(x, "monthly", type = type, indexAt = "endof")
5: merge.zoo(..., all = all, fill = fill, suffixes = suffixes,  
retclass = "zoo")
4: cbind.zoo(periodReturn(x, "daily", type = type, leading),  
periodReturn(x,
        "weekly", type = type), periodReturn(x, "monthly", type = type,
        indexAt = "endof"), periodReturn(x, "quarterly", type = type,
        indexAt = "endof"), periodReturn(x, "yearly", type = type))
3: allReturns(model.cumret)
2: modelReturn(quantmodResults, trade.dates = trade.dates, leverage =  
leverage,
        ret.type = ret.type)
1: tradeModel(test)
 > sessionInfo()
R version 2.7.0 (2008-04-22)
i386-apple-darwin8.10.1

locale:
en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] nnet_7.2-44    TTR_0.14-0     quantmod_0.3-7 Defaults_1.1-1  
xts_0.6-4
[6] zoo_1.5-4

loaded via a namespace (and not attached):
[1] grid_2.7.0      lattice_0.17-15 tools_2.7.0
 >



More information about the R-SIG-Finance mailing list