2012-02-01 13 views
1

我對R和Quantmod非常陌生。Quantmod添加指標並保存爲csv(無圖)

是否可以添加像MACD這樣的指標並將時間序列保存爲csv?

顯示圖表是很容易的:

getSymbols("AAPL",src="yahoo") 
barChart(AAPL) 
addMACD() 

但我想這些指標添加到時間序列(另存爲CSV)和不希望顯示它:)

謝謝!

如何告訴移動平均線使用近列? cbind(AAPL,SMA(AAPL,n = 50))

我該如何向csv添加額外的列?

回答

3

您可以使用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" 
) 
+0

謝謝:)我會檢查在家裏 – 2012-02-01 07:42:45

+1

+1,雖然我會建議使用'write.zoo',而不是'write.csv'。 – 2012-02-01 12:59:21

0
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([email protected]),MACD([email protected])) 
# ?write.csv is a function that will write this data frame to your current directory 
write.csv(ts_data,file="my_data.csv")