[R-SIG-Finance] An easy way to run the quantstrat faber.R demo within an R function?
Andre Mikulec
andre_mikulec at hotmail.com
Thu Sep 11 14:26:00 CEST 2014
Hi,
I am trying to run the faber.R demo within a function
Here is the url
https://r-forge.r-project.org/scm/viewvc.php/pkg/quantstrat/demo/faber.R?view=markup&root=blotter
Within a function, when the 'faber.R code reaches initPortf() the following error is thrown.
Error in exists(paste("portfolio", name, sep = "."), envir = .blotter, :
object '.blotter' not found
what I have done to fix is insert the following code and tested running
1 within a function
2 within a lexical function
3 within R studio ( within a lexical function )
Just after the calls
require(quantstrat) call
required ( If I am running in a function )
I put the following code
if(!is.null(sys.call(sys.parent()))) {
if (!exists('.blotter' , envir=.GlobalEnv )) { .GlobalEnv$.blotter <- new.env() }
if (!exists('.strategy', envir=.GlobalEnv )) { .GlobalEnv$.strategy <- new.env() }
}
Just after the calls
getSymbols( . . . )
and optionally ( If I am running in a function ) I put in the following code.
( This does not affect the program ( but for consistency if nothing else ) )
(note: .getSymbols is not an environment )
if(!is.null(sys.call(sys.parent()))) {
.GlobalEnv$.getSymbols <- .getSymbols
}
Just after the calls
assign(symbol,x) call
(required ( If I am running in a function )
I put in the following code.
if(!is.null(sys.call(sys.parent()))) {
assign(symbol,x, envir = .GlobalEnv)
}
If I am running the code through RStudio
I surround any dev() OR chart.#() calls to prevent the following errors
1 dev() # Only one RStudio graphics device is permitted
2 chart # RStudio margins error
I put the following cde wrapper
if (!any(search() %in% "tools:rstudio")) {
# ...
}
Does any other easier way exist, so I do not have to modify
so much code?
Thanks,
Andre Mikulec
Andre_Mikulec at Hotmail.com
More information about the R-SIG-Finance
mailing list