[R-SIG-Finance] quantstrat executes trades that are more worth than total equity

Sergey Pisarenko drseergio at gmail.com
Thu Feb 16 18:23:20 CET 2012

Hi R programmers,

I am seeing strange behavior with quanstrat transactions. The problem
is that it triggers orders that are larger than the total equity. For
example, if I create an account with initial equity of 100 000 I see
that rules that exceed the total equity are still executed and

Is this the intended behavior? Do I need to write my own sizing
function which will take care of this?

To reproduce this I have set "maxpos" to very high value which cannot
be successfully executed with available equity (100000). The
"add.rule" and related parts of the code:

initPortf('p', symbols=watched, currency=curr, initDate=from)
initAcct(name='a', portfolios='p', initEq=equity, currency=curr, initDate=from)
initOrders(portfolio='p', symbols=watched, initDate=from)

s <- add.rule(strategy=s, name='ruleSignal',
arguments=list(data=quote(mktdata), sigcol='buySig', sigval=TRUE,
orderqty=100000, ordertype='market', orderside=NULL, threshold=NULL,
osFUN='osMaxPos', ruletype='enter'), type='enter', path.dep=TRUE)

for (symbol in symbols) {
  ubars    <- getSymbols(symbol, src='yahoo', auto.assign=FALSE)
  bars     <- ubars[index(ubars) >= as.Date(from)]
  assign(symbol, bars)
  addPosLimit(portfolio='p', symbol, from, maxpos=100000)

applyStrategy(strategy='s', portfolios='p')

The R output for getTxns:
> getTxns(Portfolio='p', Symbol='BP')
           Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost
2009-01-01   0e+00      0.00        0         0         0.00
2010-07-08   1e+05     33.74        0   3374000        33.74
2009-01-01                   0
2010-07-08                   0

I have attached the whole runnable sample script to this e-mail. I
have removed all the code which is not relevant to the issue.

Kind Regards,
Sergey Pisarenko.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: tainted.R
Type: application/octet-stream
Size: 1985 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20120216/adea2f80/attachment.obj>

More information about the R-SIG-Finance mailing list