Hi,

I just picked up R and played with few samples.  I need some help on
debugging.

How do I debug a function in a package like blotter?  I would like to step
into the function.  Right now, I got the following error from my script
below.  I copied and modified the sample script from somewhere.  I don't
know where the error message coming from (OK, I guess it is coming from
getEndEq()), the message does not have a call stack trace.  What's the
effective way of debugging such message?

====================

Error in if (length(c(year, month, day, hour, min, sec)) == 6 && c(year,  :
  missing value where TRUE/FALSE needed
In addition: Warning message:
In as_numeric(YYYY) : NAs introduced by coercion

====================
require(xts)
require(quantmod)
require(blotter)


Sys.setenv(TZ="GMT")
initDate='2009-01-01'

currency("USD")
stock("SPY",currency="USD",multiplier=1)

getSymbols('SPY',from=initDate,index.class=c("POSIXt","POSIXct"))
SPY<-adjustOHLC(SPY,use.Adjusted=T)

cl <- Cl(SPY)
sema <- EMA(Cl(SPY), 5)
lema <- EMA(Cl(SPY), 21)

symbols<-c('SPY')

ltportfolio<-'defaultPort'
initEq<-100000
ltaccount<-'defaultAccount'

rm(list=ls(envir=.blotter),envir=.blotter)
initPortf(ltportfolio,'SPY', initDate=initDate)
initAcct(ltaccount,portfolios=c(ltportfolio),
initDate=initDate,initEq=initEq)


for(i in 21+1:length(cl))
{
currentDate= time(signal)[i]
 equity = getEndEq(ltaccount, currentDate)
position = getPosQty(ltportfolio, Symbol='SPY', Date=currentDate)

if(position==0)
{Error in if (length(c(year, month, day, hour, min, sec)) == 6 && c(year,
 :
  missing value where TRUE/FALSE needed
In addition: Warning message:
In as_numeric(YYYY) : NAs introduced by coercionError in if (length(c(year,
month, day, hour, min, sec)) == 6 && c(year,  :
  missing value where TRUE/FALSE needed
In addition: Warning message:
In as_numeric(YYYY) : NAs introduced by coercion
#open a new position if signal is >0
 if( as.numeric(cl[i-1]) < as.numeric(sema[i-1])
  & as.numeric(sema[i-1]) < as.numeric(sema[i-2])
  & as.numeric(sema[i-1]) > as.numeric(lema[i-1]))
{
closePrice<-as.double(Cl(SPY[currentDate]))
 unitSize = as.numeric(trunc((equity/closePrice)))
                        # 5$ per transaction
addTxn(ltportfolio, Symbol=symbols[1],  TxnDate=currentDate,
TxnPrice=closePrice, TxnQty = unitSize , TxnFees=5, verbose=T)
 }

}
else
 {
#position is open. If signal is 0 - close it.
if((as.numeric(cl[i-1]) < as.numeric(lema[i-1])) & position > 0)
 {Error in if (length(c(year, month, day, hour, min, sec)) == 6 && c(year,
 :
  missing value where TRUE/FALSE needed
In addition: Warning message:
In as_numeric(YYYY) : NAs introduced by coercion
position = getPosQty(ltportfolio, Symbol=symbols[1], Date=currentDate)
closePrice<-as.double((Cl(SPY[currentDate])))#as.double(get(symbols[1])[i+100])

                        # 5$ per transaction
addTxn(ltportfolio, Symbol=symbols[1],  TxnDate=currentDate,
TxnPrice=closePrice, TxnQty = -position , TxnFees=5, verbose=T)
 }
}

updatePortf(ltportfolio, Dates = currentDate)
 updateAcct(ltaccount, Dates = currentDate)
updateEndEq(ltaccount, Dates = currentDate)

equity = getEndEq(ltaccount, currentDate)Error in if (length(c(year, month,
day, hour, min, sec)) == 6 && c(year,  :
  missing value where TRUE/FALSE needed
In addition: Warning message:
In as_numeric(YYYY) : NAs introduced by coercion
}

#plot(getAccount(ltaccount)[["TOTAL"]]$End.Eq)
chart.Posn(ltportfolio, Symbol = symbols[1], Dates = '2009::')
plot(add_SMA(n=10,col='darkgreen', on=1))

getTxns(ltportfolio, Symbol='SPY', Date="2009::")
===========

Error in if (length(c(year, month, day, hour, min, sec)) == 6 && c(year,  :
  missing value where TRUE/FALSE needed
In addition: Warning message:
In as_numeric(YYYY) : NAs introduced by coercion



-- 
LJI

	[[alternative HTML version deleted]]

