[Blotter-commits] r1222 - pkg/quantstrat/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Oct 15 13:25:23 CEST 2012
Author: bodanker
Date: 2012-10-15 13:25:23 +0200 (Mon, 15 Oct 2012)
New Revision: 1222
Modified:
pkg/quantstrat/R/applyStrategy.rebalancing.R
Log:
- fix 'strategy -> s' typos, remove tabs
- move code to determine rebalancing periods inside path-independent loop
- refactor pindex creation
- combine sret$rules$pathdep using rbind instead of c in endpoints loop
- fix 'mktdata -> md_subset' typo in endpoints loop
Modified: pkg/quantstrat/R/applyStrategy.rebalancing.R
===================================================================
--- pkg/quantstrat/R/applyStrategy.rebalancing.R 2012-10-13 16:14:31 UTC (rev 1221)
+++ pkg/quantstrat/R/applyStrategy.rebalancing.R 2012-10-15 11:25:23 UTC (rev 1222)
@@ -28,10 +28,10 @@
ret<-list()
- if (!is.strategy(strategy)) {
- s<-try(getStrategy(strategy))
- if(inherits(strategy,"try-error"))
- stop ("You must supply an object of type 'strategy'.")
+ if (!is.strategy(strategy)) {
+ s<-try(getStrategy(strategy))
+ if(inherits(s,"try-error"))
+ stop ("You must supply an object of type 'strategy'.")
} else {
s <- strategy
}
@@ -69,10 +69,10 @@
for (portfolio in portfolios) {
- # initStrategy
+ # initStrategy
if(isTRUE(initStrat)) initStrategy(strategy=s, portfolio, symbols, ...=...)
- ret[[portfolio]]<-list() # this is slot [[i]] which we will use later
+ ret[[portfolio]]<-list() # this is slot [[i]] which we will use later
pobj<-getPortfolio(portfolio)
symbols<-names(pobj$symbols)
@@ -80,30 +80,24 @@
#should be able to use this directly
#assign(st,paste(s$name,'mktdata',sep='.'),pos=.strategy)
+ # initialize rebalancing period variables
if(length(periods)>1){ warning('no guarantee multiple-periodicity rebalancing will work just yet, patches welcome.') }
st$periods<-periods
- # get the rebalancing periods list for this portfolio
plist<-list()
- for( period in periods ) {
- from<-as.POSIXlt(index(pobj$summary)[1],tz=indexTZ(pobj$summary))
- # this sequence should work pretty generically
- plist[[period]]<-seq(from=from, to=as.POSIXlt(Sys.Date()), by = period)
- #TODO FIXME sort out a more robust 'to' parameter for this
- }
- st$plist<-plist
-
- if (length(plist) >1) pindex<-lapply(plist,c)
- else pindex<-plist[[1]]
-
- pindex<-xts(1:length(pindex),order.by=pindex)
- pindex<-index(pindex)
- st$rebalance_index<-pindex
-
+
#first do the path-independent stuff for indicators and signals
for (symbol in symbols){
+
sret<-list()
if(isTRUE(load.mktdata)) mktdata <- get(symbol)
+ if(isTRUE(load.mktdata) || length(plist)==0) {
+ # get the rebalancing periods list for this portfolio
+ for( period in periods ) {
+ plist[[period]] <- c(as.POSIXct(index(mktdata)[endpoints(mktdata, period)]), plist[[period]])
+ }
+ }
+
#loop over indicators
sret$indicators <- applyIndicators(strategy=s , mktdata=mktdata , parameters=parameters, ... )
#this should be taken care of by the mktdata<<-mktdata line in the apply* fn
@@ -125,7 +119,13 @@
#TODO capture rebalance periods here?
} # end path-independent loop over indicators and signals by symbol
+
+ st$plist<-plist
+ # combine plist into one sorted index
+ pindex <- sort(do.call(c, c(plist, use.names=FALSE)))
+ st$rebalance_index<-pindex
+
#now we need to do the endpoints loop.
for(i in 2:length(pindex)){
#the proper endpoints for each symbol will vary, so we need to get them separately, and subset each one
@@ -135,9 +135,9 @@
mktdata<-get(symbol,pos=st)
#now subset
md_subset<-mktdata[as.POSIXct(index(mktdata))>pindex[i-1]&as.POSIXct(index(mktdata))<=pindex[i]]
- if(nrow(mktdata)<1) next()
+ if(nrow(md_subset)<1) next()
#applyRules to this subset for this instrument
- sret$rules$pathdep<-c(sret$rules$pathdep,
+ sret$rules$pathdep<-rbind(sret$rules$pathdep,
applyRules(portfolio=portfolio, symbol=symbol, strategy=s, mktdata=md_subset, Dates=NULL, indicators=sret$indicators, signals=sret$signals, parameters=parameters, ..., path.dep=TRUE))
ret[[portfolio]][[symbol]]<-sret
@@ -157,7 +157,7 @@
}
# updateStrat
- if(isTRUE(updateStrat)) updateStrategy(strategy, portfolio, Symbols=symbols, ...=...)
+ if(isTRUE(updateStrat)) updateStrategy(s, portfolio, Symbols=symbols, ...=...)
}
More information about the Blotter-commits
mailing list