[Blotter-commits] r353 - in pkg/quantstrat: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Jul 16 19:44:32 CEST 2010
Author: braverock
Date: 2010-07-16 19:44:32 +0200 (Fri, 16 Jul 2010)
New Revision: 353
Modified:
pkg/quantstrat/R/orders.R
pkg/quantstrat/R/traderules.R
pkg/quantstrat/man/add.indicator.Rd
pkg/quantstrat/man/osMaxPos.Rd
pkg/quantstrat/man/osNoOp.Rd
pkg/quantstrat/man/ruleSignal.Rd
Log:
- smarter handling of orderqty='all'
- better handling of 'prefer' argument
- update documentation
Modified: pkg/quantstrat/R/orders.R
===================================================================
--- pkg/quantstrat/R/orders.R 2010-07-14 15:44:43 UTC (rev 352)
+++ pkg/quantstrat/R/orders.R 2010-07-16 17:44:32 UTC (rev 353)
@@ -102,6 +102,16 @@
#' add an order to the order book
#'
+#' It is important to understand that all the order functionality included in \code{quantstrat}
+#' exists to more closely model a real trading environment both in backtesting and in production.
+#' Many backtesting systems make a set of assumptions about instant execution,
+#' and we have chosen not to do this in quantstrat, because real quantitative
+#' trading systems do not have instant execution. They make decisions
+#' (the Rules) and then enter orders (the province of this function in backtesting),
+#' during which there is some \code{delay} between receiving the data that fires the
+#' Signal and Rule, and the time the order reaches the market, and then those orders
+#' MAY become transactions if market prices and liquidity cooperate.
+#'
#' By default, this function will locate and replace any 'open' order(s)
#' on the requested portfolio/symbol that have the same type and side.
#' This is the equivalent of what is sometimes called an
@@ -119,6 +129,23 @@
#' Some markets and brokers recognize a stop that triggers a market order, when the stop is triggered,
#' a market order will be executed at the then-prevailing price. We have not modeled this type of order.
#'
+#' If you ever wanted to move from a backtesting mode to a production mode,
+#' this function (and the linked funtion \code{\link{ruleOrderProc}}) would
+#' need to be replaced by functions that worked against your execution environment.
+#' Basically, the execution environment must provide three interfaces in a live
+#' trading environment:
+#'
+#' \enumerate{
+#' \item a market data interface to provide updated market data, usually accessed in an event loop
+#'
+#' \item an order interface for sending orders (and canceling or updating them) to the market
+#'
+#' \item a fill interface that reports the transaction details when an order has been filled
+#' }
+#'
+#' Conversion to a live trading environment will also likely require a new version of
+#' \code{\link{applyStrategy}} to provide the event loop interface and interact with \code{mktdata}.
+#'
#' @param portfolio text name of the portfolio to associate the order book with
#' @param symbol identfier of the instrument to find orders for. The name of any associated price objects (xts prices, usually OHLC) should match these
#' @param timestamp timestamp coercible to POSIXct that will be the time to search for orders before this time
@@ -254,6 +281,11 @@
}
#' process open orders at time t, generating transactions or new orders
+#'
+#' This function is meant to be sufficient for backtesting most strategies,
+#' but would need to be replaced for production use. It provides the interface
+#' for taking the order book and determining when orders become trades.
+#'
#' @param portfolio text name of the portfolio to associate the order book with
#' @param symbol identfier of the instrument to find orders for. The name of any associated price objects (xts prices, usually OHLC) should match these
#' @param mktdata an xts object containing market data. depending on indicators, may need to be in OHLCV or BBO formats, default NULL
Modified: pkg/quantstrat/R/traderules.R
===================================================================
--- pkg/quantstrat/R/traderules.R 2010-07-14 15:44:43 UTC (rev 352)
+++ pkg/quantstrat/R/traderules.R 2010-07-16 17:44:32 UTC (rev 353)
@@ -26,22 +26,21 @@
#' @param portfolio text name of the portfolio to place orders in
#' @param symbol identifier of the instrument to place orders for. The name of any associated price objects (xts prices, usually OHLC) should match these
#' @param ... any other passthru parameters
-#' @seealso \code{\link{osNoOp}}
+#' @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, ... ) {
+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 ) {
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) {
#TODO add fancy formals matching for osFUN
- if(orderqty=='all'){
- orderqty=-1*getPosQty(Portfolio=portfolio,Symbol=symbol,Date=timestamp)
- }
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,...=...)
- #calculate order price using pricemethod
+ orderqty <- osFUN(strategy=strategy, mktdata=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
switch(pricemethod,
opside = {
@@ -49,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,symbol=symbol,prefer=prefer))
orderprice <- try(getPrice(x=mktdata,prefer=prefer))
},
- market = {
-# orderprice <- try(getPrice(x=mktdata,symbol=symbol,prefer=NULL))
- orderprice <- try(getPrice(x=mktdata, prefer=NULL))
+ market = {
+ if(hasArg(prefer)) prefer=match.call(expand.dots=TRUE)$prefer
+ else prefer = NULL
+ orderprice <- try(getPrice(x=mktdata, prefer=prefer))
}
)
if(inherits(orderprice,'try-error')) orderprice<-NULL
@@ -89,12 +88,26 @@
#' default order sizing function
#'
#' default function performs no operation (NoOp), returns orderqty
+#'
+#' if orderqty 'all', will only work on an exit rule type, otherwize orderqty is zero.
#'
+#' @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 portfolio text name of the portfolio to place orders in
+#' @param symbol identifier of the instrument to place orders for. The name of any associated price objects (xts prices, usually OHLC) should match these
#' @param ... any other passthru parameters
+#' @param ruletype one of "risk","order","rebalance","exit","entry", see \code{\link{add.rule}}
#' @export
-osNoOp <- function(orderqty, ...){
- return(orderqty)
+osNoOp <- function(timestamp, orderqty, portfolio, symbol, ruletype, ...){
+ if(orderqty=='all'){
+ if (ruletype=='exit') {
+ orderqty=-1*getPosQty(Portfolio=portfolio,Symbol=symbol,Date=timestamp)
+ } else {
+ message("orderqty 'all' would produce nonsense, maybe use osMaxPos instead?")
+ orderqty=0
+ }
+ }
+ return(orderqty)
}
@@ -164,9 +177,11 @@
#' @param orderside one of either "long" or "short"
#' @param portfolio text name of the portfolio to place orders in
#' @param symbol identifier of the instrument to place orders for. The name of any associated price objects (xts prices, usually OHLC) should match these
+#' @param ruletype one of "risk","order","rebalance","exit","entry", see \code{\link{add.rule}}
+#' @param ... any other passthru parameters
#' @seealso \code{\link{addPosLimit}},\code{\link{getPosLimit}}
#' @export
-osMaxPos <- function(mktdata, timestamp, orderqty, ordertype, orderside, portfolio, symbol){
+osMaxPos <- function(mktdata, timestamp, orderqty, ordertype, orderside, portfolio, symbol, ruletype, ...){
# check for current position
pos<-getPosQty(portfolio,symbol,timestamp)
# check against max position
Modified: pkg/quantstrat/man/add.indicator.Rd
===================================================================
--- pkg/quantstrat/man/add.indicator.Rd 2010-07-14 15:44:43 UTC (rev 352)
+++ pkg/quantstrat/man/add.indicator.Rd 2010-07-16 17:44:32 UTC (rev 353)
@@ -4,6 +4,13 @@
\usage{add.indicator(strategy, name, arguments, label, ..., enabled=TRUE,
indexnum, store=FALSE)}
\description{add an indicator to a strategy}
+\details{Indicators are typically standard technical or statistical analysis outputs,
+such as moving averages, bands, or pricing models.
+
+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.}
\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-07-14 15:44:43 UTC (rev 352)
+++ pkg/quantstrat/man/osMaxPos.Rd 2010-07-16 17:44:32 UTC (rev 353)
@@ -2,7 +2,7 @@
\alias{osMaxPos}
\title{order sizing function for position limits and level sizing...}
\usage{osMaxPos(mktdata, timestamp, orderqty, ordertype, orderside, portfolio,
- symbol)}
+ symbol, ruletype, ...)}
\description{order sizing function for position limits and level sizing}
\details{levels are a simplification of more complex (proprietary)
techniques sometimes used for order sizing.
@@ -17,4 +17,6 @@
\item{ordertype}{one of "market","limit","stoplimit", or "stoptrailing"}
\item{orderside}{one of either "long" or "short"}
\item{portfolio}{text name of the portfolio to place orders in}
-\item{symbol}{identifier of the instrument to place orders for. The name of any associated price objects (xts prices, usually OHLC) should match these}}
+\item{symbol}{identifier of the instrument to place orders for. The name of any associated price objects (xts prices, usually OHLC) should match these}
+\item{ruletype}{one of "risk","order","rebalance","exit","entry", see \code{\link{add.rule}}}
+\item{...}{any other passthru parameters}}
Modified: pkg/quantstrat/man/osNoOp.Rd
===================================================================
--- pkg/quantstrat/man/osNoOp.Rd 2010-07-14 15:44:43 UTC (rev 352)
+++ pkg/quantstrat/man/osNoOp.Rd 2010-07-16 17:44:32 UTC (rev 353)
@@ -1,8 +1,14 @@
\name{osNoOp}
\alias{osNoOp}
\title{default order sizing function...}
-\usage{osNoOp(orderqty, ...)}
+\usage{osNoOp(timestamp, orderqty, portfolio, symbol, ruletype, ...)}
\description{default order sizing function}
-\details{default function performs no operation (NoOp), returns orderqty}
-\arguments{\item{orderqty}{numeric quantity of the desired order, modified by osFUN}
-\item{...}{any other passthru parameters}}
+\details{default function performs no operation (NoOp), returns orderqty
+
+if orderqty 'all', will only work on an exit rule type, otherwize orderqty is zero.}
+\arguments{\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{portfolio}{text name of the portfolio to place orders in}
+\item{symbol}{identifier of the instrument to place orders for. The name of any associated price objects (xts prices, usually OHLC) should match these}
+\item{...}{any other passthru parameters}
+\item{ruletype}{one of "risk","order","rebalance","exit","entry", see \code{\link{add.rule}}}}
Modified: pkg/quantstrat/man/ruleSignal.Rd
===================================================================
--- pkg/quantstrat/man/ruleSignal.Rd 2010-07-14 15:44:43 UTC (rev 352)
+++ pkg/quantstrat/man/ruleSignal.Rd 2010-07-16 17:44:32 UTC (rev 353)
@@ -3,7 +3,8 @@
\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, ...)}
+ 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
@@ -14,7 +15,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}}}
+\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}
\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}
@@ -29,4 +30,5 @@
\item{pricemethod}{one of 'market' or 'opside', see Details}
\item{portfolio}{text name of the portfolio to place orders in}
\item{symbol}{identifier of the instrument to place orders for. The name of any associated price objects (xts prices, usually OHLC) should match these}
-\item{...}{any other passthru parameters}}
+\item{...}{any other passthru parameters}
+\item{ruletype}{one of "risk","order","rebalance","exit","entry", see \code{\link{add.rule}}}}
More information about the Blotter-commits
mailing list