我一直在阅读 Guy 的 quantstrat 讲座(下面的链接),在反复尝试重新执行代码之后,我遇到了一些初始错误,这些错误导致讲座中的大部分后续代码无法运行。
这是代码(从讲座中复制,经过非常小的重新安排):
rm(list=ls(all=TRUE)) #added this to delete memory
library(quantstrat)
library(blotter) #added this hoping it would rectify the errors
library(FinancialInstrument) #added this hoping it would rectify the errors
# initialize portfolio, accounts and orders
qs.strategy <- "qsFaber"
initPortf(qs.strategy, 'SPY', initDate = '1997-12-31')
initAcct(qs.strategy, portfolios = qs.strategy, initDate = '1997-12-31', initEq= 1e6)
以下是我收到的错误:
1)
> initPortf(qs.strategy, 'SPY', initDate = '1997-12-31')
Error in exists(paste("portfolio", name, sep = "."), envir = .blotter, :
object '.blotter' not found
2)
> initAcct(qs.strategy, portfolios = qs.strategy, initDate = '1997-12-31', initEq= 1e6)
Error in exists(paste("account", name, sep = "."), envir = .blotter, inherits = TRUE) :
object '.blotter' not found
我必须直接下载吸墨纸,因为我使用的是 Windows 64 位,但尽管从讲座中复制了代码,但我不确定为什么会出现这些错误。我的搜索工作表明,一部分吸墨纸演变成 FinancialInstrument 包,但即使在清除内存并加载 FinancialInstruments 之后,我仍然遇到同样的错误。
任何帮助将不胜感激。
讲座链接:http://www.r-programming.org/files/quantstrat-I.pdf
最佳答案
吸墨纸和 quantstrat 包将东西存储在 .GlobalEnv
中。 (这是他们不在 CRAN 上的原因之一。)当您运行 rm(list=ls(all=TRUE))
时,您正在删除那些包希望能够在您的工作区中找到的内容。为了让一切正常工作,您必须在 globalenv() 中放回几个环境。运行这两行代码后,我认为您的代码将起作用。
.blotter <- new.env()
.strategy <- new.env()
过去,FinancialInstrument 用于创建
.instrument
.GlobalEnv
中的环境(后来期望它存在)。几年前,我将其更改为 .instrument
现在存储在 FinancialInstrument 命名空间中。由于该更改是在 Guy 的幻灯片之后进行的,因此代码不兼容。幻灯片 14-15 应更改为currency("USD")
getInstrument("USD")
stock("SPY", "USD")
getInstrument("SPY")
或者更密切地遵循他的原始代码,
get("USD", envir=FinancialInstrument:::.instrument)
get("SPY", envir=FinancialInstrument:::.instrument)
通过将包级对象存储在包的命名空间中,用户可以自由地从
globalenv()
中删除所有内容。不会破坏任何包的代码。
关于r - Guy Yollin 的 QuantStrat I 讲座问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17120496/