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
applyIndicatorfunction as a stand alone function call doesn't really make much sense, because themktdata = GOOGargument already contains the data you want. I suspect you want to get the symbol in theapplyIndicatorcall though when you to work when you callapplyStrategythough...You can do this:
This works for
applyStrategybecause the parent environment up a few levels loops around a symbols loop (callingapplyIndicatorson each iteration), withsymbolholding 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
mktdataobject 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
xobject insidetestfun, if the column names contain the symbol label.)