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

domodo 1111938 at qq.com
Mon Sep 1 05:23:12 CEST 2014


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.

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

1	2012-01-18 09:15:00	0
2	2012-01-18 09:16:00	0
3	2012-01-18 09:17:00	0
4	2012-01-18 09:18:00	0
5	2012-01-18 09:19:00	0
6	2012-01-18 09:20:00	0
7	2012-01-18 09:21:00	0
8	2012-01-18 09:22:00	0
9	2012-01-18 09:23:00	0
10	2012-01-18 09:24:00	0
11	2012-01-18 09:25:00	0
12	2012-01-18 09:26:00	0
13	2012-01-18 09:27:00	0
14	2012-01-18 09:28:00	0
15	2012-01-18 09:29:00	0
16	2012-01-18 09:30:00	0
17	2012-01-18 09:31:00	0
18	2012-01-18 09:32:00	-1
19	2012-01-18 09:33:00	0
20	2012-01-18 09:34:00	0
21	2012-01-18 09:35:00	1
22	2012-01-18 09:36:00	0
23	2012-01-18 09:37:00	0
24	2012-01-18 09:38:00	0
25	2012-01-18 09:39:00	0
26	2012-01-18 09:40:00	0
27	2012-01-18 09:41:00	0
28	2012-01-18 09:42:00	0
29	2012-01-18 09:43:00	-1
30	2012-01-18 09:44:00	0
31	2012-01-18 09:45:00	0
32	2012-01-18 09:46:00	0
33	2012-01-18 09:47:00	0
34	2012-01-18 09:48:00	0
35	2012-01-18 09:49:00	0
36	2012-01-18 09:50:00	0
37	2012-01-18 09:51:00	0
38	2012-01-18 09:52:00	0
39	2012-01-18 09:53:00	0
40	2012-01-18 09:54:00	1
41	2012-01-18 09:55:00	0
42	2012-01-18 09:56:00	0
43	2012-01-18 09:57:00	-1
44	2012-01-18 09:58:00	1
45	2012-01-18 09:59:00	0
46	2012-01-18 10:00:00	0
47	2012-01-18 10:01:00	0

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.

why it happen,do I miss something?

regards.



--
View this message in context: http://r.789695.n4.nabble.com/function-addtxn-in-blotter-package-can-t-add-intraday-trade-into-account-tp4696291.html
Sent from the Rmetrics mailing list archive at Nabble.com.



More information about the R-SIG-Finance mailing list