i would like to access the current symbol string eg "GOOG" inside my custom indicator function. Here is the most basic example i could make.
require(quantstrat)
Sys.setenv(TZ="UTC")
symbols <- c("GOOG", "AAPL")
getSymbols(symbols, src="yahoo")
strategy.st <- "test"
strategy(strategy.st, store=TRUE)
test_fun <- function(x){
print(symbol) ##### i want to access the current symbol eg "GOOG"
return(x)
}
add.indicator(strategy = strategy.st,
name = "test_fun",
arguments = list(x = quote(Cl(mktdata))),
label = "test_ind")
mktdata <- applyIndicators(strategy = strategy.st, GOOG)
Error in print(symbol) : object 'symbol' not found
Called from: print(symbol)
Good question.
Getting the symbol from your
applyIndicator
function as a stand alone function call doesn't really make much sense, because themktdata = GOOG
argument already contains the data you want. I suspect you want to get the symbol in theapplyIndicator
call though when you to work when you callapplyStrategy
though...You can do this:
This works for
applyStrategy
because the parent environment up a few levels loops around a symbols loop (callingapplyIndicators
on each iteration), withsymbol
holding the current symbol for which indicators are being computed.This obviously allows you to pull in external data from your global environment or other environments, if you want to do more advanced indicator construction with more than just the data in your
mktdata
object for the currenct symbol that is passed toapplyIndicators
.(An easier approach is also to simply extract the symbol name from the OHLC column names, which may exist in the
x
object insidetestfun
, if the column names contain the symbol label.)