[R-SIG-Finance] A bug in blotter?

Ivan Popivanov ivan.popivanov at gmail.com
Wed Apr 15 01:33:14 CEST 2015


Hello,

Looks like I found a bug in blotter while trying to use it to verify some
code of mine. :)

Here is the code that I question (from addTxn.r):

    # Compute transaction fees if a function was supplied
    if (is.function(TxnFees)) {
      txnfees <- TxnFees(TxnQty, TxnPrice, Symbol)
    } else {
      txnfees<- as.numeric(TxnFees)
    }

    if(is.null(txnfees) || is.na(txnfees))
        txnfees <- 0
    if(txnfees>0 && !isTRUE(allowRebates))
        stop('Positive Transaction Fees should only be used in the
case of broker/exchange rebates for TxnFees ',TxnFees,'. See
Documentation.')

    # split transactions that would cross through zero
    if(PrevPosQty!=0 && sign(PrevPosQty+TxnQty)!=sign(PrevPosQty) &&
PrevPosQty!=-TxnQty){
        txnFeeQty=txnfees/abs(TxnQty) # calculate fees pro-rata by quantity
        addTxn(Portfolio=pname, Symbol=Symbol, TxnDate=TxnDate,
TxnQty=-PrevPosQty, TxnPrice=TxnPrice, ...,
                TxnFees = txnFeeQty*abs(PrevPosQty), ConMult =
ConMult, verbose = verbose, eps=eps)
        TxnDate=TxnDate+2*eps #transactions need unique timestamps, so
increment a bit
        TxnQty=TxnQty+PrevPosQty
        PrevPosQty=0
        TxnFees=txnFeeQty*abs(TxnQty+PrevPosQty)
    }


This code is executed only when a transaction needs to be split in two -
for instance, we have two futures long, and add a transaction for going
short 3 futures.

The problem occurs because the code in the if-statement updates TxnFees,
while the rest of the code, before and after, has computed and used\s
txnfees. In other words, the last line should be:

txnfees=txnFeeQty*abs(TxnQty+PrevPosQty)

Regards,
Ivan

	[[alternative HTML version deleted]]



More information about the R-SIG-Finance mailing list