Skip to content
Prev 7201 / 15274 Next

Blotter example by kafka from R-bloggers

My apologies.

I did not realize the script worked so slowly.  I reduced the time scale 
it covered so it commenced at the beginning of the year and it did run 
to completion.  I will try the full term and see if it produces the same 
graphs as the original example.

I'm always a bit worried about warnings as they often mean something is 
going wrong and it might be useful if kafta had warned one not to worry 
about them.  Mind you I think he did say it all took a long time ;-)

I can assure you I do try and read man before I ask for help but dealing 
with other people's code is not always easy particularly when working 
with a programming system that uses a different paradigm like R with its 
emphasis on operations on vectors and the like. and the extensive use of 
calls to functions each of which often require a wet towel and cup of 
coffee to understand.

I added the parameter definitions you suggest:

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

and the warnings reduced to one:

Warning messages:
1: In updatePortf(ltportfolio, Dates = currentDate) :
   Incompatible methods ("Ops.Date", "Ops.POSIXt") for ">="

updatePortf goes:

updatePortf
function (Portfolio, Symbols = NULL, Dates = NULL, Prices = NULL,
     ...)
{
     pname <- Portfolio
     Portfolio <- getPortfolio(pname)
     if (is.null(Symbols)) {
         Symbols = names(Portfolio$symbols)
     }
     for (symbol in Symbols) {
         tmp_instr <- try(getInstrument(symbol))
         .updatePosPL(Portfolio = pname, Symbol = as.character(symbol),
             Dates = Dates, Prices = Prices, ... = ...)
     }
     Portfolio <- getPortfolio(pname)
     if (is.null(Dates))
         Dates <- time(Portfolio$symbols[[1]]$posPL)
     Attributes = c("Long.Value", "Short.Value", "Net.Value",
         "Gross.Value", "Period.Realized.PL", "Period.Unrealized.PL",
         "Gross.Trading.PL", "Txn.Fees", "Net.Trading.PL")
     summary = NULL
     tmp.attr = NULL
     for (attribute in Attributes) {
         result = NULL
         switch(attribute, Net.Value = , Gross.Value = , Long.Value = ,
             Short.Value = , {
                 if (is.null(tmp.attr)) {
                   table = .getBySymbol(Portfolio = Portfolio,
                     Attribute = "Pos.Value", Dates = Dates, Symbols = 
Symbols)
                   tmp.attr = "Pos.Value"
                 }
                 switch(attribute, Gross.Value = {
                   result = xts(rowSums(abs(table), na.rm = TRUE),
                     order.by = index(table))
                 }, Long.Value = {
                   tmat = apply(table, MARGIN = c(1, 2), FUN = max,
                     0)
                   result = xts(rowSums(tmat, na.rm = TRUE), order.by = 
index(table))
                 }, Short.Value = {
                   tmat = apply(table, MARGIN = c(1, 2), FUN = min,
                     0)
                   result = xts(rowSums(tmat, na.rm = TRUE), order.by = 
index(table))
                 }, Net.Value = {
                   result = xts(rowSums(table, na.rm = TRUE),
                     order.by = index(table))
                 })
             }, Period.Realized.PL = , Period.Unrealized.PL = ,
             Gross.Trading.PL = , Txn.Fees = , Net.Trading.PL = {
                 table = .getBySymbol(Portfolio = Portfolio, Attribute = 
attribute,
                   Dates = Dates, Symbols = Symbols)
                 tmp.attr = NULL
                 result = xts(rowSums(table, na.rm = TRUE), order.by = 
index(table))
             })
         colnames(result) = attribute
         if (is.null(summary)) {
             summary = result
         }
         else {
             summary = cbind(summary, result)
         }
     }
     if (!is.timeBased(Dates))
         Dates = time(Portfolio$symbols[[1]][Dates])
     startDate = first(xts:::.parseISO8601(Dates))$first.time -
         1
     if (attr(Portfolio, "initDate") >= startDate | 
length(Portfolio$summary) ==
         0) {
         Portfolio$summary <- summary
     }
     else {
         Portfolio$summary <- rbind(Portfolio$summary[paste("::",
             startDate, sep = "")], summary)
     }
     assign(paste("portfolio", pname, sep = "."), Portfolio, envir = 
.blotter)
     return(pname)
}
<environment: namespace:blotter>
 >

"Ops.Date", "Ops.POSIXt" don't appear in the function call so they must 
be somewhere deeper.  I'm afraid I'm currently a windows user so grep is 
not available and the windows native text search didn't reveal much.  
However, I did find some references in the documentation (Date-Time 
Classes, Operators on the Date Class & S3 Group Generic Functions) but 
Ops.POSIXt doesn't appear therein only POSIXlt and Ops.POSIXct.  Is 
there a typo somewhere ?

It would be nice to get rid of the warnings.

Stephen Choularton Ph.D., FIoD
On 28/12/2010 9:56 PM, Brian G. Peterson wrote: