[Blotter-commits] r369 - in pkg/quantstrat: . R demo man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Aug 12 13:44:01 CEST 2010
Author: braverock
Date: 2010-08-12 13:44:00 +0200 (Thu, 12 Aug 2010)
New Revision: 369
Added:
pkg/quantstrat/man/sigFormula.Rd
Modified:
pkg/quantstrat/NAMESPACE
pkg/quantstrat/R/indicators.R
pkg/quantstrat/R/orders.R
pkg/quantstrat/R/signals.R
pkg/quantstrat/R/traderules.R
pkg/quantstrat/demo/bbands.R
pkg/quantstrat/demo/faber.R
pkg/quantstrat/demo/maCross.R
pkg/quantstrat/demo/macd.R
pkg/quantstrat/man/add.indicator.Rd
pkg/quantstrat/man/osMaxPos.Rd
pkg/quantstrat/man/ruleSignal.Rd
pkg/quantstrat/man/sigComparison.Rd
pkg/quantstrat/man/sigCrossover.Rd
pkg/quantstrat/man/sigThreshold.Rd
Log:
- set default for data param in signals and rules, should simplify strat creation
- update docs
- revise demos to conform to new defaults
Modified: pkg/quantstrat/NAMESPACE
===================================================================
--- pkg/quantstrat/NAMESPACE 2010-08-12 02:36:35 UTC (rev 368)
+++ pkg/quantstrat/NAMESPACE 2010-08-12 11:44:00 UTC (rev 369)
@@ -17,6 +17,7 @@
export(sigCrossover)
export(sigPeak)
export(sigThreshold)
+export(sigFormula)
export(strategy)
export(applyStrategy)
export(is.strategy)
Modified: pkg/quantstrat/R/indicators.R
===================================================================
--- pkg/quantstrat/R/indicators.R 2010-08-12 02:36:35 UTC (rev 368)
+++ pkg/quantstrat/R/indicators.R 2010-08-12 11:44:00 UTC (rev 369)
@@ -9,8 +9,27 @@
#' Indicators are applied before signals and rules, and the output of indicators
#' may be used as inputs to construct signals or fire rules.
#'
+#' \code{arguments} and \code{parameters} are named lists that describe the arguments to be passed to nthe indicator function.
+#' \code{arguments} is for defining any non-default arguments to be passed to the function named in the \code{name} of the indicator.
+#' For example, the \code{x} argument to a moving average function may be defined as \code{x=quote(Cl(mktdata))}
#'
+#' If you look at the demo scripts, you'll notice that we often use \code{quote(mktdata)} in setting up indicators, signals, or rules.
+#' This tells \R to delay evaluation via \code{quote()}, and to use the special variable \code{mktdata}.
+#'
+#' \code{mktdata} is typically created internally to \code{quantstrat} by looking in the global environment for
+#' a time series of prices or returns. mktdata may also contain other data you've manipulated outside quatstrat,
+#' though where possible you should use quantstrat to contain all the logic for the strategy, to aid in maintenance and modifications.
+#'
+#' The use of \code{quote()} tells R to not evaluate what's inside the quote until the function is evaluated later.
+#' By the time that code is evaluated, \code{mktdata} will be populated with the correct price information based on the contents of whatever portfolio you are evaluating the strategy on.
#'
+#' \code{parameters} is another named list, and normally will not be needed.
+#' If you have multiple indicator, signal, or rule functions share the that
+#' \emph{both} share the same argument names \emph{and} will need to have
+#' different values passed to those arguments as defined parameters at apply-time,
+#' then you may need to give them unique names so that delayed evaluation can
+#' sort it all out for you at apply-time.
+#' We will endeavor to get an example of named parameters into the demo scripts.
#'
#' @param strategy an object of type 'strategy' to add the indicator to
#' @param name name of the indicator, must correspond to an R function
@@ -22,6 +41,7 @@
#' @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
+#' @seealso \code{\link{quote}}
add.indicator <- function(strategy, name, arguments, parameters=NULL, label=NULL, ..., enabled=TRUE, indexnum=NULL, store=FALSE) {
if(!is.strategy(strategy)) stop("You must pass in a strategy object to manipulate")
tmp_indicator<-list()
Modified: pkg/quantstrat/R/orders.R
===================================================================
--- pkg/quantstrat/R/orders.R 2010-08-12 02:36:35 UTC (rev 368)
+++ pkg/quantstrat/R/orders.R 2010-08-12 11:44:00 UTC (rev 369)
@@ -331,17 +331,12 @@
{
orderbook <- getOrderBook(portfolio)
orderset <- orderbook[[portfolio]][[symbol]]
-#
-# orderbook[[portfolio]][[symbol]][index(updatedorders),]<-updatedorders
-#
-# # assign order book back into place (do we need a non-exported "put" function?)
-# assign(paste("order_book",portfolio,sep='.'),orderbook,envir=.strategy)
# get open orders
procorders=NULL
procorders<-getOrders(portfolio=portfolio, symbol=symbol, status="open", timespan=timespan, ordertype=ordertype,which.i=TRUE)
- if (!is.null(procorders)){
- if (length(procorders)>=1){
+ # check for open orders
+ if (length(procorders)>=1){
# get previous bar
prevtime <- time(mktdata[last(mktdata[timespan, which.i = TRUE])-1])
timestamp <- time(last(mktdata[timespan]))
@@ -449,13 +444,21 @@
}
}
# if market is beyond price+(-threshold), replace order
- if(is.null(txnprice)){
- if(as.numeric(orderset[ii,]$Order.Qty)>0){
- prefer='offer'
- } else {
- prefer='bid'
- }
- # we didn't trade, so check to see if we need to move the stop
+ if(is.null(txnprice)) {
+ # we didn't trade, so check to see if we need to move the stop
+
+ # first figure out how to find a price
+ if (is.OHLC(mktdata)){
+ prefer='close'
+ } else if(is.BBO(mktdata)) {
+ if(as.numeric(orderset[ii,]$Order.Qty)>0){
+ prefer='offer'
+ } else {
+ prefer='bid'
+ }
+ } else {
+ prefer=NULL # see if getPrice can figure it out
+ }
if( getPrice(mktdata[timestamp],prefer=prefer)+orderset[ii,]$Order.Threshold > orderset[ii,]$Order.Price ){
neworder<-addOrder(portfolio=portfolio,
symbol=symbol,
@@ -486,13 +489,12 @@
} # end higher frequency processing
) # end switch on freq
+
# now put the orders back in
# assign order book back into place (do we need a non-exported "put" function?)
orderbook[[portfolio]][[symbol]] <- orderset
assign(paste("order_book",portfolio,sep='.'),orderbook,envir=.strategy)
-
- } # end check for open orders
- } #end is.null check
+ } # end check for open orders
}
###############################################################################
# R (http://r-project.org/) Quantitative Strategy Model Framework
Modified: pkg/quantstrat/R/signals.R
===================================================================
--- pkg/quantstrat/R/signals.R 2010-08-12 02:36:35 UTC (rev 368)
+++ pkg/quantstrat/R/signals.R 2010-08-12 11:44:00 UTC (rev 369)
@@ -134,11 +134,8 @@
#' @param data data to apply comparison to
#' @param columns named columns to apply comparison to
#' @param relationship one of c("gt","lt","eq","gte","lte","op") or reasonable alternatives
-#' @example
-#' getSymbols("IBM")
-#' sigComparison(label="Cl.gt.Op",data=IBM,columns=c("Close","Open"),"gt")
#' @export
-sigComparison <- function(label,data, columns, relationship=c("gt","lt","eq","gte","lte")) {
+sigComparison <- function(label,data=mktdata, columns, relationship=c("gt","lt","eq","gte","lte")) {
relationship=relationship[1] #only use the first one
if (length(columns==2)){
ret_sig=NULL
@@ -192,7 +189,7 @@
#' @param columns named columns to apply crossover of the first against the second
#' @param relationship one of c("gt","lt","eq","gte","lte") or reasonable alternatives
#' @export
-sigCrossover <- function(label,data, columns, relationship=c("gt","lt","eq","gte","lte")) {
+sigCrossover <- function(label,data=mktdata, columns, relationship=c("gt","lt","eq","gte","lte")) {
ret_sig = FALSE
lng<-length(columns)
for (i in 1:(lng-1)) {
@@ -238,7 +235,7 @@
#' @param relationship one of c("gt","lt","eq","gte","lte") or reasonable alternatives
#' @param cross if TRUE, will return TRUE only for the first observation to cross the threshold in a run
#' @export
-sigThreshold <- function(label, data, column, threshold=0, relationship=c("gt","lt","eq","gte","lte"),cross=FALSE) {
+sigThreshold <- function(label, data=mktdata, column, threshold=0, relationship=c("gt","lt","eq","gte","lte"),cross=FALSE) {
relationship=relationship[1] #only use the first one
ret_sig=NULL
colNum <- match.names(column, colnames(data))
@@ -260,6 +257,22 @@
return(ret_sig)
}
+#' generate a signal from a formula
+#' @param label text label to apply to the output
+#' @param data data to apply formula to
+#' @param formula a logical expression like that used in an if statement, will typically reference column names in \code{mktdata}
+#' @param cross if TRUE, will return TRUE only for the first observation to match the formula in a run
+#' @export
+sigFormula <- function(label, data=mktdata, formula ,cross=FALSE){
+ # Vijay's PAST/AAII/SIPRO example
+ # fieldVals <- try(eval(parse(text=expression), al, parent.frame()))
+ ret_sig=NULL
+ ret_sig <- try(eval(parse(text=formula), data, parent.frame()))
+ if(isTRUE(cross)) ret_sig <- diff(ret_sig)==1
+ colnames(ret_sig)<-label
+ return(ret_sig)
+}
+
#TODO Going Up/Going Down maybe better implemented as slope/diff() indicator, then coupled with threshold signal
#TODO set/reset indicator/signal for n-periods since some other signal is set, or signal set for n periods
Modified: pkg/quantstrat/R/traderules.R
===================================================================
--- pkg/quantstrat/R/traderules.R 2010-08-12 02:36:35 UTC (rev 368)
+++ pkg/quantstrat/R/traderules.R 2010-08-12 11:44:00 UTC (rev 369)
@@ -11,7 +11,7 @@
#' If \code{orderside} is NULL, the function will attempt to calculate the side from the current position
#' (if any) and the order quantity.
#'
-#' @param mktdata an xts object containing market data. depending on rules, may need to be in OHLCV or BBO formats, and may include indicator and signal information
+#' @param data an xts object containing market data. depending on rules, may need to be in OHLCV or BBO formats, and may include indicator and signal information
#' @param timestamp timestamp coercible to POSIXct that will be the time the order will be inserted on
#' @param sigcol column name to check for signal
#' @param sigval signal value to match against
@@ -29,16 +29,16 @@
#' @param ruletype one of "risk","order","rebalance","exit","entry", see \code{\link{add.rule}}
#' @seealso \code{\link{osNoOp}} , \code{\link{add.rule}}
#' @export
-ruleSignal <- function(mktdata, timestamp, sigcol, sigval, orderqty=0, ordertype, orderside, threshold=NULL, replace=TRUE, delay=0.0001, osFUN='osNoOp', pricemethod=c('market','opside'), portfolio, symbol, ..., ruletype ) {
+ruleSignal <- function(data=mktdata, timestamp, sigcol, sigval, orderqty=0, ordertype, orderside=NULL, threshold=NULL, replace=TRUE, delay=0.0001, osFUN='osNoOp', pricemethod=c('market','opside'), portfolio, symbol, ..., ruletype ) {
if(!is.function(osFUN)) osFUN<-match.fun(osFUN)
#print(paste(symbol,timestamp))
- #print(mktdata[timestamp][,sigcol])
- if (!is.na(mktdata[as.character(timestamp)][,sigcol]) & mktdata[as.character(timestamp)][,sigcol] == sigval) {
+ #print(data[timestamp][,sigcol])
+ if (!is.na(data[as.character(timestamp)][,sigcol]) & data[as.character(timestamp)][,sigcol] == sigval) {
#TODO add fancy formals matching for osFUN
if(orderqty>0 & orderqty<1){
# TODO add proportional order? or should that go in order sizing function?
}
- orderqty <- osFUN(strategy=strategy, mktdata=mktdata, timestamp=timestamp, orderqty=orderqty, ordertype=ordertype, orderside=orderside, portfolio=portfolio, symbol=symbol,...=...,ruletype=ruletype)
+ orderqty <- osFUN(strategy=strategy, data=mktdata, timestamp=timestamp, orderqty=orderqty, ordertype=ordertype, orderside=orderside, portfolio=portfolio, symbol=symbol,...=...,ruletype=ruletype)
#calculate order price using pricemethod
pricemethod<-pricemethod[1] #only use the first if not set by calling function
@@ -48,12 +48,12 @@
prefer='ask' # we're buying, so pay what they're asking
else
prefer='bid' # we're selling, so give it to them for what they're bidding
- orderprice <- try(getPrice(x=mktdata,prefer=prefer))
+ orderprice <- try(getPrice(x=data,prefer=prefer))
},
market = {
if(hasArg(prefer)) prefer=match.call(expand.dots=TRUE)$prefer
else prefer = NULL
- orderprice <- try(getPrice(x=mktdata, prefer=prefer))
+ orderprice <- try(getPrice(x=data, prefer=prefer))
}
)
if(inherits(orderprice,'try-error')) orderprice<-NULL
@@ -170,7 +170,7 @@
#' but this is the default. If you don't want to use levels, set
#' them to 1.
#'
-#' @param mktdata an xts object containing market data. depending on rules, may need to be in OHLCV or BBO formats, and may include indicator and signal information
+#' @param data an xts object containing market data. depending on rules, may need to be in OHLCV or BBO formats, and may include indicator and signal information
#' @param timestamp timestamp coercible to POSIXct that will be the time the order will be inserted on
#' @param orderqty numeric quantity of the desired order, modified by osFUN
#' @param ordertype one of "market","limit","stoplimit", or "stoptrailing"
@@ -181,7 +181,7 @@
#' @param ... any other passthru parameters
#' @seealso \code{\link{addPosLimit}},\code{\link{getPosLimit}}
#' @export
-osMaxPos <- function(mktdata, timestamp, orderqty, ordertype, orderside, portfolio, symbol, ruletype, ...){
+osMaxPos <- function(data, timestamp, orderqty, ordertype, orderside, portfolio, symbol, ruletype, ...){
# TODO integrate orderqty='all' into osMaxPos by combining side
# check for current position
pos<-getPosQty(portfolio,symbol,timestamp)
Modified: pkg/quantstrat/demo/bbands.R
===================================================================
--- pkg/quantstrat/demo/bbands.R 2010-08-12 02:36:35 UTC (rev 368)
+++ pkg/quantstrat/demo/bbands.R 2010-08-12 11:44:00 UTC (rev 369)
@@ -30,20 +30,16 @@
s <- add.indicator(strategy = s, name = "BBands", arguments = list(HLC = quote(HLC(mktdata)), maType='SMA'))
-#if you wanted to manually apply a signal function for demonstration
-#cbind(IBM.mod,sigComparison(label="Close.gt.Open",data=IBM.inds,columns=c("Close","Open"),">"))
-#cbind(IBM.mod,sigComparison(label="Adjusted.gt.SMA",data=IBM.inds,columns=c("Adjusted","SMA10"),">"))
-
#add signals:
-s<- add.signal(s,name="sigCrossover",arguments = list(data=quote(mktdata),columns=c("Close","up"),relationship="gt"),label="Cl.gt.UpperBand")
-s<- add.signal(s,name="sigCrossover",arguments = list(data=quote(mktdata),columns=c("Close","dn"),relationship="lt"),label="Cl.lt.LowerBand")
-s<- add.signal(s,name="sigCrossover",arguments = list(data=quote(mktdata),columns=c("High","Low","mavg"),relationship="op"),label="Cross.Mid")
+s<- add.signal(s,name="sigCrossover",arguments = list(columns=c("Close","up"),relationship="gt"),label="Cl.gt.UpperBand")
+s<- add.signal(s,name="sigCrossover",arguments = list(columns=c("Close","dn"),relationship="lt"),label="Cl.lt.LowerBand")
+s<- add.signal(s,name="sigCrossover",arguments = list(columns=c("High","Low","mavg"),relationship="op"),label="Cross.Mid")
# lets add some rules
s
-s <- add.rule(s,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Cl.gt.UpperBand",sigval=TRUE, orderqty=-100, ordertype='market', orderside=NULL, threshold=NULL),type='enter')
-s <- add.rule(s,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Cl.lt.LowerBand",sigval=TRUE, orderqty= 100, ordertype='market', orderside=NULL, threshold=NULL),type='enter')
-s <- add.rule(s,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Cross.Mid",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
+s <- add.rule(s,name='ruleSignal', arguments = list(sigcol="Cl.gt.UpperBand",sigval=TRUE, orderqty=-100, ordertype='market', orderside=NULL, threshold=NULL),type='enter')
+s <- add.rule(s,name='ruleSignal', arguments = list(sigcol="Cl.lt.LowerBand",sigval=TRUE, orderqty= 100, ordertype='market', orderside=NULL, threshold=NULL),type='enter')
+s <- add.rule(s,name='ruleSignal', arguments = list(sigcol="Cross.Mid",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
#s <- add.rule(s,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Lo.gt.UpperBand",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
#s <- add.rule(s,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Hi.lt.LowerBand",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
#TODO add thresholds and stop-entry and stop-exit handling to test
Modified: pkg/quantstrat/demo/faber.R
===================================================================
--- pkg/quantstrat/demo/faber.R 2010-08-12 02:36:35 UTC (rev 368)
+++ pkg/quantstrat/demo/faber.R 2010-08-12 11:44:00 UTC (rev 369)
@@ -89,15 +89,15 @@
# There are two signals:
# The first is when monthly price crosses over the 10-month SMA
-s<- add.signal(s,name="sigCrossover",arguments = list(data=quote(mktdata),columns=c("Close","SMA10"),relationship="gt"),label="Cl.gt.SMA")
+s<- add.signal(s,name="sigCrossover",arguments = list(columns=c("Close","SMA10"),relationship="gt"),label="Cl.gt.SMA")
# The second is when the monthly price crosses under the 10-month SMA
-s<- add.signal(s,name="sigCrossover",arguments = list(data=quote(mktdata),columns=c("Close","SMA10"),relationship="lt"),label="Cl.lt.SMA")
+s<- add.signal(s,name="sigCrossover",arguments = list(columns=c("Close","SMA10"),relationship="lt"),label="Cl.lt.SMA")
# There are two rules:
# The first is to buy when the price crosses above the SMA
-s <- add.rule(s, name='ruleSignal', arguments = list(data=quote(mktdata), sigcol="Cl.gt.SMA", sigval=TRUE, orderqty=1000, ordertype='market', orderside='long', pricemethod='market'), type='enter', path.dep=TRUE)
+s <- add.rule(s, name='ruleSignal', arguments = list(sigcol="Cl.gt.SMA", sigval=TRUE, orderqty=1000, ordertype='market', orderside='long', pricemethod='market'), type='enter', path.dep=TRUE)
# The second is to sell when the price crosses below the SMA
-s <- add.rule(s, name='ruleSignal', arguments = list(data=quote(mktdata), sigcol="Cl.lt.SMA", sigval=TRUE, orderqty='all', ordertype='market', orderside='long', pricemethod='market'), type='exit', path.dep=TRUE)
+s <- add.rule(s, name='ruleSignal', arguments = list(sigcol="Cl.lt.SMA", sigval=TRUE, orderqty='all', ordertype='market', orderside='long', pricemethod='market'), type='exit', path.dep=TRUE)
# Process the indicators and generate trades
start_t<-Sys.time()
Modified: pkg/quantstrat/demo/maCross.R
===================================================================
--- pkg/quantstrat/demo/maCross.R 2010-08-12 02:36:35 UTC (rev 368)
+++ pkg/quantstrat/demo/maCross.R 2010-08-12 11:44:00 UTC (rev 369)
@@ -24,16 +24,16 @@
stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "SMA", arguments = list(x=quote(Cl(mktdata)), n=50),label= "ma50" )
stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "SMA", arguments = list(x=quote(Cl(mktdata)), n=200),label= "ma200")
-stratMACROSS <- add.signal(strategy = stratMACROSS,name="sigCrossover",arguments = list(data=quote(mktdata),columns=c("ma50","ma200"),relationship="gt"),label="ma50.gt.ma200")
-stratMACROSS <- add.signal(strategy = stratMACROSS,name="sigCrossover",arguments = list(data=quote(mktdata),column=c("ma50","ma200"),relationship="lt"),label="ma50.lt.ma200")
+stratMACROSS <- add.signal(strategy = stratMACROSS,name="sigCrossover",arguments = list(columns=c("ma50","ma200"),relationship="gt"),label="ma50.gt.ma200")
+stratMACROSS <- add.signal(strategy = stratMACROSS,name="sigCrossover",arguments = list(column=c("ma50","ma200"),relationship="lt"),label="ma50.lt.ma200")
-stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="ma50.gt.ma200",sigval=TRUE, orderqty=100, ordertype='market', orderside='long'),type='enter')
-stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="ma50.lt.ma200",sigval=TRUE, orderqty=-100, ordertype='market', orderside='long'),type='exit')
+stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal', arguments = list(sigcol="ma50.gt.ma200",sigval=TRUE, orderqty=100, ordertype='market', orderside='long'),type='enter')
+stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal', arguments = list(sigcol="ma50.lt.ma200",sigval=TRUE, orderqty=-100, ordertype='market', orderside='long'),type='exit')
# if you want a long/short Stops and Reverse MA cross strategy, you'd add two more rules for the short side:
-# stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="ma50.lt.ma200",sigval=TRUE, orderqty=-100, ordertype='market', orderside='short'),type='enter')
-# stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="ma50.gt.ma200",sigval=TRUE, orderqty=100, ordertype='market', orderside='short'),type='exit')
+# stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal', arguments = list(sigcol="ma50.lt.ma200",sigval=TRUE, orderqty=-100, ordertype='market', orderside='short'),type='enter')
+# stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal', arguments = list(sigcol="ma50.gt.ma200",sigval=TRUE, orderqty=100, ordertype='market', orderside='short'),type='exit')
getSymbols(stock.str,from=initDate)
start_t<-Sys.time()
Modified: pkg/quantstrat/demo/macd.R
===================================================================
--- pkg/quantstrat/demo/macd.R 2010-08-12 02:36:35 UTC (rev 368)
+++ pkg/quantstrat/demo/macd.R 2010-08-12 11:44:00 UTC (rev 369)
@@ -40,20 +40,20 @@
stratMACD <- add.indicator(strategy = stratMACD, name = "MACD", arguments = list(x=quote(Cl(mktdata))) )
-stratMACD <- add.signal(strategy = stratMACD,name="sigThreshold",arguments = list(data=quote(mktdata),column="signal",relationship="gt",threshold=0,cross=TRUE),label="signal.gt.zero")
-stratMACD <- add.signal(strategy = stratMACD,name="sigThreshold",arguments = list(data=quote(mktdata),column="signal",relationship="lt",threshold=0,cross=TRUE),label="signal.lt.zero")
+stratMACD <- add.signal(strategy = stratMACD,name="sigThreshold",arguments = list(column="signal",relationship="gt",threshold=0,cross=TRUE),label="signal.gt.zero")
+stratMACD <- add.signal(strategy = stratMACD,name="sigThreshold",arguments = list(column="signal",relationship="lt",threshold=0,cross=TRUE),label="signal.lt.zero")
-stratMACD <- add.rule(strategy = stratMACD,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="signal.gt.zero",sigval=TRUE, orderqty=100, ordertype='market', orderside='long', threshold=NULL),type='enter')
-stratMACD <- add.rule(strategy = stratMACD,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="signal.gt.zero",sigval=TRUE, orderqty=-100, ordertype='stoplimit', orderside='long', threshold=.95,tmult=TRUE),type='risk')
-stratMACD <- add.rule(strategy = stratMACD,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="signal.lt.zero",sigval=TRUE, orderqty='all', ordertype='market', orderside='long', threshold=NULL),type='exit')
+stratMACD <- add.rule(strategy = stratMACD,name='ruleSignal', arguments = list(sigcol="signal.gt.zero",sigval=TRUE, orderqty=100, ordertype='market', orderside='long', threshold=NULL),type='enter')
+stratMACD <- add.rule(strategy = stratMACD,name='ruleSignal', arguments = list(sigcol="signal.gt.zero",sigval=TRUE, orderqty=-100, ordertype='stoplimit', orderside='long', threshold=.95,tmult=TRUE),type='risk')
+stratMACD <- add.rule(strategy = stratMACD,name='ruleSignal', arguments = list(sigcol="signal.lt.zero",sigval=TRUE, orderqty='all', ordertype='market', orderside='long', threshold=NULL),type='exit')
getSymbols(stock.str,from=initDate)
start_t<-Sys.time()
out<-try(applyStrategy(strategy=stratMACD , portfolios=portfolio.st,parameters=list(nFast=fastMA, nSlow=slowMA, nSig=signalMA,maType=maType)))
end_t<-Sys.time()
end_t-start_t
-updatePortf(Portfolio='macd',Dates=paste('::',as.Date(Sys.time()),sep=''))
-chart.Posn(Portfolio='macd',Symbol=stock.str)
+updatePortf(Portfolio=portfolio.st,Dates=paste('::',as.Date(Sys.time()),sep=''))
+chart.Posn(Portfolio=portfolio.st,Symbol=stock.str)
plot(add_MACD(fast=fastMA, slow=slowMA, signal=signalMA,maType="EMA"))
Modified: pkg/quantstrat/man/add.indicator.Rd
===================================================================
--- pkg/quantstrat/man/add.indicator.Rd 2010-08-12 02:36:35 UTC (rev 368)
+++ pkg/quantstrat/man/add.indicator.Rd 2010-08-12 11:44:00 UTC (rev 369)
@@ -10,7 +10,30 @@
Indicators are always path-independent, and should be constructed from vectorized functions where possible.
Indicators are applied before signals and rules, and the output of indicators
-may be used as inputs to construct signals or fire rules.}
+may be used as inputs to construct signals or fire rules.
+
+\code{arguments} and \code{parameters} are named lists that describe the arguments to be passed to nthe indicator function.
+\code{arguments} is for defining any non-default arguments to be passed to the function named in the \code{name} of the indicator.
+For example, the \code{x} argument to a moving average function may be defined as \code{x=quote(Cl(mktdata))}
+
+If you look at the demo scripts, you'll notice that we often use \code{quote(mktdata)} in setting up indicators, signals, or rules.
+This tells \R to delay evaluation via \code{quote()}, and to use the special variable \code{mktdata}.
+
+\code{mktdata} is typically created internally to \code{quantstrat} by looking in the global environment for
+a time series of prices or returns. mktdata may also contain other data you've manipulated outside quatstrat,
+though where possible you should use quantstrat to contain all the logic for the strategy, to aid in maintenance and modifications.
+
+The use of \code{quote()} tells R to not evaluate what's inside the quote until the function is evaluated later.
+By the time that code is evaluated, \code{mktdata} will be populated with the correct price information based on the contents of whatever portfolio you are evaluating the strategy on.
+
+\code{parameters} is another named list, and normally will not be needed.
+If you have multiple indicator, signal, or rule functions share the that
+\emph{both} share the same argument names \emph{and} will need to have
+different values passed to those arguments as defined parameters at apply-time,
+then you may need to give them unique names so that delayed evaluation can
+sort it all out for you at apply-time.
+We will endeavor to get an example of named parameters into the demo scripts.}
+\seealso{\code{\link{quote}}}
\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}
Modified: pkg/quantstrat/man/osMaxPos.Rd
===================================================================
--- pkg/quantstrat/man/osMaxPos.Rd 2010-08-12 02:36:35 UTC (rev 368)
+++ pkg/quantstrat/man/osMaxPos.Rd 2010-08-12 11:44:00 UTC (rev 369)
@@ -1,7 +1,7 @@
\name{osMaxPos}
\alias{osMaxPos}
\title{order sizing function for position limits and level sizing...}
-\usage{osMaxPos(mktdata, timestamp, orderqty, ordertype, orderside, portfolio,
+\usage{osMaxPos(data, timestamp, orderqty, ordertype, orderside, portfolio,
symbol, ruletype, ...)}
\description{order sizing function for position limits and level sizing}
\details{levels are a simplification of more complex (proprietary)
@@ -11,7 +11,7 @@
but this is the default. If you don't want to use levels, set
them to 1.}
\seealso{\code{\link{addPosLimit}},\code{\link{getPosLimit}}}
-\arguments{\item{mktdata}{an xts object containing market data. depending on rules, may need to be in OHLCV or BBO formats, and may include indicator and signal information}
+\arguments{\item{data}{an xts object containing market data. depending on rules, may need to be in OHLCV or BBO formats, and may include indicator and signal information}
\item{timestamp}{timestamp coercible to POSIXct that will be the time the order will be inserted on}
\item{orderqty}{numeric quantity of the desired order, modified by osFUN}
\item{ordertype}{one of "market","limit","stoplimit", or "stoptrailing"}
Modified: pkg/quantstrat/man/ruleSignal.Rd
===================================================================
--- pkg/quantstrat/man/ruleSignal.Rd 2010-08-12 02:36:35 UTC (rev 368)
+++ pkg/quantstrat/man/ruleSignal.Rd 2010-08-12 11:44:00 UTC (rev 369)
@@ -1,10 +1,10 @@
\name{ruleSignal}
\alias{ruleSignal}
\title{default rule to generate a trade order on a signal...}
-\usage{ruleSignal(mktdata, timestamp, sigcol, sigval, orderqty=0, ordertype,
- orderside, threshold, replace=TRUE, delay=1e-04, osFUN="osNoOp",
- pricemethod=c("market", "opside"), portfolio, symbol, ...,
- ruletype)}
+\usage{ruleSignal(data=mktdata, timestamp, sigcol, sigval, orderqty=0,
+ ordertype, orderside, threshold, replace=TRUE, delay=1e-04,
+ osFUN="osNoOp", pricemethod=c("market", "opside"), portfolio,
+ symbol, ..., ruletype)}
\description{default rule to generate a trade order on a signal}
\details{\code{pricemethod} may be one of 'market' or 'opside'
which will either try to get the price of the 'market' at \code{timestamp} and use this as the order price
@@ -16,7 +16,7 @@
If \code{orderside} is NULL, the function will attempt to calculate the side from the current position
(if any) and the order quantity.}
\seealso{\code{\link{osNoOp}} , \code{\link{add.rule}}}
-\arguments{\item{mktdata}{an xts object containing market data. depending on rules, may need to be in OHLCV or BBO formats, and may include indicator and signal information}
+\arguments{\item{data}{an xts object containing market data. depending on rules, may need to be in OHLCV or BBO formats, and may include indicator and signal information}
\item{timestamp}{timestamp coercible to POSIXct that will be the time the order will be inserted on}
\item{sigcol}{column name to check for signal}
\item{sigval}{signal value to match against}
Modified: pkg/quantstrat/man/sigComparison.Rd
===================================================================
--- pkg/quantstrat/man/sigComparison.Rd 2010-08-12 02:36:35 UTC (rev 368)
+++ pkg/quantstrat/man/sigComparison.Rd 2010-08-12 11:44:00 UTC (rev 369)
@@ -1,8 +1,8 @@
\name{sigComparison}
\alias{sigComparison}
\title{generate comparison signal...}
-\usage{sigComparison(label, data, columns, relationship=c("gt", "lt", "eq",
- "gte", "lte"))}
+\usage{sigComparison(label, data=mktdata, columns, relationship=c("gt", "lt",
+ "eq", "gte", "lte"))}
\description{generate comparison signal}
\details{Currently, this function compares two columns.
Patches to compare an arbitrary number of columns would be gladly accepted.
Modified: pkg/quantstrat/man/sigCrossover.Rd
===================================================================
--- pkg/quantstrat/man/sigCrossover.Rd 2010-08-12 02:36:35 UTC (rev 368)
+++ pkg/quantstrat/man/sigCrossover.Rd 2010-08-12 11:44:00 UTC (rev 369)
@@ -1,8 +1,8 @@
\name{sigCrossover}
\alias{sigCrossover}
\title{generate a crossover signal...}
-\usage{sigCrossover(label, data, columns, relationship=c("gt", "lt", "eq",
- "gte", "lte"))}
+\usage{sigCrossover(label, data=mktdata, columns, relationship=c("gt", "lt",
+ "eq", "gte", "lte"))}
\description{generate a crossover signal}
\details{This will generate a crossover signal, which is a dimension-reduced version
of a comparison signal \code{\link{sigComparison}}.
Added: pkg/quantstrat/man/sigFormula.Rd
===================================================================
--- pkg/quantstrat/man/sigFormula.Rd (rev 0)
+++ pkg/quantstrat/man/sigFormula.Rd 2010-08-12 11:44:00 UTC (rev 369)
@@ -0,0 +1,9 @@
+\name{sigFormula}
+\alias{sigFormula}
+\title{generate a signal from a formula...}
+\usage{sigFormula(label, data=mktdata, formula, cross=FALSE)}
+\description{generate a signal from a formula}
+\arguments{\item{label}{text label to apply to the output}
+\item{data}{data to apply formula to}
+\item{formula}{a logical expression like that used in an if statement, will typically reference column names in \code{mktdata}}
+\item{cross}{if TRUE, will return TRUE only for the first observation to match the formula in a run}}
Modified: pkg/quantstrat/man/sigThreshold.Rd
===================================================================
[TRUNCATED]
To get the complete diff run:
svnlook diff /svnroot/blotter -r 369
More information about the Blotter-commits
mailing list