xts 对象 xts_IndData3 包含一个 PositionSize 属性,该属性记录金融工具中的头寸大小和其他两个属性。NewEntrySize >= 0。NewExitSize <= 0。我将下面的代码放在一起以避免我在其他地方讨论过的 xts 错误。我试图避免的错误与在同一个表达式中引用两个不同的 xts rowID 有关。我仍然无法让下面的代码工作。提前感谢您的帮助。
已编辑...我将 xts 对象转换为数据框。这是问题的数据和新代码,可以更清楚地完成我需要完成的工作。
> 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
>
我将数据框转换为 xts 对象。这是对象的 xts 版本上的 dput(head(xts_TestData)) 的输出。
> 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")))
>