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

domodo 1111938 at qq.com
Mon Sep 1 17:28:28 CEST 2014


this is data file :  ifBAC2.csv
<http://r.789695.n4.nabble.com/file/n4696307/ifBAC2.csv>  
and below is complete code

[code]
library(blotter)
library(quantstrat)

#initialize environtments
currency("USD")
startdate <- '2012-01-18'
finaldate <- '2012-01-19'

future("if2", currency = "USD", multiplier = 300, tick_size = 0.2)
Sys.setenv(TZ = "UTC") 
rm(list=ls(envir=.blotter),envir=.blotter)
b.strategy <- "strategy"

initPortf(b.strategy, "if2", initDate = startdate)
initAcct(b.strategy, portfolios = b.strategy, initDate = startdate, initEq =
1e+06) 

ifBAC2 <- read.table("C:/ifBAC2.csv", head = F, sep = ",")
coredata <- ifBAC2[3:6]
rownames(coredata) <- as.POSIXlt(paste(ifBAC2[,1],ifBAC2[,2]))
ifxts <- as.xts(coredata)
colnames(ifxts) <- c("open","high","low","close")
if2 <- ifxts['2012-01-18 09:15:00/2012-01-19 15:14:00']
if2$SMA15 <- SMA(Cl(if2),15)

#custom theme
myTheme <- chart_theme()
myTheme$col$dn.col <- "lightgreen"
myTheme$col$up.col <- "red"
myTheme$col$dn.border <- "grey"
myTheme$col$up.border <- "grey"

MA <- if2$SMA15
C <- Cl(if2)
O <- Op(if2)

#trading signal judgement
signal <- ifelse(lag(C)>lag(MA) & lag(C,2)<lag(MA,2),1,
                 ifelse(lag(C)<lag(MA) & lag(C,2)>lag(MA,2),-1,0))
signal[is.na(signal)] <- 0

#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)
}

chart.Posn(b.strategy, Symbol = "if2", theme = myTheme, TA =
"add_SMA(n=15,col=4)")

txns <- getTxns(Portfolio = b.strategy, Symbol = "if2")
tstats <- tradeStats(Portfolio = b.strategy, Symbol = "if2")
[/code]



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



More information about the R-SIG-Finance mailing list