Glancing at the documentation, it looks like tq_get
supports multiple symbols, only if_supported_ticker
goes one at a time. So probably you should check all the tickers to see if they are supported, and then use tq_get
once on all the supported ones. Something like this (untested, as I don't have any of these packages):
calculate <- function (x) {
supported = sapply(x, is_supported_ticker, type = "tiingo")
result = rep(NA, length(x))
result[supported] =
(
tq_get(x[supported], get = "tiingo", from = yesterday, to = yesterday)$adjusted /
tq_get(x[supported], get = "tiingo", from = before, to = before)$adjusted
) - 1
return(result)
}
It worries me that before
and yesterday
aren't function arguments - they're just assumed to be there in the global environment. I'd suggest passing them in as arguments to calculate()
, like this:
calculate <- function (x, before, yesterday) {
supported = sapply(x, is_supported_ticker, type = "tiingo")
result = rep(NA, length(x))
result[supported] =
(
tq_get(x[supported], get = "tiingo", from = yesterday, to = yesterday)$adjusted /
tq_get(x[supported], get = "tiingo", from = before, to = before)$adjusted
) - 1
return(result)
}
# then calling it
calculate(top$ticker, before = <...>, yesterday = <...>)
This way you can pass values in for before
and yesterday
on the fly. If they are objects in your global environment, you can simply use calculate(top$ticker, before, yesterday)
, but it gives you freedom to vary those arguments without redefining those names in your global environment.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…