我想知道如何在其中使用“for 回圈”的索引 (i)。尤其是“$-notation”。
for (i in tickers){
getSymbols(i, from = '2021-01-6',
to = "2021-10-21",warnings = FALSE,
auto.assign = TRUE)
MA9 = mean(tail(i$i.Adjusted, n=9))
print(MA9)
}
感謝您花時間閱讀本文!
uj5u.com熱心網友回復:
getSymbols函式同時適用于多個股票代碼。您可以使用mget將它們放入串列中,并且由于Adjustedvalue 是第 6 列,因此您可以對其進行子集化并使用sapply.
這是一個帶有股票代碼的示例c('QQQ','SPY')。
library(quantmod)
tickers <- c('QQQ','SPY')
getSymbols(tickers, from = '2021-01-6',
to = "2021-10-21",warnings = FALSE,
auto.assign = TRUE)
sapply(mget(tickers), function(x) mean(tail(x[, 6], n=9)))
# QQQ SPY
#366.1511 442.2178
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/335728.html
