The xts object, xts_IndData3 contains a PositionSize property that records the position size in a financial instrument and two other properties. NewEntrySize is >= 0. NewExitSize is <= 0. I put together the code below to avoid xts errors I've seen discussed elsewhere. The error I've tried to avoid has to do with referencing two different xts rowIDs within the same expression. Still I can't get the code below to work. Thanks in advance for assistance.
EDITED... I transformed the xts object to a data frame. Here's the data and new code with the problem doing what I need to accomplish clearer.
> str(TestData)
'data.frame': 12 obs. of 5 variables:
$ DateTime : Date, format: "2012-07-27" "2012-07-27" "2012-07-27" ...
$ NewEntrySize : int 0 0 0 1 0 0 0 0 0 0 0 0...
$ NewExitSize : int 0 0 0 0 0 0 0 0 0 -1 0 0...
$ PositionSize : int 0 0 0 1 0 0 0 0 0 -1 0 0...
$ DesiredResultPositionSize: int 0 0 0 1 1 1 1 1 1 0 0 0...
>
> PositionSizeLag <- 0
> PositionSizeLag <- as.integer( PositionSizeLag )
> TestData$PositionSize <- as.integer( TestData$PositionSize )
> TestData$NewEntrySize <- as.integer( TestData$NewEntrySize )
> TestData$NewExitSize <- as.integer( TestData$NewExitSize )
> for (i in 1 : nrow( TestData )) {
+ TestData[i]$PositionSize <- PositionSizeLag +
+ TestData[i]$NewEntrySize +
+ TestData[i]$NewExitSize
+ PositionSizeLag <- TestData[i]$PositionSize
+ }
Error in `$<-.data.frame`(`*tmp*`, "PositionSize", value = numeric(0)) :
replacement has 0 rows, data has 12
>
I transformed the data frame to an xts object. Here's the output of a dput(head(xts_TestData)) on the xts version of the object.
> dput(head(xts_TestData))
structure(c("2012-07-27", "2012-07-27", "2012-07-27", "2012-07-27",
"2012-07-27", "2012-07-27", "0", "0", "0", "1", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "1", "0", "0", "0", "0",
"0", "1", "1", "1"), class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", index = structure(c(1343347200,
1343347200, 1343347200, 1343347200, 1343347200, 1343347200), tzone = "UTC", tclass = "Date"), .Dim = c(6L,
5L), .Dimnames = list(NULL, c("DateTime", "NewEntrySize", "NewExitSize",
"PositionSize", "DesiredResultPositionSize")))
>