Pergunta

I'm new to R, using the quantmod() package for a project. The following block of code works:

require(quantmod) 
stocks<-c("MMM", "MSFT", "BP")
for(i in 1:length(stocks)){
getSymbols(stocks[i], from= "2013-07-01")
s<-get(stocks[i])
dr<-dailyReturn(s)
print(paste(dr))
}

However, I need to reference specific columns to calculate some technical analysis indicators from the TTR package. For example:

open<-MMM$MMM.Open
RSI(open, n=14)

When I check:

identical(s, BP) #TRUE

And this works:

BP$BP.Open

However, this does not work:

s$s.Open #NULL

To provide adequate context, my goal is to go through a vector of stocks, check for a ]condition, then calculate some technical analysis and time series figures for that day and copy it into an ARFF file for use as training examples for a machine learning environment (Weka). Thanks.

Foi útil?

Solução

It's generally easier to use the extractor functions Op et al. See ?OHLC.Transformations. Also, if you only have one symbol, you can use auto.assign=FALSE in your call to getSymbols to avoid the get call all together.

s <- getSymbols("BP", auto.assign=FALSE)

If you have multiple symbols, it's easier to store them in an environment and then loop over them with eapply:

e <- new.env()
getSymbols(stocks, env=e)
dr <- eapply(e, dailyReturn)

You can also apply TTR functions to each symbol this way.

rsi <- eapply(e, function(x) RSI(Op(x), n=14))

And you can use do.call with cbind to put them into a single object.

rsi_all <- do.call(cbind, rsi)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top