[R-SIG-Finance] function 'addtxn' in blotter package can't add intraday trade into account?

Joshua Ulrich josh.m.ulrich at gmail.com
Mon Sep 1 17:18:57 CEST 2014


On Sun, Aug 31, 2014 at 10:23 PM, domodo <1111938 at qq.com> wrote:
> hi,guys,I'm learning blotter package,and have coded several trade strategies
> in R.
> all are good when the strategis run on daily data,but when I run on intraday
> data, function 'addtxn' runs incorrectly.
>
addTxn is working correctly.  See below.

> the strategy stores trade signals of each bar,when close price cross over
> sma,signal = 1,close price cross under sma, signal = -1,otherwise, signal =
> 0
>
> and the bar-by-bar treatment are:
>
> #Bar-by-bar treatment
> for( i in 17:nrow(if2) )
> {
>   currentDate <- time(if2)[i]
>
>   equity<-getEndEq(b.strategy, currentDate)
>   Posn <- getPosQty(b.strategy, Symbol='if2', Date=currentDate)
>   #cat(as.character(i),"position on current bar is ",Posn, append = FALSE)
>
>   if(!is.na(MA[i-2]))
>   {
>     if( Posn == 0 ) { #no marketposition
>       if( signal[i] == 1 ) {
>         #long entry
>         #openprice <- as.double((Op(if2[i])))  #the result is the same as
> the following line
>         openprice <- as.double((Op(if2[currentDate])))
>         unitsize <- abs(as.numeric(trunc(equity/(openprice*300*0.15))))
>         addTxn(b.strategy, Symbol='if2', TxnDate=currentDate,
>                TxnPrice=openprice, TxnQty = unitsize ,
> TxnFees=-openprice*300*0.00005*unitsize, verbose = F)
>       }
>     }
>     else {
>       if( signal[i] == -1 ) {
>         #exit position
>         openprice <- as.double((Op(if2[currentDate])))
>         unitsize  <- abs(getPosQty(b.strategy, Symbol='if2',
> Date=currentDate))
>         addTxn(b.strategy, Symbol='if2', TxnDate=currentDate,
>                TxnPrice=openprice, TxnQty = -unitsize ,
> TxnFees=-openprice*300*0.00005*unitsize, verbose = F)
>       }
>     }
>   }
>
>   updatePortf(b.strategy, Dates = currentDate)
>   updateAcct(b.strategy, Dates = currentDate)
>   updateEndEq(b.strategy, Dates = currentDate)
> }
>
> these are signal's table
>
<snip>
>
> relating trades info when getting by function 'getTxns':
>
>                     Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost
> Net.Txn.Realized.PL
> 2012-01-18 00:00:00       0       0.0        0         0            0
> 0
> 2012-01-18 09:35:00       0    2580.0        0         0          NaN
> NaN
> 2012-01-18 09:54:00       0    2580.8        0         0          NaN
> NaN
> 2012-01-18 09:58:00       0    2581.8        0         0          NaN
> NaN
> 2012-01-18 10:33:00       0    2552.6        0         0          NaN
> NaN
> 2012-01-18 10:40:00       0    2552.6        0         0          NaN
> NaN
> 2012-01-18 10:57:00       0    2555.4        0         0          NaN
> NaN
> 2012-01-18 11:00:00       0    2559.6        0         0          NaN
> NaN
> 2012-01-18 11:14:00       0    2562.8        0         0          NaN
> NaN
> 2012-01-18 11:26:00       0    2555.8        0         0          NaN
> NaN
> 2012-01-18 11:29:00       0    2553.4        0         0          NaN
> NaN
> 2012-01-18 13:01:00       0    2556.0        0         0          NaN
> NaN
> 2012-01-18 13:26:00       0    2549.2        0         0          NaN
> NaN
> 2012-01-18 13:44:00       0    2553.8        0         0          NaN
> NaN
>
> from the table, only time and trade price are added, TxnQty, TxnFees, and
> other info are ignored.
>
They are not ignored.  TxnQty=0, TxnFees=0, Txn.Avg.Cost=NaN, and
Net.Txn.Realized.PL=NaN.

> why it happen,do I miss something?
>
Yes. It's fairly obvious that, since TxnQty=0, TxnFees will also equal
zero, and the two values I mentioned above will be NaN.
TxnQty=unitsize, so why does unitsize=0?  It's likely because
equity=0.

So the question isn't "why isn't addTxn working?"; it's "Why does
equity=0?".  You need to provide more information for anyone to be
able to help you answer that question.

> regards.
>

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



More information about the R-SIG-Finance mailing list