Guy Yollin的QuantStrat I讲座问题

3 r quantstrat blotter

我一直在经历Guy的量子讲座(下面的链接),并且在反复尝试重新执行代码之后,我得到一些初始错误,这些错误阻止了讲座中的大部分后续代码的运行.

这是代码(从讲座中复制,只需很少的重新安排):

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)
Run Code Online (Sandbox Code Playgroud)

以下是我得到的错误:

1)

> initPortf(qs.strategy, 'SPY', initDate = '1997-12-31')
Error in exists(paste("portfolio", name, sep = "."), envir = .blotter,  : 
object '.blotter' not found
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

当我使用Windows 64位时,我不得不直接下载吸墨纸,但尽管从讲座中复制了代码,但我不确定为什么我会收到这些错误.我的搜索努力表明,一部分吸墨纸演变成了FinancialInstrument包,但即使在清除内存和加载FinancialInstruments之后,我仍然会遇到同样的错误.

任何帮助将受到高度赞赏.

链接讲座:http://www.r-programming.org/files/quantstrat-I.pdf

GSe*_*See 8

吸墨纸和quantstrat包存储在中.GlobalEnv(这是他们不在CRAN上的一个原因.)当你运行时rm(list=ls(all=TRUE)),你正在删除这些包期望能够在你的工作区中找到的东西.为了使一切工作,您必须将几个环境放回到globalenv()中.运行这两行代码后,我认为您的代码将起作用.

.blotter <- new.env()
.strategy <- new.env()
Run Code Online (Sandbox Code Playgroud)

过去,FinancialInstrument曾用于创建一个.instrument环境.GlobalEnv(后来预计它会存在).几年前,我更改了它,.instrument现在存储在FinancialInstrument命名空间中.由于在Guy的幻灯片之后发生了这种变化,因此代码不兼容.幻灯片14-15应更改为

currency("USD")
getInstrument("USD")
stock("SPY", "USD")
getInstrument("SPY")
Run Code Online (Sandbox Code Playgroud)

或者更密切地遵循他的原始代码,

get("USD", envir=FinancialInstrument:::.instrument)
get("SPY", envir=FinancialInstrument:::.instrument)
Run Code Online (Sandbox Code Playgroud)

通过将包级别对象存储在包的命名空间中,用户可以自由地删除所有内容,而globalenv()不会破坏任何包的代码.