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.
function 'addtxn' in blotter package can't add intraday trade into account?
5 messages · domodo, Joshua Ulrich, Brian G. Peterson
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.
Ulrich,thanks for your reply. I found the reason why equity are always zero. the argument 'initDate' in initPortf and initAcct should be earlier than market data's start date,in this case, the start date is '2012-01-18 09:15:00',so argument 'initDate' in initPortf and initAcct should at least be '2012-01-18 09:14:00' or earlier. Having replace both of the value of initDate with '2012-01-18 09:14:00', I got the result of txns as below:
head(txns)
Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost Net.Txn.Realized.PL 2012-01-18 09:14:00 0 0.0 0.000 0 0.0 0.000 2012-01-18 09:35:00 8 2580.0 -309.600 6192000 2580.0 -309.600 2012-01-18 09:43:00 -8 2582.0 -309.840 -6196800 2582.0 4490.160 2012-01-18 09:54:00 8 2580.8 -309.696 6193920 2580.8 -309.696 2012-01-18 09:57:00 -8 2575.8 -309.096 -6181920 2575.8 -12309.096 2012-01-18 09:58:00 8 2581.8 -309.816 6196320 2581.8 -309.816 all transactions are OK,except the first transaction. why it occurs ? I tried to run the codes in debug mode line-by-line,but couldn't find the reason. PS: I couldn't find the reason why argument 'initDate' in initPortf and initAcct must be earlier than market data's start date/time in help documents. regards. -- View this message in context: http://r.789695.n4.nabble.com/function-addtxn-in-blotter-package-can-t-add-intraday-trade-into-account-tp4696291p4696314.html Sent from the Rmetrics mailing list archive at Nabble.com.
On 09/01/2014 01:00 PM, domodo wrote:
[Joshua] Ulrich,thanks for your reply. I found the reason why equity are always zero. the argument 'initDate' in initPortf and initAcct should be earlier than market data's start date,in this case, the start date is '2012-01-18 09:15:00',so argument 'initDate' in initPortf and initAcct should at least be '2012-01-18 09:14:00' or earlier. Having replace both of the value of initDate with '2012-01-18 09:14:00', I got the result of txns as below:
head(txns)
Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost Net.Txn.Realized.PL 2012-01-18 09:14:00 0 0.0 0.000 0 0.0 0.000 2012-01-18 09:35:00 8 2580.0 -309.600 6192000 2580.0 -309.600 2012-01-18 09:43:00 -8 2582.0 -309.840 -6196800 2582.0 4490.160 2012-01-18 09:54:00 8 2580.8 -309.696 6193920 2580.8 -309.696 2012-01-18 09:57:00 -8 2575.8 -309.096 -6181920 2575.8 -12309.096 2012-01-18 09:58:00 8 2581.8 -309.816 6196320 2581.8 -309.816 all transactions are OK,except the first transaction. why it occurs ? I tried to run the codes in debug mode line-by-line,but couldn't find the reason.
The first row is there only as an initialization row. You need a starting point (an empty portfolio) or none of the math makes sense.
PS: I couldn't find the reason why argument 'initDate' in initPortf and initAcct must be earlier than market data's start date/time in help documents.
I don't know why it should be unclear that initDate must be before the
first transaction.
The documentation currently describes initDate as:
initDate: A date prior to the first close price given,
used to contain initial account equity and initial position
I suppose we can try to make this more clear and say that it must be
prior to the first *transaction* and that you need to have initDate
before the market data you wish to use to mark the portfolio.
As with the initial state of the portfolio, above, where you start with
nothing. Portfolio accounting is about marking the portfolio with
realized and unrealized differences. There must be a starting point.
You have to create the portfolio and account *before* you can put
transactions in it. Transactions have to occur in order or you need to
go back and reassess what happened.
Regards,
Brian
Brian G. Peterson http://braverock.com/brian/ Ph: 773-459-4973 IM: bgpbraverock
braverock,your instruction is very good,thanks. -- View this message in context: http://r.789695.n4.nabble.com/function-addtxn-in-blotter-package-can-t-add-intraday-trade-into-account-tp4696291p4696372.html Sent from the Rmetrics mailing list archive at Nabble.com.