I'm new to R, using the quantmod()
package for a project. The following block of code works:
我是R的新手,在一个项目中使用quantmod()包。以下代码块工作:
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:
但是,我需要引用特定的列来计算TTR包中的一些技术分析指标。例如:
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.
为了提供足够的上下文,我的目标是遍历一个股票向量,检查一个]条件,然后计算一些当天的技术分析和时间序列数据,并将其复制到一个ARFF文件中,用作机器学习环境(Weka)的训练示例。谢谢。
1 个解决方案
#1
3
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.
通常更容易使用提取函数Op等。另外,如果你只有一个符号,你可以使用auto。在调用getSymbols时,assign=FALSE,以避免所有的get调用。
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
:
如果您有多个符号,那么更容易将它们存储在一个环境中,然后使用eapply循环它们:
e <- new.env()
getSymbols(stocks, env=e)
dr <- eapply(e, dailyReturn)
You can also apply TTR functions to each symbol this way.
您也可以这样对每个符号应用TTR函数。
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.
你可以用do。调用cbind将它们放入单个对象中。
rsi_all <- do.call(cbind, rsi)
#1
3
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.
通常更容易使用提取函数Op等。另外,如果你只有一个符号,你可以使用auto。在调用getSymbols时,assign=FALSE,以避免所有的get调用。
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
:
如果您有多个符号,那么更容易将它们存储在一个环境中,然后使用eapply循环它们:
e <- new.env()
getSymbols(stocks, env=e)
dr <- eapply(e, dailyReturn)
You can also apply TTR functions to each symbol this way.
您也可以这样对每个符号应用TTR函数。
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.
你可以用do。调用cbind将它们放入单个对象中。
rsi_all <- do.call(cbind, rsi)