I've been going through Guy's quantstrat lecture (link below) and after repeatedly attempting to re-execute the code, I'm getting a few initial errors that are preventing most of the subsequent code in the lecture from functioning.
Here is the code (copied from the lecture with very minor re-arrangements):
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)
Here are the errors I am getting:
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
I had to directly download blotter as I am using Windows 64 bit, but despite copying the code from the lecture, I am unsure why I am getting those errors. My search efforts have indicated that a portion of blotter evolved into the FinancialInstrument package, but even after clearing memory and loading FinancialInstruments I am still getting the same error.
Any help would be highly appreciated.
LINK to lecture: http://www.r-programming.org/files/quantstrat-I.pdf
The sheets by Guy Yollin are excellent learning material, but unfortunately they are somewhat outdated (2011). Many changes have been made to blotter, quantstrat and other packages over the last 2 years, and much of the code in Guy's sheets will no longer run as such.
As far as the quantstrat package is concerned, you may want to take a look at the sheets from the R/Finance 2013 conference in Chicago; you can get a copy at http://www.rinfinance.com/agenda/2013/workshop/Humme+Peterson.pdf.
UPDATE: Guy Yollin has updated his slides to the latest quantstrat as of August 2013, they are available here http://www.r-programming.org/papers
The blotter and quantstrat packages store things in the .GlobalEnv
(which is one reason they're not on CRAN.) When you run rm(list=ls(all=TRUE))
, you are removing things, that those packages expect to be able to find in your workspace. In order for everything to work, you have to put a couple environments back in your globalenv(). After running these two lines of code, I think your code will work.
.blotter <- new.env()
.strategy <- new.env()
In the past, FinancialInstrument used to create a .instrument
environment in the .GlobalEnv
(and later expect it to exist). A couple years ago, I changed it so that .instrument
is now stored in the FinancialInstrument namespace. Since that change came after Guy's slides, the code is not compatible. Slides 14-15 should be changed to
currency("USD")
getInstrument("USD")
stock("SPY", "USD")
getInstrument("SPY")
Or to more closely follow his original code,
get("USD", envir=FinancialInstrument:::.instrument)
get("SPY", envir=FinancialInstrument:::.instrument)
By storing package level objects in the package's namespace, the user is free to remove everything from the globalenv()
without breaking any of the package's code.
来源:https://stackoverflow.com/questions/17120496/guy-yollins-quantstrat-i-lecture-issue