The ruleOrderProc function is effectively the default fill simulator for quantstrat. 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.
In this version, in contrast with an earlier version, this function will allow a transaction to cross your current position through zero. The accounting rules for realizing gains in such cases are quite complicated, so blotter will split this transaction into two transactions. Many brokers will break, revise, or split such transactions for the same reason.
ruleOrderProc(portfolio, symbol, mktdata, timestamp = NULL,
ordertype = NULL, ..., slippageFUN = NULL)
text name of the portfolio to associate the order book with
identfier of the instrument to find orders for. The name of any associated price objects (xts prices, usually OHLC or BBO) should match these
an xts object containing market data. depending on indicators, may need to be in OHLCV or BBO formats, default NULL
timestamp coercible to POSIXct that will be the time the order will be processed on
one of NULL, "market","limit","stoplimit", or "stoptrailing" default NULL
any other passthru parameters
default NULL, not yet implemented
This function would need to be revised or replaced for connection to a live trading infrastructure.
In a production mode, you would replace the addOrder
function
with a custom function to connect to your market infrastructure.
In that case, you might need to add additional code to your strategy,
or overload functions for checking position.
Note that this function is called by default in the 'orders' slot of the
applyRules
processing. If you have defined another order
processing rule, it will replace this function. If you want your
custom order rule and ruleOrderProc to both be called, you will need
explicitly add a rule to call ruleOrderProc either before or after your
custom order processing function.
We would like to model slippage here via slippageFUN
. Code contributions, suggestions,
and requests appreciated.
add.rule
applyRules
getOrderBook
addOrder
updateOrders