我对 R 和 Quantmod 很陌生。
是否可以添加像 MACD 这样的指标并将时间序列保存为 csv?
显示图表非常简单:
getSymbols("AAPL",src="yahoo")
barChart(AAPL)
addMACD()
但我想将指标添加到时间序列中(将其保存为 csv)并且不想显示它:)
谢谢!
如何告诉移动平均线使用收盘?e <- cbind(AAPL, SMA(AAPL, n=50))
以及如何向 csv 添加其他列?
您可以只使用cbind
添加信号。
library(quantmod)
getSymbols("AAPL",src="yahoo")
d <- cbind( AAPL, MACD( AAPL ) )
write.csv(
data.frame( date=index(d), coredata(d) ),
row.names=FALSE,
file="tmp.csv"
)
library(quantmod)
foo=getSymbols("AAPL",src="yahoo")
# tip: use ?barChart to see usage. The option plot=FALSE turns off plotting
x=barChart(foo,plot=FALSE)
# Look up ?MACD for a reference.
# x is a S4 object (https://github.com/hadley/devtools/wiki/S4)
ts_data=data.frame(cbind(x@xdata),MACD(x@xdata))
# ?write.csv is a function that will write this data frame to your current directory
write.csv(ts_data,file="my_data.csv")