我想将几个xts对象合并到一个xts对象中。这样我就可以在收盘价格之间获得相关矩阵。
下面的代码可以下拉外汇数据
require(xts)
symbols <- c("AUDJPY", "AUDUSD", "CHFJPY", "EURCHF", "EURGBP", "EURJPY",
"EURUSD", "GBPCHF", "GBPJPY", "GBPUSD", "NZDUSD", "USDCAD",
"USDCHF", "USDJPY", "XAGUSD", "XAUUSD")
fxhistoricaldata <- function(Symbol, timeframe, download = FALSE, bandwidth) {
# setup temp folder
temp.folder <- tempdir()
filename <- paste(temp.folder, '/',"fxhistoricaldata_",Symbol ,"_" ,timeframe,".csv", sep='')
if(download) {
downloadfile <- paste("http://api.fxhistoricaldata.com/v1/indicators?instruments=" ,Symbol ,"&expression=open,high,low,close&item_count=10000&format=csv&timeframe=" ,timeframe,sep='')
download.file(downloadfile, filename, mode = 'wb')
}
tempdf <- read.csv(filename)
colnames(tempdf) <- c("Curr","Date","Open","High","Low","Close")
tempdf <- tempdf[c("Date","Open","High","Low","Close")]
result <- xts(tempdf[,-1], order.by=as.POSIXct(tempdf[,1]))
return(result)
}
AUDJPY <- fxhistoricaldata('AUDJPY' ,'day',download=T,5)
AUDUSD <- fxhistoricaldata('AUDUSD' ,'day',download=T,5)
CHFJPY <- fxhistoricaldata('CHFJPY' ,'day',download=T,5)
EURCHF <- fxhistoricaldata('EURCHF' ,'day',download=T,5)
EURGBP <- fxhistoricaldata('EURGBP' ,'day',download=T,5)
EURJPY <- fxhistoricaldata('EURJPY' ,'day',download=T,5)
EURUSD <- fxhistoricaldata('EURUSD' ,'day',download=T,5)
GBPCHF <- fxhistoricaldata('GBPCHF' ,'day',download=T,5)
GBPJPY <- fxhistoricaldata('GBPJPY' ,'day',download=T,5)
GBPUSD <- fxhistoricaldata('GBPUSD' ,'day',download=T,5)
NZDUSD <- fxhistoricaldata('NZDUSD' ,'day',download=T,5)
USDCAD <- fxhistoricaldata('USDCAD' ,'day',download=T,5)
USDCHF <- fxhistoricaldata('USDCHF' ,'day',download=T,5)
USDJPY <- fxhistoricaldata('USDJPY' ,'day',download=T,5)
XAGUSD <- fxhistoricaldata('XAGUSD' ,'day',download=T,5)
XAUUSD <- fxhistoricaldata('XAUUSD' ,'day',download=T,5)
我想要的结果看起来像这样,收盘价低于符号名称,但所有符号名称
AUDJPY AUDUSD
2016-01-01 1.200 1.300
2016-01-02 1.21 1.31
其中一个xts对象的示例
答案 0 :(得分:1)
使用quantmod
加载项qmao
可以轻松完成此操作。以几个股票代码符号为例,下载数据(或从文件导入数据)为xts-objects
,然后一次创建一个价格或回报的对象。假设您想要从每日回报中创建一个相关矩阵:
library(qmao)
tickers <- c('MSFT','AAPL','AMZN')
getsSymbols(tickers, from = '2010-01-01')
# xts-object of daily returns (RF stands for `Return Frame`,PF returns a
# Price Frame)
returns <- RF(tickers, silent = TRUE, type = 'discrete')
> tail(returns)
MSFT AAPL AMZN
2017-01-10 -0.0003192848 0.001008513 -0.001279876
2017-01-11 0.0091025233 0.005373176 0.003920085
2017-01-12 -0.0091786360 -0.004175365 0.018297408
2017-01-13 0.0014374700 -0.001760998 0.004301657
2017-01-17 -0.0027113556 0.008064508 -0.009080505
2017-01-18 -0.0004797537 -0.000083350 -0.002766377
获得相关矩阵:
> cor(returns,use = 'pairwise.complete.obs')
MSFT AAPL AMZN
MSFT 1.0000000 0.4655617 0.4701170
AAPL 0.4655617 1.0000000 0.4390303
AMZN 0.4701170 0.4390303 1.0000000
查看函数PF
和RF
它们是紧凑且非常有用的。
答案 1 :(得分:0)
考虑构建一个xts对象列表,在列名前加上相应的符号,然后运行merge.xts
:
xtsList <- lapply(symbols, function(s) {
df <- get(s)
colnames(df) <- paste0(s, "_", colnames(df))
return(df)
})
masterxts <- do.call(merge, xtsList)
答案 2 :(得分:0)
使用getSymbols找到更好的解决方案。 感谢您的帮助。
require(lubridate)
require(quantmod)
symbols <- c("AUD/JPY",
"AUD/USD",
"CHF/JPY",
"EUR/CHF",
"EUR/GBP",
"EUR/JPY",
"EUR/USD",
"GBP/CHF",
"GBP/JPY",
"GBP/USD",
"NZD/USD",
"USD/CAD",
"USD/CHF",
"USD/JPY",
"XAG/USD",
"XAU/USD"
)
fixedsymbols <- lapply(symbols, function(x) {
gsub("/", "", x)
})
getSymbols(symbols,src="oanda", from="2014-08-05", to="2017-01-17")
xtsList <- lapply(fixedsymbols, function(s) {
df <- get(s)
colnames(df) <- colnames(df)
return(df)
})
masterxts <- do.call(merge, xtsList)