2

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)
FXQuantTrader
  • 6,821
  • 3
  • 36
  • 67
GeV 126
  • 351
  • 1
  • 3
  • 14

1 Answers1

2

Good question.

Getting the symbol from your applyIndicator function as a stand alone function call doesn't really make much sense, because the mktdata = GOOG argument already contains the data you want. I suspect you want to get the symbol in the applyIndicator call though when you to work when you call applyStrategy though...

You can do this:

require(quantstrat)
Sys.setenv(TZ="UTC")
symbols <- c("GOOG", "AAPL")
getSymbols(symbols, src="yahoo")

currency("USD")
stock(c("GOOG", "AAPL"), "USD")

strategy.st  <- "test"
portfolio.st  <- "test"
rm.strat(strategy.st)
initPortf(portfolio.st, symbols = symbols)
strategy(strategy.st, store=TRUE)


account.st  <- "test"
initAcct(account.st, portfolios = portfolio.st, initEq = 1000)
initOrders(portfolio.st)



test_fun <- function(x){
  symbol <- parent.frame(n = 2)$symbol
  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")
applyStrategy(strategy.st, portfolio.st)

This works for applyStrategy because the parent environment up a few levels loops around a symbols loop (calling applyIndicators on each iteration), with symbol 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 to applyIndicators.

(An easier approach is also to simply extract the symbol name from the OHLC column names, which may exist in the x object inside testfun, if the column names contain the symbol label.)

FXQuantTrader
  • 6,821
  • 3
  • 36
  • 67
  • Thank you! Yes in reality i'm running the applyStrategy() function. The custom indicator uses the symbol to do a lookup on a dataframe, that is why i needed the symbol code. – GeV 126 Nov 12 '17 at 06:29
  • Apologies I've just marked as answered, thanks again – GeV 126 Dec 15 '17 at 06:46
  • Seems like a kludge though? It would be better if one could pass it into the arguments as `sym = quote(symbol)` where `test_fun` would have a second argument `sym`. – James Hirschorn Aug 03 '18 at 17:30
  • Yeah, it is, but it works for me :) Last time i looked at the source, iirc, indicators and signals do not pass through the symbol argument. there's actually some commented code that touches on passing through symbols but didn't get around to it I guess. It is easy to update the code to reflect this change. – FXQuantTrader Aug 04 '18 at 01:08