[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 18:49:25 CEST 2014


Your first transaction is when signal[i] == -1, when Posn=0, so the
TxnQty=0 too.  I don't know how/why that causes updateEndEq() to make
the account equity equal zero, but I'm not going to dig into it
because that transaction doesn't make sense.
--
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com


On Mon, Sep 1, 2014 at 10:28 AM, domodo <1111938 at qq.com> wrote:
> 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.
>
> _______________________________________________
> 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.



More information about the R-SIG-Finance mailing list