R:XTS对象的奇怪行为

时间:2018-05-13 06:07:18

标签: r xts quantstrat blotter

编辑(1):这是sessionInfo():

> sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils    
[5] datasets  methods   base     

other attached packages:
 [1] FSVBackTest_0.0.0.9000       
 [2] blotter_0.14.2               
 [3] FSVTradingRules_0.0.0.9000   
 [4] FinancialInstrument_1.2.0    
 [5] quantmod_0.4-13.1            
 [6] FSVIndicators_0.0.0.9000     
 [7] roxygen2_6.0.1               
 [8] dlm_1.1-4                    
 [9] PerformanceAnalytics_1.4.3541
[10] xts_0.10-2.1                 
[11] zoo_1.8-2                    
[12] TTR_0.23-2                   
[13] FSVPositionSizing_0.0.0.9000 
[14] FSVDataCheck_0.0.0.9000      
[15] RevoUtils_10.0.6             
[16] RevoUtilsMath_10.0.1         

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.13     xml2_1.1.1      
 [3] magrittr_1.5     rtvs_1.0.0.0    
 [5] lattice_0.20-35  R6_2.2.0        
 [7] foreach_1.4.4    stringr_1.2.0   
 [9] tools_3.4.2      grid_3.4.2      
[11] iterators_1.0.8  commonmark_1.4  
[13] codetools_0.2-15 curl_3.0        
[15] stringi_1.1.5    compiler_3.4.2  
[17] boot_1.3-20    

编辑(2):为了使这完全可重复(希望如此),我创建了一个新的库(称为DUMMY),添加了xts(0.10-2.1)作为唯一的依赖项,从github blotter repo添加了IBM.RData文件只有文件(.R,.Rdata,.Rda或其他),重新启动R,只将该库加载到全局环境中,我得到以下sessionInfo():

> sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] DUMMY_0.0.0.9000     xts_0.10-2.1         zoo_1.8-2           
[4] RevoUtils_10.0.6     RevoUtilsMath_10.0.1

loaded via a namespace (and not attached):
[1] compiler_3.4.2  grid_3.4.2      rtvs_1.0.0.0   
[4] lattice_0.20-35

我的描述文件如下所示:

Package: DUMMY
Title: What the Package Does (one line, title case)
Version: 0.0.0.9000
Authors@R: person("First", "Last", email = "first.last@example.com", role = c("aut", "cre"))
Description: What the package does (one paragraph).
Depends: R (>= 3.4.2),
         xts (>= 0.10-2.1)
License: What license is it under?
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.0.1

现在,当我调用特定的IBM xts对象行时,我得到了

> class(IBM)
[1] "xts" "zoo"
> IBM[1,]
     IBM.Open IBM.High IBM.Low IBM.Close IBM.Volume IBM.Adjusted
> IBM[21,]
     IBM.Open IBM.High IBM.Low IBM.Close IBM.Volume IBM.Adjusted
> IBM[20,]
     IBM.Open IBM.High IBM.Low IBM.Close IBM.Volume IBM.Adjusted
> IBM[2,]
     IBM.Open IBM.High IBM.Low IBM.Close IBM.Volume IBM.Adjusted

原始问题:

突然间,我似乎正在观察xts对象的奇怪行为,并且它导致了斑点(和量子)函数不再正常工作。甚至演示都失败了。作为参考,我使用xts_0.10-2.1,blotter_0.14.2& zoo_1.8-2。如果我使用来自IBM的吸墨器的内部数据,引用IBM的第一行会产生列名,而不是第一行的IBM价格数据。

> IBM[1,]
 IBM.Open IBM.High IBM.Low IBM.Close IBM.Volume
 IBM.Adjusted

供参考,

> dim(IBM)
[1] 21  6

要获取第一行IBM价格数据,我需要引用xts对象的前两行。

> IBM[1:2,]
                IBM.Open IBM.High IBM.Low IBM.Close
2007-01-03 22:00:00    97.25    98.79   96.88     98.31
                IBM.Volume IBM.Adjusted
2007-01-03 22:00:00   10524500        94.76

当我尝试引用IBM数据的最后一行时,我得到了

> IBM[nrow(IBM), ]
 IBM.Open IBM.High IBM.Low IBM.Close IBM.Volume
 IBM.Adjusted

如果我尝试引用最后两行IBM数据

> IBM[20:21,]
                IBM.Open IBM.High IBM.Low IBM.Close
2007-01-31 22:00:00    98.97    99.18   97.96        99
                IBM.Volume IBM.Adjusted
2007-01-31 22:00:00    6610700        95.43

如果我尝试使用tail,

> tail(IBM, 2)
                IBM.Open IBM.High IBM.Low IBM.Close
2007-01-31 22:00:00    98.97    99.18   97.96        99
                IBM.Volume IBM.Adjusted
2007-01-31 22:00:00    6610700        95.43

当我尝试从吸墨纸上运行amzn_test演示时,

> demo(package = "blotter")
> demo("amzn_test")

Type  <Return>   to start : 

    demo(amzn_test)
    ---- ~~~~~~~~~



> require(blotter)

> # Remove portfolio and account data if run previously
> try(rm("portfolio.amzn_port","account.amzn_acct",pos=.blotter), silent = TRUE)

> # load the example data
> data("amzn")

> currency("USD")
[1] "USD"

> stock("amzn",currency="USD",multiplier=1)
[1] "amzn"

> # Initialize the Portfolio
> initPortf("amzn_port",symbols="amzn",initDate="2010-01-14")
[1] "amzn_port"

> initAcct("amzn_acct",portfolios="amzn_port",initDate="2010-01-14", initEq=10000)
[1] "amzn_acct"

> # look at the transactions data
> amzn.trades
                    TxnPrice TxnQty
2010-01-14 07:18:50   127.49   -400
2010-01-14 07:18:53   127.49    400
2010-01-14 07:21:50   127.26   -300
2010-01-14 07:21:53   127.26    300
2010-01-14 07:23:07   127.17    100
2010-01-14 07:23:10   127.19   -100
2010-01-14 07:37:56   127.12   -400
2010-01-14 07:37:59   127.12    400
2010-01-14 07:38:32   127.23   -500
2010-01-14 07:38:35   127.23    500
2010-01-14 07:38:37   127.27    300
2010-01-14 07:38:40   127.30   -300
2010-01-14 07:46:08   126.95  -1300
2010-01-14 07:46:11   126.95   1300

> # Add the transactions to the portfolio
> blotter:::addTxns("amzn_port","amzn",TxnData=amzn.trades,verbose=TRUE)
Error in if (TxnDate < lastTxnDate) { : argument is of length zero
In addition: Warning messages:
1: In rm("portfolio.amzn_port", "account.amzn_acct", pos = .blotter) :
  object 'portfolio.amzn_port' not found
2: In rm("portfolio.amzn_port", "account.amzn_acct", pos = .blotter) :
  object 'account.amzn_acct' not found
> 

我认为发生此错误是因为引用最后一行数据(其中lastTxnDate来自)会产生列名而不是最后一行数据,index()将从中获取日期。有没有人见过这个?它刚刚开始发生在过去几天,所以最近的问题。谢谢你的帮助!

0 个答案:

没有答案