[Blotter-commits] r222 - in pkg: . quantstrat quantstrat/R quantstrat/demo quantstrat/inst quantstrat/man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Feb 2 04:25:16 CET 2010
Author: peter_carl
Date: 2010-02-02 04:25:09 +0100 (Tue, 02 Feb 2010)
New Revision: 222
Added:
pkg/quantstrat/
pkg/quantstrat/.Rbuildignore
pkg/quantstrat/.project
pkg/quantstrat/DESCRIPTION
pkg/quantstrat/NAMESPACE
pkg/quantstrat/R/
pkg/quantstrat/R/indicators.R
pkg/quantstrat/R/strategy.R
pkg/quantstrat/demo/
pkg/quantstrat/demo/00Index
pkg/quantstrat/demo/simplestrat.R
pkg/quantstrat/inst/
pkg/quantstrat/inst/doc/
pkg/quantstrat/man/
pkg/quantstrat/man/.onLoad.Rd
pkg/quantstrat/man/add.indicator.Rd
pkg/quantstrat/man/applyIndicators.Rd
pkg/quantstrat/man/applyStrategy.Rd
pkg/quantstrat/man/getStrategy.Rd
pkg/quantstrat/man/is.strategy.Rd
pkg/quantstrat/man/strategy.Rd
Log:
- first draft of strategy framework
Added: pkg/quantstrat/.Rbuildignore
===================================================================
--- pkg/quantstrat/.Rbuildignore (rev 0)
+++ pkg/quantstrat/.Rbuildignore 2010-02-02 03:25:09 UTC (rev 222)
@@ -0,0 +1,3 @@
+sandbox
+generatechangelog.sh
+ChangeLog.1.0.0
Added: pkg/quantstrat/.project
===================================================================
--- pkg/quantstrat/.project (rev 0)
+++ pkg/quantstrat/.project 2010-02-02 03:25:09 UTC (rev 222)
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>quantstrat</name>
+ <comment></comment>
+ <projects>
+ <project>blotter</project>
+ <project>FinancialInstrument</project>
+ <project>PerformanceAnalytics</project>
+ <project>PortfolioAnalytics</project>
+ <project>quantmod</project>
+ <project>xts</project>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>de.walware.statet.r.builders.RSupport</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>de.walware.statet.base.StatetNature</nature>
+ <nature>de.walware.statet.r.RNature</nature>
+ </natures>
+</projectDescription>
Added: pkg/quantstrat/DESCRIPTION
===================================================================
--- pkg/quantstrat/DESCRIPTION (rev 0)
+++ pkg/quantstrat/DESCRIPTION 2010-02-02 03:25:09 UTC (rev 222)
@@ -0,0 +1,13 @@
+Package: quantstrat
+Type: Package
+Title: Quantitative Strategy Model Framework
+Version: 0.0.1
+Date: 2010-01-25
+Author: Peter Carl, Dirk Eddelbuettel, Brian G. Peterson, Jeffrey A. Ryan, Joshua Ulrich
+Depends: xts(>= 0.7-0),TTR(>= 0.2),blotter(>= 0.4), FinancialInstrument, quantmod (>= 0.3-14)
+Suggests: PerformanceAnalytics,PortfolioAnalytics
+Maintainer: Jeffrey A. Ryan <jeff.a.ryan at gmail.com>
+Description: Specify, build, and backtest quantitative financial trading strategies
+LazyLoad: yes
+License: GPL-3
+Collate: 'indicators.R' 'strategy.R'
Added: pkg/quantstrat/NAMESPACE
===================================================================
--- pkg/quantstrat/NAMESPACE (rev 0)
+++ pkg/quantstrat/NAMESPACE 2010-02-02 03:25:09 UTC (rev 222)
@@ -0,0 +1,6 @@
+export(add.indicator)
+export(strategy)
+export(applyStrategy)
+export(applyIndicators)
+export(is.strategy)
+export(getStrategy)
Added: pkg/quantstrat/R/indicators.R
===================================================================
--- pkg/quantstrat/R/indicators.R (rev 0)
+++ pkg/quantstrat/R/indicators.R 2010-02-02 03:25:09 UTC (rev 222)
@@ -0,0 +1,36 @@
+
+#' add an indicator to a strategy
+#' @param strategy an object of type 'strategy' to add the indicator to
+#' @param name name of the indicator, must correspond to an R function
+#' @param arguments default arguments to be passed to an indicator function when executed
+#' @param ... any other passthru parameters
+#' @param indexnum if you are updating a specific indicator, the index number in the $indicators list to update
+#' @param store TRUE/FALSE whether to store the strategy in the .strategy environment, or return it. default FALSE
+#' @export
+add.indicator <- function(strategy, name, arguments, ..., indexnum=NULL,store=FALSE) {
+ if(!is.strategy(strategy)) stop("You must pass in a strategy object to manipulate")
+ tmp_indicator<-list()
+ tmp_indicator$name<-name
+ if (!is.list(arguments)) stop("arguments must be passed as a named list")
+ tmp_indicator$arguments<-arguments
+ if(!hasArg(indexnum) | (hasArg(indexnum) & is.null(indexnum))) indexnum = length(strategy$indicators)+1
+ tmp_indicator$call<-match.call()
+ strategy$indicators[[indexnum]]<-tmp_indicator
+
+ if (store) assign(strategy$name,strategy,envir=as.environment(.strategy))
+ else return(strategy)
+}
+
+
+###############################################################################
+# R (http://r-project.org/) Quantitative Strategy Model Framework
+#
+# Copyright (c) 2009-2010
+# Peter Carl, Dirk Eddelbuettel, Brian G. Peterson, Jeffrey Ryan, and Joshua Ulrich
+#
+# This library is distributed under the terms of the GNU Public License (GPL)
+# for full details see the file COPYING
+#
+# $Id: strategy.R 217 2010-01-29 18:10:53Z braverock $
+#
+###############################################################################
Added: pkg/quantstrat/R/strategy.R
===================================================================
--- pkg/quantstrat/R/strategy.R (rev 0)
+++ pkg/quantstrat/R/strategy.R 2010-02-02 03:25:09 UTC (rev 222)
@@ -0,0 +1,184 @@
+#' constructor for objects of type 'strategy'
+#' @param name character string naming the strategy
+#' @param ... any other passthru parameters
+#' @param assets optional list of assets to apply the strategy to
+#' @param constraints optional portfolio constraints object matching assets
+#' @param store TRUE/FALSE whether to store the strategy in the .strategy environment, or return it. default FALSE
+#' @export
+strategy <- function(name, ..., assets=NULL, constraints=NULL ,store=FALSE)
+{ # modeled on GPL R-Forge pkg roi by Stefan Thuessel,Kurt Hornik,David Meyer
+
+ if(!is.null(assets)){
+ if(is.numeric(assets)){
+ if (length(assets) == 1) {
+ nassets=assets
+ #we passed in a number of assets, so we need to create the vector
+ message("assuming equal weighted seed portfolio")
+ assets<-rep(1/nassets,nassets)
+ } else {
+ nassets = length(assets)
+ }
+ # and now we may need to name them
+ if (is.null(names(assets))) {
+ for(i in 1:length(assets)){
+ names(assets)[i]<-paste("Asset",i,sep=".")
+ }
+ }
+ }
+ if(is.character(assets)){
+ nassets=length(assets)
+ assetnames=assets
+ message("assuming equal weighted seed portfolio")
+ assets<-rep(1/nassets,nassets)
+ names(assets)<-assetnames # set names, so that other code can access it,
+ # and doesn't have to know about the character vector
+ }
+ # if assets is a named vector, we'll assume it is current weights
+ }
+
+ ## now structure and return
+ strat<-structure(
+ list(
+ name= name,
+ assets = assets,
+ indicators = list(),
+ signals = list(),
+ rules = list(),
+ constraints = NULL,
+ call = match.call()
+ ),
+ class=c("strategy")
+ )
+ if(store) assign(strat$name,strat,envir=as.environment(.strategy))
+ else return(strat)
+}
+
+#' apply the strategy to arbitrary market data
+#' @param strategy an object of type 'strategy' to add the indicator to
+#' @param mktdata an xts object containing market data. depending on indicators, may need to be in OHLCV or BBO formats
+#' @param ... any other passthru parameters
+#' @export
+applyStrategy <- function(strategy,mktdata, ... ) {
+ #TODO add Date subsetting
+
+ ret<-list()
+
+ if (!is.strategy(strategy)) {
+ strategy<-try(getStrategy(strategy))
+ if(inherits(strategy,"try-error"))
+ stop ("You must supply an object of type 'strategy'.")
+ }
+
+ #loop over indicators
+ ret$indicators <- applyIndicators(strategy,mktdata, ... )
+
+ #loop over signal generators
+
+ #loop over rules
+
+ return(ret)
+}
+
+#' apply the indicators in the strategy to arbitrary market data
+#' @param strategy an object of type 'strategy' to add the indicator to
+#' @param mktdata an xts object containing market data. depending on indicators, may need to be in OHLCV or BBO formats
+#' @param ... any other passthru parameters
+#' @export
+applyIndicators <- function(strategy, mktdata, ...) {
+ #TODO add Date subsetting
+
+ # TODO check for symbol name in mktdata using Josh's code:
+ # symbol <- strsplit(colnames(mktdata)[1],"\\.")[[1]][1]
+
+ if (!is.strategy(strategy)) {
+ strategy<-try(getStrategy(strategy))
+ if(inherits(strategy,"try-error"))
+ stop ("You must supply an object of type 'strategy'.")
+ }
+ ret <- NULL
+ nargs <-list(...)
+ if(length(nargs)==0) nargs=NULL
+ if (length('...')==0 | is.null('...')) {
+ rm('...')
+ nargs=NULL
+ }
+
+ for (indicator in strategy$indicators){
+ #TODO check to see if they've already been calculated
+ if(!is.function(get(indicator$name))) {
+ message(paste("Skipping indicator",indicator$name,"because there is no function by that name to call"))
+ next()
+ }
+
+ # see 'S Programming p. 67 for this matching
+ fun<-match.fun(indicator$name)
+ .formals <- formals(fun)
+ onames <- names(.formals)
+
+ pm <- pmatch(names(indicator$arguments), onames, nomatch = 0L)
+ if (any(pm == 0L))
+ warning(paste("some arguments stored for",indicator$name,"do not match"))
+ names(indicator$arguments[pm > 0L]) <- onames[pm]
+ .formals[pm] <- indicator$arguments[pm > 0L]
+ #now add dots
+ if (length(nargs)) {
+ pm <- pmatch(names(nargs), onames, nomatch = 0L)
+ names(nargs[pm > 0L]) <- onames[pm]
+ .formals[pm] <- nargs[pm > 0L]
+ }
+ .formals$... <- NULL
+
+ formals(fun) <- .formals
+ tmp_val<-do.call(fun,.formals)
+ if(is.null(names(tmp_val)) & ncol(tmp_val)==1) names(tmp_val)<-indicator$name
+ if (nrow(mktdata)==nrow(tmp_val) | length(mktdata)==length(tmp_val)) {
+ # the indicator returned a time series, so we'll name it and cbind it
+ mktdata<-cbind(mktdata,tmp_val)
+ } else {
+ # the indicator returned something else, add it to the ret list
+ if(is.null(ret)) ret<-list()
+ ret[[indicator$name]]<-tmp_val
+ }
+ #print(tmp_val)
+ } #end indicators loop
+ if(is.null(ret)) return(mktdata)
+ else return(list(mktdata=mktdata,indicator_ret=ret))
+}
+
+#' test to see if object is of type 'strategy'
+#' @param x object to be tested
+#' @export
+is.strategy <- function( x ) {
+ inherits( x, "strategy" )
+}
+
+#' retrieve strategy from the container environment
+#' @param x string name of object to be retrieved
+#' @export
+getStrategy <- function(x){
+ tmp_strat<-get(as.character(x),pos=.strategy, inherits=TRUE)
+ if( inherits(tmp_strat,"try-error") | !is.strategy(tmp_strat) ) {
+ warning(paste("Strategy",x," not found, please create it first."))
+ return(FALSE)
+ } else {
+ if(is.strategy(tmp_strat)) return(tmp_strat) else return(NULL)
+ }
+}
+
+.onLoad <- function(lib, pkg) {
+ if(!exists('.strategy'))
+ .strategy <<- new.env()
+}
+
+###############################################################################
+# R (http://r-project.org/) Quantitative Strategy Model Framework
+#
+# Copyright (c) 2009-2010
+# Peter Carl, Dirk Eddelbuettel, Brian G. Peterson, Jeffrey Ryan, and Joshua Ulrich
+#
+# This library is distributed under the terms of the GNU Public License (GPL)
+# for full details see the file COPYING
+#
+# $Id: strategy.R 217 2010-01-29 18:10:53Z braverock $
+#
+###############################################################################
Added: pkg/quantstrat/demo/00Index
===================================================================
--- pkg/quantstrat/demo/00Index (rev 0)
+++ pkg/quantstrat/demo/00Index 2010-02-02 03:25:09 UTC (rev 222)
@@ -0,0 +1 @@
+simplestrat Build a simple strategy using a few indicators
Added: pkg/quantstrat/demo/simplestrat.R
===================================================================
--- pkg/quantstrat/demo/simplestrat.R (rev 0)
+++ pkg/quantstrat/demo/simplestrat.R 2010-02-02 03:25:09 UTC (rev 222)
@@ -0,0 +1,7 @@
+
+s <- strategy("simplestrat")
+s <- add.indicator(strategy = s, name = "EMA", arguments = list(x = quote(Cl(mktdata)), n=10))
+s <- add.indicator(strategy = s, name = "BBands", arguments = list(HLC = quote(HLC(mktdata)), sd = 1.8,maType=quote(SMA)))
+
+getSymbols("IBM")
+applyIndicators(s,IBM)
\ No newline at end of file
Added: pkg/quantstrat/man/.onLoad.Rd
===================================================================
--- pkg/quantstrat/man/.onLoad.Rd (rev 0)
+++ pkg/quantstrat/man/.onLoad.Rd 2010-02-02 03:25:09 UTC (rev 222)
@@ -0,0 +1,4 @@
+\name{.onLoad}
+\alias{.onLoad}
+\title{.onLoad}
+\usage{.onLoad(lib, pkg)}
Added: pkg/quantstrat/man/add.indicator.Rd
===================================================================
--- pkg/quantstrat/man/add.indicator.Rd (rev 0)
+++ pkg/quantstrat/man/add.indicator.Rd 2010-02-02 03:25:09 UTC (rev 222)
@@ -0,0 +1,11 @@
+\name{add.indicator}
+\alias{add.indicator}
+\title{add an indicator to a strategy...}
+\usage{add.indicator(strategy, name, arguments, ..., indexnum, store=FALSE)}
+\description{add an indicator to a strategy}
+\arguments{\item{strategy}{an object of type 'strategy' to add the indicator to}
+\item{name}{name of the indicator, must correspond to an R function}
+\item{arguments}{default arguments to be passed to an indicator function when executed}
+\item{...}{any other passthru parameters}
+\item{indexnum}{if you are updating a specific indicator, the index number in the $indicators list to update}
+\item{store}{TRUE/FALSE whether to store the strategy in the .strategy environment, or return it. default FALSE}}
Added: pkg/quantstrat/man/applyIndicators.Rd
===================================================================
--- pkg/quantstrat/man/applyIndicators.Rd (rev 0)
+++ pkg/quantstrat/man/applyIndicators.Rd 2010-02-02 03:25:09 UTC (rev 222)
@@ -0,0 +1,8 @@
+\name{applyIndicators}
+\alias{applyIndicators}
+\title{apply the indicators in the strategy to arbitrary market data...}
+\usage{applyIndicators(strategy, mktdata, ...)}
+\description{apply the indicators in the strategy to arbitrary market data}
+\arguments{\item{strategy}{an object of type 'strategy' to add the indicator to}
+\item{mktdata}{an xts object containing market data. depending on indicators, may need to be in OHLCV or BBO formats}
+\item{...}{any other passthru parameters}}
Added: pkg/quantstrat/man/applyStrategy.Rd
===================================================================
--- pkg/quantstrat/man/applyStrategy.Rd (rev 0)
+++ pkg/quantstrat/man/applyStrategy.Rd 2010-02-02 03:25:09 UTC (rev 222)
@@ -0,0 +1,8 @@
+\name{applyStrategy}
+\alias{applyStrategy}
+\title{apply the strategy to arbitrary market data...}
+\usage{applyStrategy(strategy, mktdata, ...)}
+\description{apply the strategy to arbitrary market data}
+\arguments{\item{strategy}{an object of type 'strategy' to add the indicator to}
+\item{mktdata}{an xts object containing market data. depending on indicators, may need to be in OHLCV or BBO formats}
+\item{...}{any other passthru parameters}}
Added: pkg/quantstrat/man/getStrategy.Rd
===================================================================
--- pkg/quantstrat/man/getStrategy.Rd (rev 0)
+++ pkg/quantstrat/man/getStrategy.Rd 2010-02-02 03:25:09 UTC (rev 222)
@@ -0,0 +1,6 @@
+\name{getStrategy}
+\alias{getStrategy}
+\title{retrieve strategy from the container environment...}
+\usage{getStrategy(x)}
+\description{retrieve strategy from the container environment}
+\arguments{\item{x}{string name of object to be retrieved}}
Added: pkg/quantstrat/man/is.strategy.Rd
===================================================================
--- pkg/quantstrat/man/is.strategy.Rd (rev 0)
+++ pkg/quantstrat/man/is.strategy.Rd 2010-02-02 03:25:09 UTC (rev 222)
@@ -0,0 +1,6 @@
+\name{is.strategy}
+\alias{is.strategy}
+\title{test to see if object is of type 'strategy'...}
+\usage{is.strategy(x)}
+\description{test to see if object is of type 'strategy'}
+\arguments{\item{x}{object to be tested}}
Added: pkg/quantstrat/man/strategy.Rd
===================================================================
--- pkg/quantstrat/man/strategy.Rd (rev 0)
+++ pkg/quantstrat/man/strategy.Rd 2010-02-02 03:25:09 UTC (rev 222)
@@ -0,0 +1,10 @@
+\name{strategy}
+\alias{strategy}
+\title{constructor for objects of type 'strategy'...}
+\usage{strategy(name, ..., assets, constraints, store=FALSE)}
+\description{constructor for objects of type 'strategy'}
+\arguments{\item{name}{character string naming the strategy}
+\item{...}{any other passthru parameters}
+\item{assets}{optional list of assets to apply the strategy to}
+\item{constraints}{optional portfolio constraints object matching assets}
+\item{store}{TRUE/FALSE whether to store the strategy in the .strategy environment, or return it. default FALSE}}
More information about the Blotter-commits
mailing list