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

Joshua Ulrich josh.m.ulrich at gmail.com
Wed Apr 15 04:34:01 CEST 2015


Hi Ivan,

On Tue, Apr 14, 2015 at 6:33 PM, Ivan Popivanov
<ivan.popivanov at gmail.com> wrote:
> 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)
>
Thanks for the report and the fix.

This looks like something I missed in r1681 when fixing #5808:
https://r-forge.r-project.org/scm/viewvc.php/pkg/blotter/R/addTxn.R?root=blotter&r1=1666&r2=1681&pathrev=1681
https://r-forge.r-project.org/tracker/index.php?func=detail&aid=5808&group_id=316&atid=1269

Fixed in r1685.  Thanks for tracking this down.  It's a subtle issue
and I'm sure it took some work to uncover the root cause.

> Regards,
> Ivan
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.



-- 
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com



More information about the R-SIG-Finance mailing list