[R-SIG-Finance] Bug in ruleOrderProc - txnfees is not a numeric?

Brian G. Peterson brian at braverock.com
Tue Jan 14 15:26:28 CET 2014


You don't actually include how you're calculating txnfees, and your 
example is not reproducible, so I'm forced to guess.

try

  return(as.numeric(res))

in your function.

As noted in the documentation, txnFees can be a string name of a 
function, so forcing it to as.numeric is a really bad idea.

I'll also note that you don't need to call updatePortf every time. 
That's extremely expensive by comparison to just adding up the realized 
P&L (from closed transaction) and Unrealized P&L (from your open 
position) yourself.  You should also be aware that this approach (or 
yours) will only capture equity changes in a single instrument (since 
instruments are processed one at a time).

Most literature on evaluating trading strategies (see e.g. Aronson or 
Tomasini and Jaekle) tends to do system design on fixed trade sizes 
because it is much easier to evaluate whether or not your system really 
has any edge, or is just participating in market drift.  I tend to 
separate the order sizing decisions till very late in the strategy 
development process.

Regards,

Brian

On 01/13/2014 07:15 PM, Ivan Popivanov wrote:
> It only happens when I use a custom function to compute the position size,
> but it won't happen with a constant size. So it may have something to do
> with the splitting of orders. My function uses all available equity
> rounding up - it's at the end. I get the following error:
>
> Error in `/.default`(TxnFees, abs(TxnQty)) :
>    non-numeric argument to binary operator
>
> It seems that the fault is in ruleOrderProc, the caller of addTxn,
> whereabouts txnfees is indeed an xts object. To me the fix is to pass
> as.numeric(txnfees) to addTxn.
>
> Regards,
> Ivan
>
> PS: Is my osAllEquity a good way to implement the compounding growth or is
> there a better alternative? I think it's worth having an example to
> illustrate this functionality.
>
> osAllEquity = function(
>        timestamp,
>        orderqty,
>        portfolio.name,
>        symbol,
>        ...)
> {
>     verbose = TRUE
>     # verbose = FALSE
>
>     if(verbose) cat("\n=====")
>     if(verbose) cat(paste( "\n", timestamp, sep=""))
>
>     if(verbose) cat(paste( "\n   orderqty=", orderqty, sep=""))
>     portfolio = updatePortf(Portfolio=portfolio.name, Dates=paste('::',
> as.Date(timestamp), sep=''))
>     # end.eq = getEndEq(portfolio.name, as.Date(timestamp))
>     # print( end.eq )
>     pnl = sum(getPortfolio(portfolio.name)$summary$Net.Trading.PL)
>     end.eq = initEq + pnl
>     if(verbose) cat(paste( "\n   pnl=", pnl, sep=""))
>     if(verbose) cat(paste( "\n   equity=", end.eq, sep=""))
>     close.price = as.numeric(Cl(mktdata[timestamp,]))
>     if(verbose) cat(paste("\n   close price=", close.price, sep=""))
>     res = as.numeric( ceiling( end.eq / close.price ) ) * sign( orderqty )
>     if(verbose) cat(paste("\n   qty=", res, sep=""))
>     if(verbose) cat("\n=====\n")
>     # return(sign(orderqty*1000))
>     # res = 1000*sign(orderqty)
>     return(res)
> }
>
> 	[[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.
>

-- 
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock



More information about the R-SIG-Finance mailing list