[R-SIG-Finance] Problems with values in blotter portfolio and end equity
liciobruno
felice.bruno at live.it
Tue Mar 13 21:18:42 CET 2012
Hi
I have found a mismatch with values in blotter.
I try to explain.
I have a xts object containing the prices (Open, Hi, Low, Close) of a stock.
Using blotter portfolio and account, I made a simple trading system using
moving averages as signals.
I made my experiments adding only few transactions to portfolio, using the
closing prices.
After the first transaction, the day after, I made a control about the end
equity, and I noted that it has a different value respect the numerical
difference between the close prices of the current day and the previous day.
Following is the code (I deleted the not inherent parts)
...
ptf_EMA='ptfEMA'
acc_EMA='accEMA'
initPortf(ptf_EMA, cross, initDate=start)
initAcct(acc_EMA, portfolios=ptf_EMA, initDate=start, initEq=initEq)
verbose=TRUE
for (i in (longPer + 1) : nrow(Prices)){
currentDate <- time(Prices)[i]
closePrice <- as.numeric(Cl(Prices[i]))
if (first.cross == F) {
if (EMA_short[i] > EMA_long[i]) {
if (EMA_short[i-1] > EMA_long[i-1]) {
sig.tot[i] <- 0
} else if (EMA_short[i-1] < EMA_long[i-1]) {
sig.tot[i] <- 1
first.cross <- T
addTxn(Portfolio=ptf_EMA, Symbol=cross, TxnDate=currentDate,
TxnPrice=closePrice, TxnQty = unitSize , TxnFees=0, verbose=T)
}
} else if (EMA_short[i] < EMA_long[i]) {
if (EMA_short[i-1] < EMA_long[i-1]) {
sig.tot[i] <- 0
} else if (EMA_short[i-1] > EMA_long[i-1]) {
sig.tot[i] <- -1
first.cross <- T
addTxn(Portfolio=ptf_EMA, Symbol=cross, TxnDate=currentDate,
TxnPrice=closePrice, TxnQty = -unitSize , TxnFees=0, verbose=T)
}
}
} else if(first.cross == T) {
if (EMA_short[i] > EMA_long[i]) {
if (EMA_short[i-1] > EMA_long[i-1]) {
sig.tot[i] <- 1
} else if (EMA_short[i-1] < EMA_long[i-1]) {
sig.tot[i] <- 1
Posn = getPosQty(ptf_EMA, Symbol=cross, Date=currentDate)
addTxn(Portfolio=ptf_EMA, Symbol=cross, TxnDate=currentDate,
TxnPrice=closePrice, TxnQty = -Posn , TxnFees=0, verbose=T)
addTxn(Portfolio=ptf_EMA, Symbol=cross, TxnDate=currentDate,
TxnPrice=closePrice, TxnQty = unitSize , TxnFees=0, verbose=T)
}
} else if (EMA_short[i] < EMA_long[i]) {
if (EMA_short[i-1] < EMA_long[i-1]) {
sig.tot[i] <- -1
} else if (EMA_short[i-1] > EMA_long[i-1]) {
sig.tot[i] <- -1
Posn = getPosQty(ptf_EMA, Symbol=cross, Date=currentDate)
cat('\n')
# Aggiungere transazione chiusura
addTxn(Portfolio=ptf_EMA, Symbol=cross, TxnDate=currentDate,
TxnPrice=closePrice, TxnQty = -Posn , TxnFees=0, verbose=T)
# Aggiungere transazione apertura
addTxn(Portfolio=ptf_EMA, Symbol=cross, TxnDate=currentDate,
TxnPrice=closePrice, TxnQty = -unitSize , TxnFees=0, verbose=T)
}
}
}
updatePortf(ptf_EMA, Dates = currentDate)
updateAcct(acc_EMA, Dates = currentDate)
updateEndEq(acc_EMA, Dates = currentDate)
}
...
I'm new in R, so please, excuse me if my code is not so good.
Thank you for your attention.
--
View this message in context: http://r.789695.n4.nabble.com/Problems-with-values-in-blotter-portfolio-and-end-equity-tp4470225p4470225.html
Sent from the Rmetrics mailing list archive at Nabble.com.
More information about the R-SIG-Finance
mailing list